<!--
//simple validation of the user contact details
//in the form whose form index is sent as a param
//When move to addr.com & can set cookies see //DISABLED

function IsExists(AFieldName,AForm) {
var AField=GetField(AFieldName,AForm);
//alert(AFieldName+' exists: '+AField);
return (AField != null && AField != undefined);
}

function GetField(AFieldName,AForm){
try {
	if (document.getElementById){
	   return document.getElementById(AFieldName);
	   }
	   else {
       return AForm.AFieldName; 
	   }
    }
    catch(err) {
        return null;
		}	
}

function validate_form(IdxForm, IdxFormType) {
//FormType:
// - 0 normal contact details form, requiring: (first name & surname), (tel, fax or email)
// - 1 mailing options, requiring: email only
var validity = true;
var StrWarning = "Sorry, the information you have entered is incomplete. Before clicking 'submit' again, please enter:";
var FrmInput=document.forms[IdxForm];
if ((FrmInput.firstname.value == "") && (IdxFormType != 1)) {
   StrWarning=StrWarning+" your *First Name*,";
   validity = false;
   }
if ((FrmInput.surname.value == "") && (IdxFormType != 1)) {
   StrWarning=StrWarning+" your *Surname*,";
   validity = false;
   }
if (IsExists("email",FrmInput)){   
	if (IdxFormType == 1){
		if (FrmInput.email.value == "") {
			StrWarning=StrWarning+" your *E-mail Address*,";   
			validity = false;
			}
		}
		else {
		if ((FrmInput.email.value == "") && (FrmInput.phone.value == "") && (FrmInput.fax.value == "")){
			StrWarning=StrWarning+" a *Telephone* or *Fax* number, or an *E-mail Address* (ideally please provide all three),";
			validity = false;
			}
		}
	}
//End bit of warning
if (IdxFormType == 1){
   StrWarning=StrWarning+" so that we can update your preferences accordingly.";
   }
   else {
   StrWarning=StrWarning+" so that we will be able to respond to your enquiry.";
   } 
//Either proceeed or alert
if (validity){
   SetFormRealNameFieldValue(IdxForm);
   SetContactDetailsCookie(IdxForm,(IdxFormType == 1));
   validity=GetKenCheckAlert(IdxForm);
   }
   else {
   alert(StrWarning);
   }
return validity;
}

function GetFormRealNameValue(IdxForm){
var FrmInput=document.forms[IdxForm];
var Idx=0;
var StrRealName=" "+ FrmInput.surname.value;
if (IsExists("title",FrmInput)){Idx=FrmInput.title.selectedIndex};
if (Idx>0){
   StrRealName=FrmInput.title.options[Idx].text +" "+ FrmInput.surname.value;
   }
   else {
   StrRealName=FrmInput.firstname.value +" "+ FrmInput.surname.value;
   }
//alert(StrRealName);
return StrRealName;
}

function GetKenCheckAlert(IdxForm){
var FrmInput=document.forms[IdxForm];
var Str=FrmInput.firstname.value + FrmInput.surname.value;
Str = Str.toUpperCase();
if ((Str == "KENNYQ") || (Str == "KENNETHQ") || (Str=="ANDREWMCQUILLAN") || (Str=="ANDYMCQUILLAN")){
   alert("Sorry, enormous bespectacled bennies are banned from this site\nNow sling your hook! Go on clear off!"); 
   top.location.replace("http://www.gaylad.com");
   return false;
   }
   else {
   return true;
   }
}

function SetFormRealNameFieldValue(IdxForm){
//set the real name value in the form
//ie from the title, firstname & surname
var FrmInput=document.forms[IdxForm];
FrmInput.realname.value=GetFormRealNameValue(IdxForm);
}

function SetContactDetailsCookie(IdxForm,IsMailOptionsForm){
var FrmInput=document.forms[IdxForm];
var ExpDate = new Date();
ExpDate.setTime (ExpDate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
var IsStore=FrmInput.rememberdetails.checked;
var StrCookie="dummy|value";
if (IsStore){
   if (IsExists("title",FrmInput)){StrCookie+="|title|"+FrmInput.title.selectedIndex} else {StrCookie+="|title|"+GetNumericCookieField(ContactCookie,"title")};	 
   if (IsExists("firstname",FrmInput)){StrCookie+="|firstname|"+FrmInput.firstname.value};	
   if (IsExists("surname",FrmInput)){StrCookie+="|surname|"+FrmInput.surname.value};	
   if (IsExists("realname",FrmInput)){StrCookie+="|realname|"+FrmInput.realname.value};			 
   if (IsExists("phone",FrmInput)){StrCookie+="|phone|"+FrmInput.phone.value};				 
   if (IsExists("fax",FrmInput)){StrCookie+="|fax|"+FrmInput.fax.value};				 
   if (IsExists("email",FrmInput)){StrCookie+="|email|"+FrmInput.email.value};				 
   if (IsExists("position",FrmInput)){StrCookie+="|position|"+FrmInput.position.value};				 
   if (IsExists("company",FrmInput)){StrCookie+="|company|"+FrmInput.company.value};	
   if (IsExists("age",FrmInput)){StrCookie+="|age|"+FrmInput.age.value};   
   if (IsMailOptionsForm){
      StrCookie = StrCookie 
                + "|mailformat|"+FrmInput.mailformat.selectedIndex
                + "|mailfrequency|"+FrmInput.mailfrequency.selectedIndex;
      }
   }
//if choose not to remember, then just remember this!
StrCookie = StrCookie+"|rememberdetails|"+IsStore+"|";
//alert(StrCookie);
setCookie(ContactCookie, StrCookie, ExpDate);
}

//return a value rather than a blank string if not present
function GetNumericCookieField(ACookieName, AFieldName){
var AVal = GetCookieField(ACookieName,AFieldName);
if (AVal=="") {return 0} else {return AVal};
}

//IsMailOptionsForm is for when form is email alerts option form
function PopulateFormFromContactDetailsCookie(IdxForm,IsMailOptionsForm){
var FrmInput=document.forms[IdxForm];
var IsStore=!IsCookieExists(ContactCookie);
if (!IsStore){
   IsStore=eval(GetCookieField(ContactCookie,"rememberdetails"));
   if (IsStore){
      if (IsExists("title",FrmInput)){FrmInput.title.selectedIndex=GetCookieField(ContactCookie,"title")};
      if (IsExists("firstname",FrmInput)){FrmInput.firstname.value=GetCookieField(ContactCookie,"firstname")};
      if (IsExists("surname",FrmInput)){FrmInput.surname.value=GetCookieField(ContactCookie,"surname")};
      if (IsExists("realname",FrmInput)){FrmInput.realname.value=GetCookieField(ContactCookie,"realname")};
      if (IsExists("phone",FrmInput)){FrmInput.phone.value=GetCookieField(ContactCookie,"phone")};
      if (IsExists("fax",FrmInput)){FrmInput.fax.value=GetCookieField(ContactCookie,"fax")};
      if (IsExists("email",FrmInput)){FrmInput.email.value=GetCookieField(ContactCookie,"email")}; 
      if (IsExists("position",FrmInput)){FrmInput.position.value=GetCookieField(ContactCookie,"position")}; 
      if (IsExists("company",FrmInput)){FrmInput.company.value=GetCookieField(ContactCookie,"company")}; 
	  if (IsExists("age",FrmInput)){FrmInput.age.value=GetCookieField(ContactCookie,"age")}; 
      //If the form contains special offer email options 
      //set these too
      if (IsMailOptionsForm){
         FrmInput.mailformat.selectedIndex=GetCookieField(ContactCookie,"mailformat");
         FrmInput.mailfrequency.selectedIndex=GetCookieField(ContactCookie,"mailfrequency");
         }
      }
   }
//if not remembering, have remembered this..oh the irony!
if (IsExists("rememberdetails",FrmInput)){FrmInput.rememberdetails.checked=IsStore};
//overwrite email field with any parameter if its the email prefs form
if (IsMailOptionsForm){
   PopulateEmailFromParam(IdxForm);
   }
return true;
}

function GetSearchParam(){
var sstring = window.location.search;
//find *final* ?
return (sstring.substring(sstring.lastIndexOf("?") + 1, sstring.length));
}

//On load for email preference form only:
function PopulateEmailFromParam(IdxForm){
//take the parameter which is the email address & populate 
//the email field from it
var FrmInput=document.forms[IdxForm];
//find *final* ?
var StrExtract = GetSearchParam();
if (StrExtract!=""){
   FrmInput.email.value=StrExtract;
   }
}

//On load for sample request form only: - requires Pge_main.js
function PopulateProductCodeFromParam(IdxForm){
//take the parameter which is the product code 
//the email field from it
var FrmInput=document.forms[IdxForm];
//find *final* ?
var StrExtract = SearchAndReplace(GetSearchParam(),"%20"," ");
if (StrExtract!=""){
   //either cat request or specific product
   FrmInput.Send_Sample_Pack.checked=false;
   //test for catalogue request special link
   if (StrExtract=="sendcatalogue"){
      FrmInput.Send_Printed_Catalogue.checked=true;
      }
      else {
      FrmInput.Product_Code.value=StrExtract;
      FrmInput.Send_Product_Sample.checked=true;      
      }
   }
//Now populate the contact details form as per usual
PopulateFormFromContactDetailsCookie(0,false);
}

//While still at BT use:
function FormThanks(ThanksPage){
var FrmInput=document.forms[0];
var IsOk=false;
if (document.DetailsForm) {
   if (validate_form(0,0)){
      //redirection now done through formail so
      //remmed: self.location.href=ThanksPage;
      FrmInput.redirect.value = ThanksPage;
      IsOk=true;
      }
   }
   else {
   alert("Sorry Form is set-up incorrectly (Name is not DetailsForm), please contact Mangolab");
   }
return IsOk;
}
//-->
