/*
-------------------------------------------------------- 
OpenPopup [v1.0]
-------------------------------------------------------- 
*/
function OpenPopup(sUrl, iWidth, iHeight) {
	var oPopup = window.open(sUrl, 'popup', 'directories=no,height='+iHeight+',width='+iWidth+',location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no,top=10');
	oPopup.focus();
	return false;
}

/*
-------------------------------------------------------- 
popupWindow [v1.3]
john's optional modal popup window code
-------------------------------------------------------- 
*/
function popupWindow(sUrl, iWidth, iHeight) {
	if (arguments.length>=4) {
		// zero based array so 3 not 4!
		bModal = arguments[3];
	}
	else if (sUrl.indexOf('google')>0) {
		// google maps doesn't like modal on IE
		bModal = false;
	}
	else {
		// default to modal...
		bModal = true;		
	}

	if (window.showModalDialog && bModal) {
		iWidth = iWidth + 8;
		iHeight = iHeight + 48;
		sWindowFeatures = "dialogTop:10px;dialogLeft:10px;dialogWidth:" + iWidth + "px;dialogHeight:" + iHeight + "px";
		oWindowReference = window.showModalDialog(sUrl, window, sWindowFeatures);
	}
	else {
		if (document.all) {
			iWidth = iWidth + 40;
			iHeight = iHeight + 20;
		}
		else {
			iWidth = iWidth + 20;
			iHeight = iHeight + 20;
		}
		sWindowName = 'popupwindow';
		sWindowFeatures = "left=10,top=10,height=" + iHeight + ",width=" + iWidth + ",location=no,menubar=no,directories=no,toolbar=no,status=yes,titlebar =yes,resizable=yes,scrollbars=yes";
		if (bModal) {
			sWindowFeatures = sWindowFeatures + "dependent=yes.modal=yes,dialog=yes,minimizable=no,alwaysRaised=yes";
		}
		if (window.oWindowReference) {
			try {
				oWindowReference.close();
			}
			catch(e) {
				// handle silently...	
			}
		}
		oWindowReference = window.open(sUrl, sWindowName, sWindowFeatures);
	}
	return false;
}

/*
-------------------------------------------------------- 
-------------------------------------------------------- 
Form validation
-------------------------------------------------------- 
-------------------------------------------------------- 
*/

/*
-------------------------------------------------------- 
RequiredFields [v2.2]
-------------------------------------------------------- 
*/
function RequiredFields(oForm) {
	var bCompleted = true;
	var bIsEmailAddress = true;
	var oField;
	var oEmailRegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	var bChecked = false;
	var bRequired = false;
	
	// loop through all form fields...
	for (var i=0; i<oForm.elements.length; i++) {
		if (oForm.elements[i].id) {
			// DOM 2
			oField = document.getElementById(oForm.elements[i].id);
		}
		else {
			// DOM 1
			oField = oForm.elements[i];
		}
		
		if (oField!=undefined && oField!=null && oField.id.length) {		
			// check if the field is required...
			bRequired = Array.find(aRequired, oField.name)!=-1;
			
			if (bRequired) {
				// required field...
				if (oField.type=='text'||oField.type=='file'||oField.type=='textarea'||oField.type=='password'||oField.type=='hidden') {
					if (oField.value.length==0) {
						bCompleted = false;
						highlight(oField);
					}
					else if (oField.name=='email') {
						if (!oEmailRegExp.test(oField.value)) {
							bIsEmailAddress = false;
							highlight(oField);
						}
					}
				}
				else if (oField.type=='radio') {
					bChecked = false;
					var aRadioField = oForm[oField.name];
					for (j=0;j<aRadioField.length;j++) {
						if (aRadioField[j].checked) {
							bChecked = true;
						}
					}
					if (!bChecked) {
						highlight(oField);
						bCompleted = false;
					}
				}
				else if (oField.type=='select'||oField.type=='select-one') {
					if (oField.options[oField.selectedIndex].value.length==0) {
						bCompleted = false;
						highlight(oField);
					}
				}
				else if (oField.type=='checkbox') {
					if (!oField.checked) {
						bCompleted = false;
						highlight(oField);
					}
				}
				else {
					bChecked = false;
					for (j=0;j<oField.length;j++) {
						if (oField[j].checked) {
							bChecked = true;
						}
					}
					if (!bChecked) {
						highlight(oField);
						bCompleted = false;
					}
				}
			}
			if (bCompleted) {
//			else {
				// check that value is not the default value...	
				if (oField.type=='text'||oField.type=='textarea'||oField.type=='password'||oField.type=='hidden') {
					if (oField.title&&oField.title.length) {
						if (oField.title.toLowerCase()==oField.value.toLowerCase()) {
							highlight(oField);
							bCompleted = false;
						}
					}
					else if (oField.alt&&oField.alt.length) {
						if (oField.alt.toLowerCase()==oField.value.toLowerCase()) {
							highlight(oField);
							bCompleted = false;
						}
					}
				}
			}
		}
			
		if (!bCompleted) {
			if (bRequired) {
				if (oField.title&&oField.title.length) {
					alert('Please enter ' + oField.title);
				}
				else if (oField.alt&&oField.alt.length) {
					alert('Please enter ' + oField.alt);
				}
				else {
					alert('Please complete all required fields');
				}
			}
			else {
				if (oField.title&&oField.title.length) {
					alert('Please enter ' + oField.title + ' or leave blank');
				}
				else if (oField.alt&&oField.alt.length) {
					alert('Please enter ' + oField.alt + ' or leave blank');
				}
				else {
					alert('Please set blank any non essential information you prefer not to provide');
				}
			}
			break;
		}
	} // end loop
	

	if (bCompleted&&!bIsEmailAddress) {
		alert('Please check your email address');
	}
	var bResult = bCompleted && bIsEmailAddress;

	if (bResult) {
		// loop through fields and find submit buttons then disable....
		// we have an issue where a disabled field is not passed back in the form so need to check that only one submit button...
		var aSubmitButtons = new Array();
		for (i=0;i<oForm.elements.length;i++) {
			if (oForm.elements[i].type=='submit') {
				aSubmitButtons[aSubmitButtons.length] = oForm.elements[i];
;
			}
		}
		if (aSubmitButtons.length==1) {
			// only one submit button so OK to disable...
			aSubmitButtons[0].style.cursor = 'wait';
			aSubmitButtons[0].disabled = true;	
			aSubmitButtons[0].value = "sending";
		}
		else {
			// more than one submit button so disable each psuedo style...
			for (i=0;i<aSubmitButtons.length;i++) {
				aSubmitButtons[i].style.color = "#5F5F5F";
				aSubmitButtons[i].style.backgroundColor = "#F4F4F4";
				aSubmitButtons[i].style.cursor = 'wait';
				aSubmitButtons[i].value = "sending";
			}
		}
	}
	return bResult;
}


/*
-------------------------------------------------------- 
highlight [v1.3]
john's highlight form elements
-------------------------------------------------------- 
*/
function highlight(oField) {
	// pick mode - highlight table cell (true) or form field (false)...
	var bHighlightTd = true;
	var sStyleParentBorder = '';
	var sStyleParentBackgroundColor = '';
	var sStyleFieldBorder = '1px solid #585EAA';
	var sStyleFieldBackgroundColor = '#FFFFFF';
	
	if (oField.type=='select'||oField.type=='select-one'||oField.type=='text'||oField.type=='textarea'||oField.type=='password') {
		if (bHighlightTd && oField.id) {
			if (sStyleParentBackgroundColor.length) {
				document.getElementById(oField.id).parentNode.style.backgroundColor = sStyleParentBackgroundColor;
			}
			if (sStyleParentBorder.length) {
				document.getElementById(oField.id).parentNode.style.border = sStyleParentBorder;
			}
			if (sStyleFieldBackgroundColor.length) {
				document.getElementById(oField.id).style.backgroundColor = sStyleFieldBackgroundColor;
			}
			if (sStyleFieldBorder.length) {
				document.getElementById(oField.id).style.border = sStyleFieldBorder;
			}
		}
		else {
			if (sStyleFieldBackgroundColor.length) {
				oField.style.backgroundColor = sStyleFieldBackgroundColor;
			}
			if (sStyleFieldBorder.length) {
				oField.style.border = sStyleFieldBorder;
			}
		}
		// set focus...
		oField.focus();
	}
	else {
//		oField.parentNode.style.backgroundColor = 'lightyellow';
//		oField.parentNode.style.border = '1px solid red';
		// radio...
		for (var i=0;i<oField.length;i++) {

			if (bHighlightTd && oField[i].id) {
				if (sStyleParentBackgroundColor.length) {
					document.getElementById(oField[i].id).parentNode.style.backgroundColor = sStyleParentBackgroundColor;
				}
				if (sStyleParentBorder.length) {
					document.getElementById(oField[i].id).parentNode.style.border = sStyleParentBorder;
				}
				if (sStyleFieldBackgroundColor.length) {
					oField[i].style.backgroundColor = sStyleFieldBackgroundColor;
				}
				if (sStyleFieldBorder.length) {
					oField[i].style.border = sStyleFieldBorder;
				}
			}
			else {
				if (sStyleFieldBackgroundColor.length) {
					oField[i].style.backgroundColor = sStyleFieldBackgroundColor;
				}
				if (sStyleFieldBorder.length) {
					oField[i].style.border = sStyleFieldBorder;
				}
			}
		}
	}
//	alert(oField.name);
}

/*
-------------------------------------------------------- 
extend Array with Find Method [v1.0]
-------------------------------------------------------- 
*/
Array.find = function(ary, element){
	for(var i=0; i<ary.length; i++){
		if(ary[i] == element){
			return i;
		}
	}
	return -1;
}


/*
--------------------------------------------------------
add getElementById functionality to IE4
-------------------------------------------------------- 
*/
if (!document.getElementById && document.all) { 
	document.getElementById = new Function('id', 'return document.all[id]') 
}

/*
-------------------------------------------------------- 
-------------------------------------------------------- 
cookie handlers
-------------------------------------------------------- 
-------------------------------------------------------- 
*/


/*
-------------------------------------------------------- 
setCookie [v1.0]
-------------------------------------------------------- 
*/
function setCookie(sKey, sValue) {
	var futdate = new Date()		//Get the current time and date
	var expdate = futdate.getTime()  //Get the milliseconds since Jan 1, 1970
	expdate += 3600*1000  //expires in 1 hour(milliseconds)
	futdate.setTime(expdate)
	var newCookie = sKey + "=" + escape(sValue) + "; path=/;"	//Set the new cookie values up
//	newCookie += " expires=" + futdate.toGMTString()
	window.document.cookie=newCookie //Write the cookie
}

/*
-------------------------------------------------------- 
getCookie [v1.0]
-------------------------------------------------------- 
*/
function getCookie(sKey) {
	var oCookie = document.cookie;
	var sKey = sKey + "=";
	var iBegin = oCookie.indexOf("; " + sKey);
	if (iBegin == -1) {
		iBegin = oCookie.indexOf(sKey);
		if (iBegin != 0) {
			return null;
		}
	} 
	else {
		iBegin = iBegin + 2;
	}
	var iEnd = oCookie.indexOf(";", iBegin);
	if (iEnd == -1) {
		iEnd = oCookie.length;
	}
	return unescape(oCookie.substring(iBegin + sKey.length, iEnd));
}

/*
-------------------------------------------------------- 
clearCookie [v1.0]
-------------------------------------------------------- 
*/
function clearCookie(sKey) {
	setCookie(sKey, '');
	alert('Information has been deleted');
	return false;
}

/*
-------------------------------------------------------- 
acceptsCookie [v1.0]
-------------------------------------------------------- 
*/
function acceptsCookie() {
	sCookieKey = 'cookiesenabled';
	setCookie(sCookieKey, true);
	var result = getCookie(sCookieKey);
	if (result==null||!result.length) {
		return false;
	}
	else {
		return true;
	}
}






