/*********************************************************************/
/*                                                                   */
/* IBM Confidential                                                  */
/*                                                                   */
/* OCO Source Materials                                              */
/*                                                                   */
/* Copyright IBM Corp. 2005, 2007                                    */
/*                                                                   */
/* The source code for this program is not published or otherwise    */
/* divested of its trade secrets, irrespective of what has been      */
/* deposited with the U.S. Copyright Office.                         */
/*                                                                   */
/*********************************************************************/

// SPR #TEDS6N6RZY: load controls from external script file, to avoid need for user activation (MS workaround for KB912945).
// See http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/activating_activex.asp
// 04/04/06 Bill Rodrick

function _qp_WriteHtml(theHTML)
{
	document.write(theHTML);
	return true;
}


var qp_ImageProcessText = "";

/****************************************
* Used by the Dojo controls to write
* the new image controls in the popup
* form.
****************************************/
function qp_writeDojoNewImageControls(sProgText) {

	this.qp_ImageProcessText = sProgText;
	
	var uri = dojo.uri.dojoUri("../dojowidgets/widget/templates/newimagecontrols.html");
	qp_writeDojoExternalHtml(uri);
}
function qp_writeDojoExternalHtml(sFile) {

	if (typeof(dojo) != "undefined") {
		dojo.require("dojo.widget.ContentPane");
		document.write("<div dojoType=\"ContentPane\" href=\"" + sFile + "\" executeScripts=\"true\"></div>");
	}
}

function getFiles(){
	var sFiles = '';
	if (h_ClientBrowser.hasActiveX ()) {
		sFiles = theForm.UploadControl.Attachment_Names.toLowerCase();
	}else{
		var oSelect = document.getElementById('SelID');
		var aOption = oSelect.getElementsByTagName("option");
		for (i = 0; i < aOption.length; i++){
			sValue = aOption[i].value.toLowerCase();
			iLS = sValue.lastIndexOf('\\') + 1;
			if(i != 0){
				sFiles += ',';
			}
			sFiles += sValue.substring(iLS,sValue.length);
		}
	}
	return sFiles;
}

function getWidgetId(){
	var sLocation = location.href;
	iWL = sLocation.lastIndexOf('=') + 1;
	return sLocation.substring(iWL,sLocation.length);
}

function checkFields(theForm){
	var isOk = true;
	var alertText = '';
	
	if(theForm.h_Name.value == ''){
		isOk = false;
		alertText += top.MSG_TITLE + '\n';
	}
	
	var sFiles = getFiles();
	if(sFiles.indexOf(',') > -1){
		alertText += top.MSG_ATTACH_ONE_IMAGE_FILE;
		isOk = false;
	}else if(sFiles == '' || (sFiles.indexOf('.gif') == -1 && sFiles.indexOf('.png') == -1 && sFiles.indexOf('.jpg') == -1 && sFiles.indexOf('.jpeg') == -1)){
		alertText += top.MSG_ATTACH_ONE_IMAGE_FILE;
		isOk = false;
	}
		
	if(!isOk){
		alert(top.MSG_COMPLETE_FIELDS + '\n\n' + alertText);
	}else{
		
	}
	return isOk;
}

function QDK_HTMLImageForm_OnSubmitHandlerCallback(theForm){
	return checkFields(theForm);
}

function QDK_HTMLImageForm_OnLoadHandlerCallback(theForm){
	document.getElementById('titleHL').style.display = 'block';
	document.getElementById('attachmentRead').style.display = 'none';
	document.getElementById('attachmentEdit').style.display = 'block';
	document.getElementById('attachmentControlTR').style.display = 'block';
}

function submitImageForm(){
	if(checkFields(theForm)){
		//Set global widget name
		parent.g_CurrentWidget = getWidgetId();
		
		abPreSubmitCalled = true;
		theForm.h_SetEditNextScene.value="";
		var publishOption = new Array ("","","","","");
		var bNeedsPublishLocation = false;
		var bIsNewDoc = (h_IsNewDoc == "1");
		var folderName = theForm.h_CurrentFolderName.value;
		
		var s = 'h_Jump';

		for (var i=0;i<formObj.fieldDef.length;i++) {
			s = formObj.fieldDef[i].submitLogic(self, s, i, publishOption);
			if (s == false) {
				return;
			}
		}
		
		for (var i=0; i<publishOption.length; i++) {
			if (publishOption[i] != ""){
				theForm.h_PublishOptions.value = (theForm.h_PublishOptions.value == "") ? publishOption[i] : theForm.h_PublishOptions.value + ", " + publishOption[i];
			}
		}
		
		theForm.h_NoSceneTrail.value = "0";
			
		setAnimation("h_StdPageAnimation");
		if (putAway.publishFolderName == "" && (h_Form!="0AD3FAE32A9296D10525670800167241" && h_Form!="BD931F9B1EE77301852567D6006AF8B4")) {
			publishToCurrentFolder();
		}
		
		putAway.publishAction = "h_Publish";
		s = "h_Next";
		
		theForm.h_EditAction.value = s;
		CommonFormPreSubmit( theForm);
		
		
		//hide the main frame
		var divMain = parent.dojo.byId("qp_newimage_mainpage");
		divMain.style.visibility = "hidden";
		
		//show the progress bar
		var divProg = parent.dojo.byId("qp_newimage_progress");
		divProg.innerHTML = this.qp_ImageProcessText;
		divProg.style.display = "block";
		
		
		setTimeout("theForm.submit();",200);
		

	
		return;
	}
}

function cancelImageForm(){
	abPreSubmitCalled = true;
	theForm.h_SetEditNextScene.value="";
	var publishOption = new Array ("","","","","");
	var bNeedsPublishLocation = false;
	var folderName = theForm.h_CurrentFolderName.value;
	
	theForm.h_SetPublishAction.value = "h_Discard";
	s = "h_Next";
	theForm.h_SetReturnURL.value = '<script type="text/javascript">location.href="' + "../../"+(h_Context_InFolder==1 && h_Context_InFolderProxyDocUnid==h_ReturnToPage?'h_Toc':(h_Context_InFolder==1?h_Context_InFolderName:'h_Toc'))+"/"+h_ReturnToPage+"/?OpenDocument&Form=h_PrintUI" + "" + '";<\/script>';
	
	theForm.h_EditAction.value = s;
	CommonFormPreSubmit( theForm);
	theForm.submit();
	parent.dojo.widget.byId('NewImage').hide();
	
	return;
}

function qp_adjustRTEText(sText) {
	/* Code automatically adds the default font face so that it shows properly in the font name
	* selection list */

	var sFontPad = "<font face=\"Times New Roman\" size=\"3\">&nbsp;</font>";
	
	sRet = sText;

	if (sText == "") {
		sRet = sFontPad;
	}
	
	return sRet;
}

function qp_adjustRTESelection(rte) {
	/* Manually check if the height is less than minHeight in ie since it does't truely support minheight
	* Also another fix for IE so you click to focus the rte anywhere in the editor (not just the first line of content
	* From here: http://dojotoolkit.org/pipermail/dojo-contributors/2007-February/006367.html
	*/
	if ( typeof(rte) != "undefined" && (dojo.render.html.ie60 || dojo.render.html.ie70) ) {
		var getLengthInPixels = function(element, value) {
			if (/px$/.test(value)) return parseInt(value);
			var sLeft = element.style.left;
			var rsLeft = element.runtimeStyle.left;
			element.runtimeStyle.left = element.currentStyle.left;
			element.style.left = value || 0;
			value = element.style.pixelLeft;
			element.style.left = sLeft;
			element.runtimeStyle.left = rsLeft;
			return value;
		};

		var minHeightInPx = getLengthInPixels( rte.domNode , rte.minHeight );

		var newHeight = (rte._lastHeight > minHeightInPx)?rte._lastHeight+"px":rte.minHeight;
		rte.editorObject.style.height = newHeight;
		var divWrapper = rte.document.body.getElementsByTagName('div')[0]; //dojo only puts the div rte content wrapper in IE
		divWrapper.style.height = newHeight;
	}
}



/****************************************
* This is used to tell Quickr to always 
* use the legacy rich text editor.
* 
* When set to false (default): The Dojo
* editor will show for IE and Firefox
*
* When set to true: The legacy 
* proprietary editors will show.
*
* NOTE - For IE7, setting this to true
* will result in a simple textarea since 
* IE7 no longer supports the DHTML 
* controls.
****************************************/
var QPUseLegacyRichTextEditor = false;
