/* Used for onload code on Email Updates as onload code in the domino form and also set in the add load event does not work together */
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
// Set the expires date for any cookies stored on the user's PC
var expiration = new Date( 2099, 11, 31, 12, 0, 0 );
// expiration.setTime(expiration.getTime() + 604800000);
// =============================================================================================================
// Function: savePolyflorCorporateUserDetails( f )
// =============================================================================================================
//
// This function writes a cookie to the user's computer.
// The cookie is named 'PolyflorCorporateUserDetails' and contains the user's name, address, telephone number etc
//
// When the user next uses the 'Sample Express' form - their details are retrieved from the cookie and used to
// populate the document fields automatically - saving the user form having to re-enter their details
function savePolyflorCorporateUserDetails( f ) {
var saveFields;
// var SelectedSector=f.sector.options[f.sector.selectedIndex].text;
  	  	
// Build a string containing all the field values - to be saved on the user's PC as a cookie
    
saveFields = f.name_first.value+"~";				//   0 - First Name:
saveFields += f.name_last.value+"~";			//   1 - Last Name:
saveFields += f.addr1.value+"~";					//   2 - Address (1)
saveFields += f.addr2.value+"~";					//   3 -      "         (2)
saveFields += f.town.value+"~";					//   4 - Town
saveFields += f.StateOrProvince.value+"~";	//   5 - State or Province:
saveFields += f.pcode.value+"~";					//   6 - Post / ZIP Code:
saveFields += f.country.selectedIndex+"~";	//   7 - Country:
saveFields += f.tel.value+"~";						//   8 - Telephone:
saveFields += f.email.value+"~";					//   9 - Email:
 	
     // Store value of Title radio button that is selected e.g. Mrs
   
for ( i=0; i<f.title.length; i++) 					// 11 - Title:
{if(f.title[i].checked==true) {saveFields += f.title[i].value+"~" }}
saveFields += f.job_title.selectedIndex+"~";		
saveFields += f.company.value+"~";			//   13 - Company:
saveFields += f.fax.value+"~";			//   14 - Fax:
saveFields += f.sector.selectedIndex+"~";		//   15 - Sector: -index of selected keyword
saveFields += f.IndustrySectorOther.value+"~";	//   16 - Specify sector: - used if sector is 'Other please specify'
saveFields += f.jobtitleother.value+"~";		//   17 - Job title:
saveFields += getCheckedValue(document.forms[0].elements['enquiryTyp'])+"~";		//   18 - Enquiry Type:
// Set the cookie - to store user's selections on their computer
setCookie('PolyflorUserDetails', saveFields, expiration );
  	
}
// =============================================================================================================
// Function: loadPolyflorCorporateUserDetails( f )
// =============================================================================================================
//
// This function reads a cookie from the user's computer.
// The cookie is named 'PolyflorCorporateUserDetails' and contains the user's name, address, telephone number etc
//
// The cookie's contents are used to populate the document fields automatically - saving the user form having to re-enter their details
function loadPolyflorCorporateUserDetails (f) {
    
	t = getCookie('PolyflorUserDetails');
	//alert( 'Cookie=' + t );
	
	if (!t=='') {
		t2 = t.split('~');
		f.name_first.value = t2[0];			//    0 - First Name:
		f.name_last.value = t2[1];			//    1 - Last Name:
		f.addr1.value = t2[2];					//    2 - Address (1)
		f.addr2.value = t2[3];					//    3 - Address (2)
		f.town.value = t2[4];						//    4 - Town
		f.StateOrProvince.value=t2[5];	     //    5 - State / Province
		f.pcode.value = t2[6];					//    6 - Post / ZIP code
		f.country.selectedIndex = t2[7];	     //    7 - Country ( index of selected keyword )
		f.tel.value = t2[8];						//    8 - Telephone:
		f.email.value = t2[9];					//    9 - Email:
		for ( i=0; i<f.title.length; i++) 			//  10 - Radio button with stored value is checked
		{if(f.title[i].value==t2[10]) { f.title[i].checked=true; }
		}
		f.job_title.selectedIndex = t2[11];			
		f.company.value = t2[12];			//    12 - Company:
		f.fax.value = t2[13];				//    13 - Fax:
		f.sector.selectedIndex = t2[14];	     	//    14 - Sector ( index of selected keyword )
		f.IndustrySectorOther.value = t2[15];		//    15 - Specify sector: - has a value if user selected Sector 'Other please specify'
		f.jobtitleother.value = t2[16];	
		if(t2[17] == undefined || t2[17] == ""){}
		else{
			setCheckedValue(document.forms[0].elements['enquiryTyp'],t2[17]);
			if(t2[17] == "Commercial"){document.getElementById('compAsterix').style.visibility = 'visible';}
		}
		
		// OK - fields are now loaded with values from the cookie - Now decide what is to be displayed
		// If the user selected industry sector 'Other please specify' then display the 'Other Industry Sector' field and its title
		
		var SelectedSector=f.sector.options[f.sector.selectedIndex].text;
		if (SelectedSector=="Other - please specify")
		{document.getElementById("IDIndustrySectorOtherTitle").style.display="block";
		document.getElementById("IDIndustrySectorOther").style.display="block";}
		var SelectedJob_Title = f.job_title.options[f.job_title.selectedIndex].text;
		if (SelectedJob_Title=="Other - please specify")
		{document.getElementById("IDJobTitle").style.display="block";
		document.getElementById("IDJobTitleOther").style.display="block";}
		
	}
} // end function loadPolyflorCorporateUserDetails
// =============================================================================================================
// Function: getCookie( name )
// =============================================================================================================
//
// This function reads the named cookie from the user's computer
function getCookie(name){
  var cname = name + "=";               			// e.g. 'PolyflorUserDetails='
  var dc = document.cookie;             			// get all defined cookies
  
  // Check cookie(s) are loaded from the user's computer  
  if (dc.length > 0) {
      // Search for the cookie - beginning with the specified name
    begin = dc.indexOf(cname);     
	// Check the required cookie name was found  
	    if (begin != -1) { 
	    		// Extract the text between the initial cookie name and the terminating ';' ( or end of string )
		      begin += cname.length;       
		      end = dc.indexOf(";", begin);
		      if (end == -1) end = dc.length;
		      // return the cookie
	           return unescape(dc.substring(begin, end));
  		  } 
	  }
  return null;
}	// end function - getCookie
// =============================================================================================================
// Function: setCookie(name, value, expires, path, domain, secure)
// =============================================================================================================
//
// This function writes the named cookie to the user's computer
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}
// =============================================================================================================
// Function: delCookie (name,path,domain)
// =============================================================================================================
//
// This function deletes the named cookie from the user's computer
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
// =============================================================================================================
// Function: validate ( f )
// =============================================================================================================
//
// This function validates the data in the required fields on the form
// It is executed when the user tries to submit the form by clicking the 'Send Request' button on the form
//
function validate(f) {
	if (f.submitted.value!='') 
	{
	alert ('You have already sent this request form.');
  	return false;
  	}
	var msg;
	var SelectedSector=f.sector.options[f.sector.selectedIndex].text;	
	var err = "";
	if(f.name_first.value.length<3) { err+='- Please enter your first name.\n' }
	if(f.name_last.value.length<3) { err+='- Please enter your last name.\n' }
	//
	if(f.addr1.value.length<4) { err+='- Please enter your address.\n' }
	if(f.town.value.length<4) { err+='- Please enter your town/city.\n' }
	if(f.StateOrProvince.value.length<4) { err+='- Please enter your county/state/province.\n' }
	if(f.pcode.value.length<4) { err+='- Please enter your post code.\n' }
	//check that enquiry type is checked
	var enquiryTypeSelection = getCheckedValue(document.forms[0].elements['enquiryTyp'])
	if(enquiryTypeSelection == ""){
	err+='- Please select your enquiry type.\n'
	}
	//If it's commercial then ask for the Company Name
	if(enquiryTypeSelection == "Commercial"){
	if(f.company.value == "") { err+='- Please enter a company name.\n' }
	}
	if(f.country[f.country.selectedIndex].value=='0' || f.country[f.country.selectedIndex].value=='-') {err+='- Please select your country.\n' }	
	if((f.email.value.length<9)||(f.email.value.indexOf('@')==-1)||(f.email.value.indexOf('.')==-1)) { err+='- Valid email address required.\n' }
	if(f.tel.value.length<4) { err+='- Please enter your telephone number.\n' }
	
	//Job Title
	var SelectedJobTitle = f.job_title.options[f.job_title.selectedIndex].text;	
	if(f.sector[f.job_title.selectedIndex].value=='0') { err+='- Please select your Job Title.\n' }
	// If user has selected 'Other - please specify' then ensure they enter an job title:
	if (SelectedJobTitle == "Other - please specify" && f.jobtitleother.value.length<3) {err +='- Please specify your Job Title.\n'}		
	//Sector
	var SelectedSector=f.sector.options[f.sector.selectedIndex].text;	
	if(f.sector[f.sector.selectedIndex].value=='0' || f.sector[f.sector.selectedIndex].value=='-') { err+='- Please select your Sector.\n' }
	//
	// If user has selected 'Other - please specify' then ensure they enter an actual industry sector:
	if (SelectedSector=="Other - please specify" && f.IndustrySectorOther.value.length<3) {err +='- Please enter your Sector.\n'}
	if(f.TextEntered.value == "") { err+='- Spam Checker text.\n' }
	msg = "Your form is not complete for the following reasons.\n";
 	msg += "Please correct these error(s) and send your form again.\n";
  	msg += "_______________________________________\n\n";
  	msg += err;	
	if(err=="") 	 
	{
	// No errors detected - just save the document:
	
	f.submitted.value='y'
	
	// Save all the contact information for the user in a cookie on the user's computer
  	savePolyflorCorporateUserDetails( f )
  	
  	// Submit the form	
  	f.submit();
  	
     } 
	else 
	{
	alert(msg);
  	return false;
	}
}
addLoadEvent(function (){
loadPolyflorCorporateUserDetails ( document.forms[0] );
 });




