LastSelected = '';
var TableRollovers = false;
var ResizePopups = false;
var PopupWidth = false;
var PopupHeight = false;

var isIE6 = false;
if (navigator.appVersion.indexOf('MSIE 6') > 0) isIE6 = true;
if (navigator.appName == 'Microsoft Internet Explorer' && navigator.platform == 'Win32') {
	pasteCompatible = true;
} else {
	pasteCompatible = false;
}
function nothing() { }
function setBg(curObj, newState) {
	if (!TableRollovers) return;
	onBg = document.styleSheets[0].rules[6].style.backgroundColor;
	offBg = document.styleSheets[0].rules[11].style.backgroundColor;
	switch(newState) {
		case 'over':
			curObj.style.backgroundColor = onBg;
			break;
		case 'out':
			if (curObj != LastSelected) 
			curObj.style.backgroundColor= offBg;
			break;
		case 'click':
			curObj.style.backgroundColor= onBg;
			if (LastSelected) LastSelected.style.backgroundColor=offBg;
			LastSelected = curObj;
			break;
	}
}
function deleteItem(curId, curTitle, curFm) {
	if (confirm('Are you sure you want to delete ' + curTitle + '?')) {
		curFm.C_DeleteId.value = curId;
		curFm.C_Action.value = 'Delete';
		if (typeof checkTitle != 'undefined') checkTitle(curFm.Title);
		curFm.submit();
	}
}
// used by shopping and registration modules (sometimes)
function popupWindow(pageName) 
{
    tmpWindow = window.open(pageName, 'itemEditor', 'scrollbars=yes,toolbar=no,status=no, menubar=no,directories=no,location=no,resizable=no,width=500,height=480,top=100,left=200');
    tmpWindow.parentFrame = 'qwerty';
    tmpWindow.focus();	
}
// used by content, contact, form modules (always)
function openDetail(curId, curPage, parentId) {
	var popupAttributes = '';
	curPath = curPage;
	if (curPage.indexOf('form_') != -1) {
		curPath += '?ParentId=' + parentId;
	} else {
		curPath += '?';
	}
	if (curId) {
		//edit an existing item
		curQuery = '&Id=' + curId;
		
		//if (curId.toString().indexOf('g') >= 0) curQuery += '&C_Action=New';
		
	} else {
		//add a new item
		curQuery = '&C_Action=New';
	}

	popupAttributes += PopupWidth ? ',width='+PopupWidth : ',width=500';	
	popupAttributes += PopupHeight ? ',width='+PopupHeight : ',height=460';	
	if(ResizePopups) popupAttributes += ',resizable=yes, status=yes';
		
	editWindow = window.open(curPath + curQuery , 'EditWindow','scrollbars=yes'+popupAttributes);
	editWindow.focus();
	editWindow.parentList = document;
}
function deleteOrphans(numOrphans, parentName) {
	if (confirm('Are you sure you want to delete ' + numOrphans + ' unassigned <?=$MainSection?>(s)?')) {
	document.location = ('form_view.php?CurAction=DeleteOrphans&Parent=' + parentName);
	}
}
function validateForm(requirements, curFm) {
	isValid = true;
    for (var i = 0; i<curFm.elements.length; i++) {
    	curField = curFm.elements[i];
        elName = curFm.elements[i].name;
		curValue = curFm.elements[i].value;	        
		fieldType = curFm.elements[i].type;
        for (var required in requirements) {
        	if(elName==requirements[required] || elName == requirements[required]+"[]"){
				switch (fieldType) {
					case 'text':
					case 'textarea':
					case 'password':
					case 'file':
						if (curValue == '') {
							isValid = false; 
							focusField = curField;					
						}
						break;
					case 'select-one':
					case 'select-multiple':
						if (curField.selectedIndex  && curField.options[curField.selectedIndex].value != '') {
							isValid = true;
						} else {
							isValid = false;
							focusField = curField;
						}
						break;
					case 'radio':
					case 'checkbox':
						numSelected = 0;
						isValid = false;
						sameField = true;
						while(sameField) {				
							if (curFm.elements[i].checked) {
								numSelected++;
								isValid = true;
							}
							if (((i+1)<curFm.elements.length) && curFm.elements[i+1].name.indexOf(requirements[required]) != -1) {
								sameField = true;
								i++;
							} else {
								sameField = false;
								if(!numSelected){
									focusField = 'unsupportedType';
								}
							}										
						}
						break;
				}
        	}
        	if(isValid == false) break; // if a missing requirement already found, stop checking
        }
        if(isValid == false) break; // if a missing requirement already found, stop checking
    }		
	if (!isValid) {
		if(focusField == 'unsupportedType'){
			window.alert('You have left unselected a required field.');		
			return false;
		} else {
			window.alert('You have left out a required piece of information. Your cursor will now move to the required field. Please fill it in and submit the form again.');
			focusField.focus();
			return false;
		}
	}			
	return true;
}
function showAll(curFm) {
	//clears all text fields and submits the current form
	numElements = curFm.elements.length;
	for (i = 0; i < numElements; i++) {
		curField = curFm.elements[i];
		if (curField.type == 'text') curField.value = '';
	}
	curFm.submit();
}
function openHelp() {
	helpWindow = window.open('help.htm','HelpWindow','width=300,height=400');
}
function updateNavFrame() {
	// not required anymore, shortcuts do not autorefresh
}
function updateList() {
	if (window.opener.document.location.toString().indexOf('Content/list_view.php') >= 0) {
		window.opener.checkTitle(window.opener.document.fm.Title);
	}
	if (window.opener.document.location.toString().indexOf('control.php') >= 0) {
		window.opener.parent.List.location.reload(false);
	} else {	
		window.opener.location.reload(false);
	}
	window.close();
}

function finishWizard() {
	document.location = '../../../../adcustom/help/wizard/finish.php';
}

function refreshForm(curForm) {
	curForm.C_UpdateList.value = 'no';
	curForm.submit();
}

function openEditor(previewPath) {
	editorUrl = '../../../content_module/design/editor/texttools.php?PreviewPath=' + previewPath;
	if (pasteCompatible) {
		if (document.selection.createRange().text.length > 0) {
			textWindow = window.open(editorUrl, 'textWindow', 'width=350,height=110');
		} else {
			window.alert('Select some text first, then click QuickHTML again.');
		}
	} else {
		textWindow = window.open(editorUrl, 'textWindow', 'width=350,height=110');
	}
}
