
// 21 Sep 2004
// Change to the CheckForm function, Pass password values instead of form

// 10 Mar 2005
// Change to the HighlightRequired and HighlightInvalid functions, Remove browser type check

// 9 Aug 2005
// Updated Multiple Checkbox Prepopulation

// 4 Dec 2007
// Change to date checking to check for invlaid dates ie 31 Feb

// 14 Aug 2008
// hidden fields are no longer validated

// 15 Aug 2008
// blank date fields are now valid unless set to compulsary

// 22 Aug 2008
// Strip commas and currency symbols from number fields

// 13 Nov 2008
// Made it highlight every field, even if they have the same attribute name

// 9 Feb 2009
// Added firefox fix to HighlightRequired function

// 16 Sep 2009
// Added check that the date year field is >= 1753 to correct database error

// 29 Sep 2009
// Removed compulsary check for fields where type is hidden

var	blnPopErrorMessage = 0
function CheckForm(frm)
{
	
	blnPopErrorMessage = 0
	var strErrorMessage = "Please complete all required fields!" + '\n'
	var strPreviousAttributeName = ""
	//Loop through form fields
	for (loop=0; loop < frm.length; loop++) {
		var field = frm[loop]
		var strFormInputName = field.name;
		
		if( field.type == "hidden" ) continue;
		
		var hidden = false;
		var obj = field; 
		do {
			if( obj.style ) {
				if( obj.style.display.toLowerCase() == "none" ) {
					hidden = true;
					break;
				}
			}
		}
		while (obj = obj.parentNode);
		if( hidden == true ) continue;
		
		if (strFormInputName)
		{

			var strFormInputPrefix = strFormInputName.slice(0,3);
			var strFormInputType = field.type
			var strAttributeName = strFormInputName.substring(3,strFormInputName.length).toLowerCase();
	//		alert(strFormInputPrefix)		
			var blnRequired = false
			
			if( strFormInputPrefix.toLowerCase() == "row" )
			{
				var strFormInputNameTmp = strFormInputName.substring( strFormInputName.indexOf("_")+1,strFormInputName.length)
				strFormInputPrefix = strFormInputNameTmp.slice(0,3);
				strAttributeName = strFormInputNameTmp.substring(3,strFormInputNameTmp.length).toLowerCase();
			}
			
			// check for valid form field starts
			
			if (strFormInputPrefix=='str' ||
				strFormInputPrefix=='txt' ||
				strFormInputPrefix=='dat' ||
				strFormInputPrefix=='int')
			{
				blnRequired = false
	// #### Check Empty Data Integrity ####
	
				if (strFormInputPrefix == 'int') {
					var varFormValue
					frm[loop].value = frm[loop].value.replace( /[$,ĄŁ€%]+/g, "" )
					varFormValue = frm[loop].value
					if(varFormValue.length > 0) {
						if(isNaN(varFormValue) == true) {
							strErrorMessage += "Invalid number entered in a numeric field" + '\n'
							blnRequired = true
						}
					}
				}
				
/*				if (strFormInputPrefix == 'dat') {
					var varFormValue
					varFormValue = frm[loop].value
					if(varFormValue.length > 0) {
						if(strFormInputName.lastIndexOf("_Day") < -1 && strFormInputName.lastIndexOf("_Month") < -1 && strFormInputName.lastIndexOf("_Year") < -1)
						{
							if(varFormValue.length < 5) {
								strErrorMessage += "Invalid date entered in a date field" + '\n'
								blnRequired = true
							}
						}  else {
							if(varFormValue.length > 0) {
								if(isNaN(varFormValue) == true && strFormInputName.lastIndexOf("/") < -1) {
									strErrorMessage += "Invalid date entered in a date field" + '\n'
									blnRequired = true
								}
							}
						}
					}
				}
*/
				if (strFormInputPrefix == 'dat') {
					var varFormValue
					varFormValue = frm[loop].value
								
					if( strFormInputName.lastIndexOf("_Day") > 0 || strFormInputName.lastIndexOf("_Month") > 0 || strFormInputName.lastIndexOf("_Year") > 0 )
					{	
						var strFormInputNameStripped = strFormInputName.substring(0,strFormInputName.lastIndexOf("_"));
						
						var objYear = frm.elements[strFormInputNameStripped + '_Year'];
						var objMonth = frm.elements[strFormInputNameStripped + '_Month'];
						var objDay = frm.elements[strFormInputNameStripped + '_Day'];
						
						if( objYear && objMonth && objYear )
						{
							var intYear = objYear.value
							var intMonth = objMonth.value
							var intDay = objDay.value
							
							var blnCheckDate = true;
							if( intYear == "" && intMonth == "" && intDay == "" )
							{
								blnCheckDate = false;
								for( index=0; index < formNames.length; index++ )
								{
									if( strFormInputNameStripped.toLowerCase() == "dat" + formNames[index].toLowerCase() )
									{
										blnCheckDate = true;
										break;
									}
								}
							}
							
							if( blnCheckDate )
							{
								intYear = parseInt(objYear.value)
								intMonth = parseInt(objMonth.value)-1
								intDay = parseInt(objDay.value)
								
								if( !isNaN(intYear) && !isNaN(intMonth) && !isNaN(intDay) )
								{
									var datCheck = new Date()
									datCheck.setFullYear(intYear,intMonth,intDay)
	
									if( isNaN( datCheck ) || datCheck.getFullYear() != intYear || datCheck.getMonth() != intMonth || datCheck.getDate() != intDay || intYear < 1753 ) {
										if( strFormInputName.lastIndexOf("_Year") > 0 )
										{
											strErrorMessage += "Invalid date entered in a date field" + '\n'
										}
										blnRequired = true
									}
								}
								else
								{
									if( strFormInputName.lastIndexOf("_Year") > 0 )
									{
										strErrorMessage += "Invalid date entered in a date field" + '\n'
									}
									blnRequired = true
								}
							}
						}
						
					}
				}
				
				//Loop through Required formNames array
	
				for (index=0; index < formNames.length; index++) {
					if (strAttributeName == formNames[index].toLowerCase()) {
				
	// ####  Check For Empty Required Fields ####
	
						if ((strFormInputType == 'text') || (strFormInputType == 'textarea') || (strFormInputType == 'hidden') || (strFormInputType == 'select-one') || (strFormInputType == 'password')) {
							if (eval("frm." + strFormInputName).value == "")
							{
								blnRequired = true						
							}
						}
	
						// Check box
						if (strFormInputType == 'checkbox') {
							var fieldCheckBox = eval("frm." + field.name)
							if(CheckBlankCheckBoxField(fieldCheckBox)) {
								blnRequired = true
							}

//							if (field.checked == false) {
//								blnRequired = true
//							}	
							}

						//Radio buttons
						if (strFormInputType == 'radio') {
							var fieldRadio = eval("frm." + field.name)
							if(CheckBlankRadioField(fieldRadio)) {
								blnRequired = true
							}
						}

	// ####  Check Special Email Types ####
						if (formValues[index] == 'email')
						{
							var strOriginalErrorMessage
							strOriginalErrorMessage = strErrorMessage
							strErrorMessage += CheckEmail(frm[loop].value)
							if(strOriginalErrorMessage != strErrorMessage) {
								strErrorMessage += '\n'
								blnRequired = true
							}
						}
	//						alert(frm[loop].value)
						// ### Email Type Ends					
	
	// ####  Check Special Credit Card Details ####
						if (formValues[index] == 'credit card number')
						{
							var strOriginalErrorMessage
							strOriginalErrorMessage = strErrorMessage
							strErrorMessage += CheckCardNumber(frm)
							if(strOriginalErrorMessage != strErrorMessage) {
								strErrorMessage += '\n'
								blnRequired = true
	
							}
						}
	
						if (formValues[index] == 'credit card expiry year')
						{
							var strOriginalErrorMessage
							strOriginalErrorMessage = strErrorMessage
							strErrorMessage += CheckExpiryYear(frm[loop])
							if(strOriginalErrorMessage != strErrorMessage) {
								strErrorMessage += '\n'
								blnRequired = true
							}
						}
	
	
						if (formValues[index] == 'credit card expiry month')
						{
							var strOriginalErrorMessage
							strOriginalErrorMessage = strErrorMessage
							strErrorMessage += CheckExpiryMonth(frm[loop])
							if(strOriginalErrorMessage != strErrorMessage) {
								strErrorMessage += '\n'
								blnRequired = true
							}
						}
	
	
						if (formValues[index] == 'password')
						{
							var fieldPasswordConfirm
							fieldPasswordConfirm = frm.PasswordConfirm
	
							var strOriginalErrorMessage
							strOriginalErrorMessage = strErrorMessage
							strErrorMessage += CheckPassword(field,fieldPasswordConfirm)
							if(strOriginalErrorMessage != strErrorMessage) {
								strErrorMessage += '\n'
								blnRequired = true
								HighlightInvalid(fieldPasswordConfirm, true)
							}
						}
						
	// ### Credit Card Ends
						
					}
				}
				//if (strPreviousAttributeName != strAttributeName) {
					HighlightRequired(field, blnRequired)
				//}
				strPreviousAttributeName = strAttributeName
			}
		// check for valid form field ends
		}
	} 
	
	if (blnPopErrorMessage==1)
	{
		alert(strErrorMessage)
		return false
	} else {
		return true
	}
}


function CheckPassword(field,fieldPasswordConfirm)
{
	if (field.value == '')
	{
		return " "
	} else {
		if (field.value.length < 4)
		{
			return "Password needs to be 4 or more characters."
		} else {

			if (field.value.lastIndexOf(" ") > -1)
			{
				return "Passwords must not have any gaps"
			} else {
				if (field.value != fieldPasswordConfirm.value)
				{
					return "Passwords do not match."
				} else {
					return ""
				}
			}
		}
	}
}


function CheckBlankRadioField(field) {

	var i

	if (field.length != undefined)
	{
		var intFieldLength = field.length
	} else {
		var intFieldLength = 0

		if(field.checked == true)
		{
			for (i=0; i < field.length; i++) {
				HighlightRequired(field[i], false)
			}
			return false
		}
	}


	for (i=0; i < intFieldLength; i++) {
		
		if(field[i].checked == true)
		{
			for (i=0; i < field.length; i++) {
				HighlightRequired(field[i], false)
			}
			return false
		}
	}

	for (i=0; i < field.length; i++) {
		HighlightRequired(field[i], true)
	}

	return true
}

function ProcessCheckBoxes(frm)

	{	
		var strAction = ''
     	for (i = 0; i < frm.length; i++)
     	{
         	if (frm(i).type == 'checkbox')
        	{	
				
				if (frm(i).checked == false)
				{
					strAction = strAction + "&" + frm(i).name + "=0"
//					frm(i).value = '0';
//					frm(i).checked = true;
//				} else {
//					frm(i).value = '1';
				}
         	}
     	}
		frm.action = frm.action + strAction

		return true
	}

function CheckBlankCheckBoxField(field) {
	var i

	if (field.length != undefined)
	{
		var intFieldLength = field.length
	} else {
		var intFieldLength = 0
		if(field.checked == true)
		{
			for (i=0; i < field.length; i++) {
				HighlightRequired(field[i], false)
			}
			return false
		}
	}


	for (i=0; i < intFieldLength; i++) {
		
		if(field[i].checked == true)
		{
			for (i=0; i < field.length; i++) {
				HighlightRequired(field[i], false)
			}
			return false
		}
	}

	for (i=0; i < field.length; i++) {
		HighlightRequired(field[i], true)
	}

	return true
}

function HighlightInvalid(field, blnHighlight)
{
	HighlightRequired(field, blnHighlight)
}

function HighlightRequired(field, blnHighlight)
{
	var fieldInput = field
	
	if (blnHighlight == true)
	{
		// Firefox fix for radio and checkboxes
		if( ( navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1 ) && ( fieldInput.type == "checkbox" || fieldInput.type == "radio" ) ) {
			var parent = fieldInput.parentNode;
			if( parent.id != "InvalidInput" ) {
				var objDiv = document.createElement('span');
				objDiv.style.background = "#FFCCCC";
				objDiv.style.display = "inline";
				objDiv.style.padding = "2px 0px";
				objDiv.id = "InvalidInput";
				var parent = fieldInput.parentNode;
				parent.insertBefore( objDiv, fieldInput );
				parent.removeChild( fieldInput );
				objDiv.appendChild( fieldInput );
			}
		}
		fieldInput.style.background='#FFCCCC';
		blnPopErrorMessage = 1
	} 
	else
	{
		// Firefox fix for radio and checkboxes
		if( ( navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1 ) && ( fieldInput.type == "checkbox" || fieldInput.type == "radio" ) ) {
			var parentSpan = fieldInput.parentNode;
			if( parentSpan.id == "InvalidInput" ) {
				var parent = parentSpan.parentNode;
				parent.insertBefore( fieldInput, parentSpan );
				parent.removeChild( parentSpan );
			}
		}
		fieldInput.style.background='#EFEBDE';
	}
}


function CheckEmail(strEmailAddress)
{
  if (strEmailAddress == '') 
   	{
		return ""
	} else {
		if (strEmailAddress.length < 5 ||
		strEmailAddress.lastIndexOf("@") < 1)
		{
			return "Please make sure your email address is correct."
		} else {
			if (strEmailAddress.lastIndexOf(" ") > -1)
			{
				return "Please make sure there are no spaces in or either side of your email address."
			} else {
				if (strEmailAddress.lastIndexOf(".") < 1)
				{
					return "Please make sure your email address is correct."
				} else {
					return ""
				}
			}
		}
	}

}


function SuggestEmail(frm)
{
	var strEmailAddress = frm.strEmailAddress.value

	if(strEmailAddress.lastIndexOf(".conz") > -1)
	alert ("Suggest changing .conz to .co.nz")

	if(strEmailAddress.lastIndexOf(",") > -1)
	alert ("Suggest changing comma to a full stop")
	return true;

}


function CheckPasswordxx(strPassword, strPasswordConfirm)
{	
 
  if (strPassword =='' || 
	strPasswordConfirm =='' ) 
		{
			alert ("Please complete your password.");
			return false;
		} else {
			if (strPassword == strPasswordConfirm)
			{
				if (strPassword.length < 4 ||
				    strPassword.length > 8)
				{
					alert ("Passwords must be between 4 to 8 characters");
					return false;
				} else {

					if (strPassword.lastIndexOf(" ") > -1)
					{
						alert ("Passwords must not have any gaps");
						return false;
					} else {
						return true;				
					}				

				}				

			} else {
				alert ("Passwords do not match.");
				return false;
			}
		}
}

function PopupWindow(url, intWidth, intHeight)

	{
		if(navigator.appName == "Microsoft Internet Explorer")
			{
			remote = window.open(url,"remote","width=" + intWidth + " ,height=" + intHeight + ",resizable=yes,left=200,top=20,scrollbars=yes");
			}
		else
			{
			remote = window.open(url,"remote","width=" + intWidth + ",height=" + intHeight + ",resizable=yes,screenX=200,screenY=20,scrollbars=yes");
			}
		if(!(parseFloat(navigator.appVersion) < 3.0 && navigator.appName == "Microsoft Internet Explorer"))
			{
			remote.focus();
			}
	}



function ConfirmDelete(url)
	{
		if(confirm("Confirm Delete"))
		{
			location.href = url
		}
	}


function UpdateLink(msgLink)
	{
		if(confirm("Are you sure you want to delete this webpage"))
		{
			msgLink.href = msgLink.href + '&blnConfirmation=1'
		}
	}

function SubmitForm(frm)
		{
		frm.submit();
		}
		
function ResetForm(frm)
		{
		frm.reset();
		}
		

function PrepopulateForm(frmPopNames,frmPopValues)
{
	//Loop through form fields
//	var frmPop = document.forms[0]
	if (document.forms.form1)
	{
		var frmPop = document.forms.form1
		PrepopulateForm2(frmPopNames, frmPopValues, frmPop)
	}
}


// 29 Sep 2009
// Fixed select field unable to populate value containing a single quote

function PrepopulateForm2(frmPopNames, frmPopValues, frmPop)
{
	//Loop through form fields
	for (loop=0; loop < frmPop.length; loop++) {
		var field = frmPop[loop]
		if (field.name)
		{
			var fieldName = field.name;
			var fieldPrefix = fieldName.slice(0,3);
			var fieldType = field.type

			var attrName = fieldName.substring(3,fieldName.length).toLowerCase();

			if (fieldName.slice(0,2)=="id")
			{
				var attrName = fieldName.toLowerCase();
			}

			//Loop through frmPopNames array
			for (index=0; index < frmPopNames.length; index++) {
				if (attrName == frmPopNames[index].toLowerCase()) {

					// One-dimensional text/string form field
					if ((fieldType == 'text') || (fieldType == 'textarea') || (fieldType == 'hidden')) {
						eval("frmPop." + fieldName + ".value = '" + frmPopValues[index] + "'");
					}

					// Check Box
					if (fieldType == 'checkbox') {
						var chkItems = eval("frmPop." + field.name)

						if( chkItems.length > 0 && frmPopValues[index].indexOf(", ") > -1 ) {
//								if( frmPopValues[index].indexOf(", " + field.value) > -1 || frmPopValues[index].indexOf(field.value + ",") > -1 ) {
							var strFormValue = frmPopValues[index]
							strFormValue = ", " + strFormValue + ","
							if( strFormValue.toLowerCase().indexOf(", " + field.value.toLowerCase() + ",") > -1) {
								field.checked = true;
							}
						}
						else
						{
							if (field.value.toLowerCase() == frmPopValues[index].toLowerCase()) {
								field.checked = true;
							}
						}	
					}
					// List field
					if (fieldType == 'select-one') {
						for (selectIndex=0; selectIndex < field.length; selectIndex++) {
							if (field[selectIndex].value == frmPopValues[index].replace( /\\'/g, "'" ).replace( /\\"/g, "\"" )) {
								field[selectIndex].selected = true;
					}	}	}

					//Radio buttons
					if (fieldType == 'radio') {
						if (field.value == frmPopValues[index]) {
							frmPop[loop].checked = true;
						}
					}
				}
			}
		}
	}
}


function EditHTMLContent(idWebSite,strUserName,idWebPage,idLayout,strImageURL,strDestinationField) {
	var intLeftPos = 0;
	var intTopPos = 0;
	var intWidth = 670;
	var intHeight = 500;

	if (screen) {
		intWidth = screen.width * 0.7;
		intHeight = screen.height * 0.7;
		intLeftPos = (screen.width / 2) - (intWidth/2);
		intTopPos = (screen.height / 2) - (intHeight/2) - (screen.height / 10);
		
		if( intTopPos < 0 ) intTopPos = 0;
	}

	var strImageLibraryURL = '/console/fckeditor/html_editor_apps.asp?idWebSite=' + idWebSite + '&strUserName=' + strUserName + '&strImageURL=' + strImageURL + '&strDestinationField=' + strDestinationField + '&idWebPage=' + idWebPage + '&idLayout=' + idLayout
	window.open(strImageLibraryURL, '', 'height=' + intHeight + ',width=' + intWidth + ',top=' + intTopPos + ',left=' + intLeftPos + ',resizable=yes');
}
	
function ImagesLibrary(idWebSite,strUserName,idWebPage,strDestinationURL,strDestinationField,strReference) {
	var strImageLibraryURL = '/images_library.asp?idWebSite=' + idWebSite + '&strUserName=' + strUserName + '&strDestinationURL=' + strDestinationURL + '&strDestinationField=' + strDestinationField + '&idWebPage=' + idWebPage + '&strReference=' + strReference
	window.open(strImageLibraryURL, '', 'height=600,width=620,top=30,left=30,resizable=yes');
}


function SingleImageUpload(idWebSite,strUserName,idWebPage,strDestinationField,strReference,strTitle,strAction,strStartExtention,strEndExtention,strDescription) {
	var frm = document.form1

	var strFileName = eval("frm." + strDestinationField).value

	var strImageLibraryURL = '/image_upload.asp?idWebSite=' + idWebSite + '&strUserName=' + strUserName + '&FLE=' + strFileName + '&DFLD=' + strDestinationField + '&idWebPage=' + idWebPage + '&REF=' + strReference + '&TIT=' + strTitle + '&ACT=' + strAction + '&ST=' + strStartExtention + '&ED=' + strEndExtention
	
	window.open(strImageLibraryURL, '', 'resizable=yes,height=300,width=350,top=200,left=250');
}


function SingleImageUploadTemplate(idWebSite,strUserName,idWebPage,strDestinationField,strReference,strTitle,strAction,strTemplateCode,strDescription) {

	var frm = document.form1

	var strFileName = eval("frm." + strDestinationField).value

	var strImageLibraryURL = '/image_upload_template.asp?idWebSite=' + idWebSite + '&strUserName=' + strUserName + '&FLE=' + strFileName + '&DFLD=' + strDestinationField + '&idWebPage=' + idWebPage + '&REF=' + strReference + '&TIT=' + strTitle + '&ACT=' + strAction + '&TPLC=' + strTemplateCode
	
	window.open(strImageLibraryURL, '', 'resizable=yes,height=300,width=350,top=200,left=250');
}

function ValidateSearchWords(){

	var searchStr = document.searchForm.strSearchKeyWords.value;

	searchStr = searchStr.replace(/[^\w\s]/g, '');
	searchStr = searchStr.replace(/^\s*/, '').replace(/\s*$/, '');
	document.searchForm.strSearchKeyWords.value=searchStr;

	if(searchStr==""){
		alert('Please enter a search word');
		document.searchForm.strSearchKeyWords.value="";
		return false;
	}
	else if(searchStr.length < 4) {
		alert('Please enter a search word longer than 3 characters');
		return false;
	}
	else{
		return true;
	}
}
	

function DeleteRow(url)
{
	if( confirm("Are you sure you want to delete?") )
	{
		window.location = url
	}
	return
}
