

var mac = false;//var mac = (navigator.userAgent.indexOf("Mac")!=-1);
var macCondition, macAction;

function CFC_getIFrameObj(location)
	{
	var iframe;
	if(!location || location == "document")
		{
		iframe = (document.all) ? document["CFC_Tab_FRAME"] : document.getElementsByName("CFC_Tab_FRAME").item(0);
		}
	else
		{
		iframe = (document.all) ? parent["CFC_Tab_FRAME"] : parent.document.getElementsByName("CFC_Tab_FRAME").item(0);
		}
	return iframe;
	}

function CFC_getIFrameDoc(iFrameObj)
	{
	var doc = (document.all) ? iFrameObj.document : iFrameObj.contentDocument;
	return doc;
	}

function CFC_getIFrameScriptContext(location)
	{
	var iframe = CFC_getIFrameObj(location);
	return (document.all) ? iframe : iframe.contentDocument.defaultView;
	}

function CFC_parSel(parSel)
	{
	top.document.location.href=top.document.location.href+"&parSel="+parSel+"&cancel=back";
	}

function CFC_dialogAny(dialogAny,parSel)
	{
	var tmp=top.document.location.href;
	var dest;
	tmp=tmp.split("?");
	dest=tmp[0]+"?dialogAny="+dialogAny+"&parSel="+parSel+"&"+tmp[1]+"&cancel=back";
	top.document.location.href=dest;
	}

function CFC_unCover(cover,tabPage)
	{
	if (!parent.frames.CFC_middle)
		{
		var iFrameObj = CFC_getIFrameObj();

		if (iFrameObj)
			{
			var iFrameDoc = CFC_getIFrameDoc(iFrameObj);	
			if (iFrameDoc.done)
				{
				if (cover)
					{
					if(document.getElementById&&!document.all)
						{
						
						eval("("+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");
						eval("(iFrameDoc.getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");

						eval("(iFrameDoc.getElementById('CFC_COVER'))."+document.stylePost+"width=1");
						eval("("+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"width=1");
						eval("(iFrameDoc.getElementById('CFC_COVER'))."+document.stylePost+"height=1");
						eval("("+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"height=1");						
						}
					else
						{
						eval(document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");
						eval("document.CFC_Tab_FRAME."+document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");

						eval("document.CFC_Tab_FRAME."+document.stylePre+"CFC_COVER."+document.stylePost+"width=1");
						eval(document.stylePre+"CFC_COVER."+document.stylePost+"width=1");
						eval("document.CFC_Tab_FRAME."+document.stylePre+"CFC_COVER."+document.stylePost+"height=1");
						eval(document.stylePre+"CFC_COVER."+document.stylePost+"height=1");
						}
					}
				if (tabPage>=0)
					{
					CFC_switchTab(tabPage);
					}	
				}
			}
		else
			{
			if (parent.document.done)
				{
				if (cover)
					{
					if(document.getElementById&&!document.all)
						{
						
						eval("("+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");
						eval("(parent."+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");

						eval("("+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"width=1");
						eval("(parent."+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"width=1");
						eval("("+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"height=1");
						eval("(parent."+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"height=1"); 
						}
					else
						{
						eval(document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");
						eval("parent."+document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");

						eval(document.stylePre+"CFC_COVER."+document.stylePost+"width=1");
						eval("parent."+document.stylePre+"CFC_COVER."+document.stylePost+"width=1");
						eval(document.stylePre+"CFC_COVER."+document.stylePost+"height=1");
						eval("parent."+document.stylePre+"CFC_COVER."+document.stylePost+"height=1");
						}
					}
				if (tabPage>=0)
					{
					parent.CFC_switchTab(tabPage);
					}	
				}
			}
		}
	else
		{
		if (parent.frames.CFC_top.document.done==1 && parent.frames.CFC_middle.document.done==1 && parent.frames.CFC_bottom.document.done==1)
			{
			if (cover)
				{
				if(document.getElementById&&!document.all)
					{
					
					eval("(parent.frames.CFC_top."+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");
					eval("(parent.frames.CFC_middle."+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");
					eval("(parent.frames.CFC_bottom."+document.stylePre+"getElementById('CFC_COVER'))."+document.stylePost+"visibility='hidden'");
					}
				else
					{
					eval("parent.frames.CFC_top."+document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");
					eval("parent.frames.CFC_middle."+document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");
					eval("parent.frames.CFC_bottom."+document.stylePre+"CFC_COVER."+document.stylePost+"visibility='hidden'");
					}
				}
			if (tabPage>=0)
				{
				parent.frames.CFC_top.CFC_switchTab(tabPage);
				}	
			}
		}
	}

function CFC_save()  
	{
	if (!mac)
		{
		CFC_getRichEditText();
		
		if (CFC_checkPassword() && CFC_validateInput() && CFC_checkMandatory())
			{
			if (!parent.frames.CFC_middle)
				{
				var iFrameObj = CFC_getIFrameObj("parent");
				var iFrameDoc = CFC_getIFrameDoc(iFrameObj);	
				iFrameDoc.CFCForm.submit();
				}
			else
				parent.frames.CFC_middle.document.CFCForm.submit();
			}
		}
	else
		{
		CFC_getMacRichEditText("CFC_MacSaveLater();");
		}
	}

function CFC_MacSaveLater() 
	{
	if (CFC_checkPassword())
		{
		if (!parent.frames.CFC_middle)
			document.CFC_Tab_FRAME.document.CFCForm.submit();
		else
			parent.frames.CFC_middle.document.CFCForm.submit();
		}
	}

function CFC_submit(doClose) 
	{
	if (!doClose) document.CFCForm.CloseMe.value="";
	if (!parent.frames.CFC_middle)
		parent.CFC_save();
	else
		parent.frames.CFC_bottom.CFC_save();
	}


function CFC_callInTab(call)  
	{
	if (!parent.frames.CFC_middle)
		{
		var iFrameContext = CFC_getIFrameScriptContext();
		eval("iFrameContext."+call);
		}
	else
		eval("parent.frames.CFC_middle."+call);
	}

function CFC_callOutTab(call)  
	{
	if (!parent.frames.CFC_middle)
		eval(call);
	else
		eval("parent.frames.CFC_bottom."+call);
	}


function CFC_delete(toDelete)
	{
	if (confirm("Are you sure you want to delete the selected item?"))
		{
		CFC_reloadAndClose(1,0,toDelete);
		}
	}

function CFC_deleteDb(idField)
	{
	if (confirm("Are you sure you want to delete the selected item?"))
		{
		document.location.href=document.location.href+"&db_idField="+idField+"&CloseMe=1&DeleteDb=1";
		}
	}


function CFC_cancel()
	{
	top.window.close();
	}

function CFC_cancelBack()
	{
	top.window.history.back();
	}

function CFC_openImageMapper(fieldName,imgsrc,handle,txtatom,cellId,urlPostfix)
	{
	qnow=(new Date()).valueOf();
	window.open("/vwcms_author/libs/CFC/content/controls/imagemapper.html?imgAtom="+imgsrc+"&txtatom="+txtatom+"&fieldName="+fieldName+"&handle="+handle+"&urlPostfix="+urlPostfix+"&cellId="+cellId+"&ck="+qnow, "imageMapperDialog", "height=560,width=530,resizable=1,scrollbars=1,showlocation=0,showtoolbar=0");
	}	

function CFC_closeImageMapper(dest,fieldName)
	{
	document.CFCForm[fieldName].value=dest;
	}

function CFC_openImageCrop(handle, targetField, consField, labelStruct, dialogAny, parName, parNum, consBlock, storagePre)
	{
	coords=document.CFCForm[targetField].value;
	cons=consField?document.CFCForm[consField].value:"";
	qnow=(new Date()).valueOf();
	var qry="?ck="+qnow;
	qry+="&handle="+escape(handle);
	qry+="&targetField="+escape(targetField);
	qry+="&consField="+escape(consField);
	qry+="&labelStruct="+escape(labelStruct);
	qry+="&dialogAny="+escape(dialogAny);
	qry+="&parName="+escape(parName);
	qry+="&parNum="+escape(parNum);
	qry+="&coords="+escape(coords);
	qry+="&constraints="+escape(cons);
	qry+="&consblock="+escape(consBlock);
	qry+="&storagePre="+escape(storagePre);
	
	window.open("/vwcms_author/libs/CFC/content/controls/imagecrop.html"+qry, "imageCropDialog", "height=560,width=530,resizable=1,scrollbars=1,showlocation=0,showtoolbar=0");
	}	

function CFC_closeImageCrop(dest,fieldName)
	{
	document.CFCForm[fieldName].value=dest;
	}

function CFC_reloadAndClose(reload,callback,toDelete)
	{
	if (document.all) top.window.opener.focus();

	if (reload!="noReload")
		{
		if (!callback)
			{
			var cK=CFC_cacheKiller();
			var masterHref=top.window.opener.location.href;
						
			var designMode = "";
			if (masterHref.indexOf("DesignMode")!=-1)
				{
				designMode = "&DesignMode=1";
				}
			
			var masterTop="";
			if (top.window.opener.document.body)
				masterTop=top.window.opener.document.body.scrollTop;

			if (masterHref.indexOf("?")!=-1)
				masterHref=masterHref.substring(0,masterHref.indexOf("?"));

			masterHref+="?"+ cK + designMode +"&CFC_scrollTop="+ masterTop;
			if (toDelete) masterHref+="&DeleteAtom="+toDelete;
			top.window.opener.location.href=masterHref;
			}
		else
			{
			eval("top.window.opener."+callback);
			}	
		}
	top.close();
	}



function CFC_checkPasswordDoc()
	{
	var elem, pwObj, storageLocator;

	pwObj=CFC_passwordObject;
	storageLocator=document;

	var checked=1;
	for (var elem in pwObj.Sub)
		{
		var val1=storageLocator.CFCForm[elem+"_1"].value;
		var val2=storageLocator.CFCForm[elem+"_2"].value;
		if (val1=="" && val2=="")
			{
			storageLocator.CFCForm[elem].value=pwObj.Sub[elem].value;
			}
		else
			{
			if (val1==val2)
				{
				storageLocator.CFCForm[elem].value=val1;
				}
			else
				{
				alert("The passwords do not match.");
				storageLocator.CFCForm[elem+"_1"].focus();
				checked=0
				break;
				}		
			}	
		} 
	return checked;
	}
	

function CFC_checkPassword()
	{
	var elem, pwObj, storageLocator;
	if (parent.CFC_Tab_FRAME)
		{
		var iFrameContext=CFC_getIFrameScriptContext("parent");
		pwObj=iFrameContext.CFC_passwordObject;
		storageLocator=CFC_getIFrameDoc(CFC_getIFrameObj("parent"));
		}
	else
		{
		pwObj=parent.frames.CFC_middle.CFC_passwordObject;
		storageLocator=parent.frames.CFC_middle.document;
		}
	var checked=1;
	for (var elem in pwObj.Sub)
		{
		var val1=storageLocator.CFCForm[elem+"_1"].value;
		var val2=storageLocator.CFCForm[elem+"_2"].value;
		if (val1=="" && val2=="")
			{
			storageLocator.CFCForm[elem].value=pwObj.Sub[elem].value;
			}
		else
			{
			if (val1==val2)
				{
				storageLocator.CFCForm[elem].value=val1;
				}
			else
				{
				alert("The passwords do not match.");
				storageLocator.CFCForm[elem+"_1"].focus();
				checked=0
				break;
				}		
			}	
		} 
	return checked;
	}
	

function CFC_validateInput()
	{
	var elem, validateObj, storageLocator;
	if (parent.CFC_Tab_FRAME)
		{
		var iFrameContext=CFC_getIFrameScriptContext("parent");
		validateObj=iFrameContext.CFC_validationObject;
		storageLocator=CFC_getIFrameDoc(CFC_getIFrameObj("parent"));
		}
	else
		{
		validateObj=parent.frames.CFC_middle.CFC_validationObject;
		storageLocator=parent.frames.CFC_middle.document;
		}	
	
	var checked = true;
	var failedList = new Object();
	
	for(var elem in validateObj.Sub)
		{
		var regexpStr = validateObj.Sub[elem].regexp;
		var fieldName = validateObj.Sub[elem].name;
		var regexpFlags=validateObj.Sub[elem].flags; 
		var strValue  = storageLocator.CFCForm[fieldName].value;
		
		if(regexpStr && strValue)
			{
			var aRegexp = (regexpFlags) ? new RegExp(regexpStr, regexpFlags) : new RegExp(regexpStr); 
			var matchArray = aRegexp.exec(strValue); //strValue.match(aRegexp);
			if (matchArray == null) 
				{
				failedList[fieldName] = (validateObj.Sub[elem].intlMsg) ? validateObj.Sub[elem].intlMsg : fieldName;
				checked = false;
				}
			}
		}
	if(!checked)
		{
		var str = "Validation failed:\n"
		for(var i in failedList)
			{
			str += failedList[i] + "\n";
			}
		alert(str);
		}
	return checked;
	}
	

function CFC_checkMandatory()
	{
	var elem, mandatoryObj, storageLocator;
	if (parent.CFC_Tab_FRAME)
		{
		var iFrameContext=CFC_getIFrameScriptContext("parent");
		mandatoryObj=iFrameContext.CFC_mandatoryObject;
		storageLocator=CFC_getIFrameDoc(CFC_getIFrameObj("parent"));
		}
	else
		{
		mandatoryObj=parent.frames.CFC_middle.CFC_mandatoryObject;
		storageLocator=parent.frames.CFC_middle.document;
		}	
	
	checked = true;
	var failedList = new Object();	

	for(var elem in mandatoryObj.Sub)
		{
		var test  = CFC_checkMandatoriesObj(storageLocator.CFCForm[elem]);
		
		if(!test)
			{
			failedList[elem] = mandatoryObj.Sub[elem].txt;
			checked = false;
			}
		}
	if(!checked)
		{
		var str = "At least one mandatory field was left empty:\n"
		for(var i in failedList)
			{
			str += failedList[i] + "\n";
			}
		alert(str);
		}
	return checked;	
	}


function CFC_getRichEditText()
	{
	var elem;
	var richiLocator, richiObi, storageLocator;
	if (parent.CFC_Tab_FRAME)
		{
		var iFrameContext=CFC_getIFrameScriptContext("parent");
		richiObj=iFrameContext.CFC_richEditObject;
		richiLocator=CFC_getIFrameDoc(CFC_getIFrameObj("parent"));
		storageLocator=CFC_getIFrameDoc(CFC_getIFrameObj("parent"));
		}
	else if (parent.frames.CFC_middle)
		{ 
		richiObj=parent.frames.CFC_middle.CFC_richEditObject;
		richiLocator=parent.frames.CFC_middle.document;
		storageLocator=parent.frames.CFC_middle.document;
		}
	else
		{ 
		richiObj=CFC_richEditObject;
		richiLocator=document;
		storageLocator=opener.document;
		storageLocator.images[richiObj.Sub[0].storageName+"_IMAGE"].src="/vwcms_author/libs/CFC/content/statics.richedit_edited.gif";
		}	


	for (elem in richiObj.Sub)
		{
		var thisRichi=richiObj.Sub[elem];
		if (thisRichi.isTableEdit)
			{
			var txt = richiLocator.applets[thisRichi.appletName].getCellData();
			storageLocator.CFCForm[thisRichi.storageName].value = txt;
			
			var fmtCtl = storageLocator.CFCForm[thisRichi.formatStorageName];
			if (fmtCtl)
				{
				fmtCtl.value = richiLocator.applets[thisRichi.appletName].getAllCellFormatting();
				}
			}
		else if(thisRichi.noRichiApplet)
			{
			// unescape possible escaped value
			storageLocator.CFCForm[thisRichi.storageName].value = unescape(storageLocator.CFCForm[thisRichi.storageName].value);
			}
		else
			{
			var txt = richiLocator.applets[thisRichi.appletName].getText();
			storageLocator.CFCForm[thisRichi.storageName].value = txt;
			}
		}
	}



function CFC_getMacRichEditText(callback)
	{
	var elem;
	var storageFunction, richiObi, storageLocatorName;

	if (parent.frames.CFC_middle)
		{
		richiObj           = parent.CFC_middle.CFC_richEditObject;
		storageFunction    = parent.CFC_middle.CFC_getOneMacRichEditText;
		storageLocatorName = "parent.CFC_middle.document";
		}
	else
		{
		richiObj           = CFC_richEditObject;
		storageFunction    = CFC_getOneMacRichEditText;
		storageLocatorName = "opener.document";

		// change the image to "edited"
		opener.document.images[richiObj.Sub[0].storageName+"_IMAGE"].src="/vwcms_author/libs/CFC/content/statics.richedit_edited.gif";
		}	

	macCondition  = "1";

	for (elem in richiObj.Sub)
		{
		var thisRichi = richiObj.Sub[elem];

		//var destination = "parent.CFC_middle.document.CFCForm." + thisRichi.storageName + ".value";
		var destination = storageLocatorName + ".CFCForm." + thisRichi.storageName + ".value";
		eval(destination + '=""');

		// start the text download process
		storageFunction(destination);		

		// gather condition to be met
		if (macCondition != "")
			macCondition += " && ";
		macCondition += destination;
		}

	macAction = callback;
	CFC_callWhenTrue();
	}

function CFC_callWhenTrue()
	{
	if (eval(macCondition))
		{
		eval(macAction);
		}
	else
		{
		var call = "CFC_callWhenTrue();"
		setTimeout(call, 1000);
		}
	}

function CFC_initAdWin(tabPage,noFocus)
	{
	var cover=0;
	if (document.all)
		{
		document.stylePre="";
		document.stylePost="style.";
		document.app="ie";
		if (typeof(CFC_COVER)!="undefined") cover=1;
		}
	else
		{
		document.stylePre="document.";
		document.stylePost="";
		document.app="ns";
		if(document.getElementById)
			{
			if (document.getElementById('CFC_COVER') != null) 
				{
				document.stylePost="style.";
				document.stylePre="document.";
				cover=1;
				}
			}
		else
			{
			if (typeof(document.CFC_COVER)!="undefined") cover=1;
			}
		}
	document.done=1;
	
	CFC_unCover(cover,tabPage);
	CFC_preloadButtons();

	if (!noFocus && document.CFCForm)
		{
		// try to set focus on first form element on first visible layer
		var ok = false;
		for (var i=0; i < document.CFCForm.elements.length; i++)
    	{
			// go through form elements
			var elem = document.CFCForm.elements[i];
			if (elem.type != "hidden")
				{
				// avoid hidden fields
				var eParent = elem;
	      while(eParent)
					{
					if(eParent.parentElement)
						{
						eParent = eParent.parentElement;
						if(eParent.id.indexOf("CFC_Tab_DIV0")>=0 && eParent.style.visibility!="hidden")
							{
							// form element is on a visible layer -> set focus
							elem.focus();
							return;
							}
						}
					else
						{
						// no (more) parents or netscape
						break;
						}
					}
				}
			}
		}
	}


function CFC_downloadNameStart(fileName,downName,noConfirm,callback)
	{
	var fileValueStart=document.CFCForm[fileName].value;
	CFCdownloadDoWhile=1;
	CFC_downloadNameWhile(fileName,downName,fileValueStart,noConfirm,callback);
	}

function CFC_downloadNameWhile(fileName,downName,fileValueStart,noConfirm,callback)
	{
	CFCfileName=fileName;
	CFCdownName=downName;
	CFCfileValueStart=fileValueStart;
	CFCnoConfirm=noConfirm;
	CFCcallback=callback;
	var fileValue=document.CFCForm[fileName].value;
	if (fileValue!=fileValueStart && CFCdownloadDoWhile)
		{
		CFCfileName=0;
		CFCdownName=0;
		CFCfileValueStart=0;
		CFCdownloadDoWhile=0;
		CFCcallback=0;
		CFCnoConfirm=0;
		CFC_downloadNameSet(fileName,downName,noConfirm,callback);
		}
	else
		{
		if (CFCdownloadDoWhile)
			setTimeout("CFC_downloadNameWhile(CFCfileName,CFCdownName,CFCfileValueStart,CFCnoConfirm,CFCcallback)",100);
		}
	}

function CFC_downloadNameSet(fileName,downName,noConfirm,callback)
	{
	var fileValue=document.CFCForm[fileName].value;
	fileValue=fileValue.substring(fileValue.lastIndexOf("\\")+1);
	if (downName)
		{
		if (fileValue!="" && document.CFCForm[downName].value!=fileValue)
			{
			if (document.CFCForm[downName].value=="")
				{
				document.CFCForm[downName].value=fileValue;
				}
			else if (document.CFCForm[downName].value!=fileName)
				{
				var ok;
				if (noConfirm)
					{
					ok=1;
					}
				else
					{	
					if (confirm("Select OK to change your filename as follows:\n\n"+document.CFCForm[downName].value+" --> "+fileValue))
						ok=1;
					}
				if (ok) 
					{
					document.CFCForm[downName].value=fileValue;
					}
				}
			}
		}
	if (callback)
		{
		eval(callback);
		}			
	}


function CFC_move_activate()
	{
	if (!parent.frames.CFC_middle)
		{
		var iframeContext = CFC_getIFrameScriptContext();
		iframeContext.move_activate();
		}
	else
		parent.frames.CFC_middle.move_activate();
	}


function CFC_restore_rev()
	{
	if (!parent.frames.CFC_middle)
		{
		var iframeContext = CFC_getIFrameScriptContext();
		iframeContext.restore_rev();
		}
	else
		parent.frames.CFC_middle.restore_rev();
	}


/**
 * Security limitations in Microsoft® Internet Explorer 6 SP1b 
 * (or higher) require applets with parameters containing 
 * external URLs to be drawn by an external javascript function.
 *
 * CFC_drawApplet draws applets according to applet objects.
 * Applet objects must be of the following format:
 *
 * CFC_AppletObject = new Object();
 * CFC_AppletObject.name = "CFC_Applet";
 * CFC_AppletObject.code = "";
 * CFC_AppletObject.archive = "";
 * CFC_AppletObject.width = 100;
 * CFC_AppletObject.height = 50;
 * CFC_AppletObject.style = ""; // optional
 * CFC_AppletObject.Params = new Object();
 * CFC_AppletObject.Params[n] = new Array(name,value);
 */

function CFC_drawApplet(appletObj)
	{
	if (!appletObj.style) appletObj.style = "";
	document.writeln('<applet name="'+appletObj.name+'" code="'+appletObj.code+'" archive="'+appletObj.archive+'" height="'+appletObj.height+'" width="'+appletObj.width+'" style="'+appletObj.style+'">');
	for (var i in appletObj.Params)
		{
		var param = appletObj.Params[i];
		document.writeln('	<param name="'+param[0]+'" value="'+param[1]+'">');
		}
	document.writeln('</applet>');
	}


