/***********************************************************/
/* formCheck.js for Shoppinglinx.co.uk by  JMM 18/3/03     */
/* Adapted for Businesslinx 08/05/03                       */
/* Cross-browser validation for online registration forms. */
/* GenericForm validation is only compatible with IE5 +.   */
/* Some functions borrowed from FormChek.js (netscape.com) */
/***********************************************************/
// Check the browser
var isMSIE = document.all?true:false

// whitespace characters
var whitespace = " \t\n\r";
// All characters which are allowed in phone numbers
var phoneNumber = "+()-0123456789/";
// DomainName
var subDomain = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/********************** FUNCTIONS **************************/
function elError(ele, msg)
{
  if(isMSIE) ele.className = "Input-Box-invalid";
  ele.focus();
  if((ele.tagName == "TEXT") || (ele.tagName == "TEXTAREA") || (ele.tagName == "PASSWORD")) ele.select();
  alert(msg);
  return false;
}
function clearInvalid(tForm)
{
if(isMSIE)
  {
  for(i = 0; i < tForm.elements.length; i++)
    if(tForm.elements[i].className == "Input-Box-invalid")
      tForm.elements[i].className = "Input-Box";
  }
}
function checkRequired(tForm)
{
  required = true;
  for(z = 0; z < tForm.elements.length; z++)
    if(tForm.elements[z].id == "rec")
      if(isWhiteSpace(tForm.elements[z].value))
        {
        if(isMSIE) tForm.elements[z].className = "Input-Box-invalid";
        required = false;
        }
  if(!required)
    {
    alert("Please complete all required fields.")
    return false;
    }
  else return true;
}
/********************* VALIDATIONS *************************/
// Returns true if str does contain any white space
function noWhiteSpace(str)
{
for (i = 0; i < str.length; i++)
  if(whitespace.indexOf(str.charAt(i)) >= 0 )
    return false;
return true;
}
// Returns true if str only contains whitespace
function isWhiteSpace(str)
{
for (i = 0; i < str.length; i++)
  if(whitespace.indexOf(str.charAt(i)) < 0 )
    return false;
return true;
}
function isPhoneNumber(str)
{
for (i = 0; i < str.length; i++) {
  //alert("Checking phone digit " +i+ " ("+ str.charAt(i) +")")
  if(phoneNumber.indexOf(str.charAt(i)) < 0 )
    return false;
  }
return true;
}
function isEmail(str){
  i = 1;
  StringLength = str.length;
  while ((i < StringLength) && (str.charAt(i) != "@")){ i++ }
  if ((i >= StringLength) || (str.charAt(i) != "@")) return false;
  else i += 2;
  while ((i < StringLength) && (str.charAt(i) != ".")){ i++ }
  if ((i >= StringLength - 1) || (str.charAt(i) != ".")) return false;
  else return true;
}
// isValid returns true if all characters in TestString are also in ValidChars
function isValid(TestString, ValidChars) {
	for (i = 0; i < TestString.length; i++) {
		var c = TestString.charAt(i);
		if (ValidChars.indexOf(c) == -1) return false;
	}
	return true;
}

/********************* PAGE SPECIFIC *********************/
function quickKey(evt) // Checks the buttons pressed in the Quick Search boxes.
{
    if(evt.keyCode == 13 && vQuickSearch())
    document.getElementById("QuickSearchPanelForm").submit()    
}

function vQuickSearch(){
  tForm = document.QuickSearch;
  clearInvalid(tForm);
  
  if(tForm.What.value == "What" || tForm.What.value == "What ?")
  {
	 tForm.What.value = ""
  }
  if(tForm.Where.value == "Where" || tForm.Where.value == "Where ?")
  {
	 tForm.Where.value = ""
  }
  
  if(isWhiteSpace(tForm.Where.value) && isWhiteSpace(tForm.What.value)){
     //tForm.Where.className = "Input-Box-invalid";
     //tForm.What.className = "Input-Box-invalid";
     alert("Please enter at least one search criteria")
     return false;
  }
  document.getElementById("qsTitle").innerHTML = "&nbsp;Searching..."
  return true;
}

function vEmail()
{
  tForm = document.getElementById("form1");
  clearInvalid(tForm);
  // Customer function (on contact page) to check radio buttons.
  if(!checkRequired(tForm)) return false;
  if((!isEmail(tForm.EmailFrom.value)) && (!isWhiteSpace(tForm.EmailFrom.value))) return elError(tForm.EmailFrom, "Please enter a valid E-Mail address");
	
	tForm.Body.value = "Message: " + tForm.Body.value + "\n"
  tForm.Body.value = "Email: " + tForm.EmailFrom.value + "\n" + tForm.Body.value + "\n"
  tForm.Body.value = "Name: " + tForm.Name.value + "\n" + tForm.Body.value + "\n"
  tForm.Body.value = "A customer has sent you the following message using the online contact form on www.businessfinder.co.uk: \n\n" + tForm.Body.value
  return true;
}
function vContact(omitCheckBoxes)
{
  tForm = document.getElementById("form1")
  clearInvalid(tForm);
  
  // Customer function (on contact page) to check radio buttons.
  if (!omitCheckBoxes) {if(!checkRadio()) return false;}
  if(!checkRequired(tForm)) return false;
  if((!isEmail(tForm.EmailFrom.value)) && (!isWhiteSpace(tForm.EmailFrom.value))) return elError(tForm.EmailFrom, "Please enter a valid E-Mail address");
  if(!isPhoneNumber(tForm.Phone.value)) return elError(tForm.Phone, "Please enter a valid phone number");

	tForm.Body.value = "Message: " + tForm.Body.value
  if(tForm.EmailFrom.value.length != 0){ tForm.Body.value = "Email: " + tForm.EmailFrom.value + "\n" + tForm.Body.value + "\n"}
  if(tForm.Phone.value.length != 0){ tForm.Body.value = "Phone: " + tForm.Phone.value + "\n" + tForm.Body.value + "\n"}
  tForm.Body.value = "Name: " + tForm.Name.value + "\n" + tForm.Body.value + "\n"
  if(tForm.Company.value.length != 0) {tForm.Body.value = "Company: " + tForm.Company.value + "\n" + tForm.Body.value + "\n"}
  tForm.Body.value = "A customer has sent you the following message using the online contact form on www.businessfinder.co.uk: \n\n" + tForm.Body.value
	tForm.submit()
}
function vRequest()
{
  tForm = document.form1;
  clearInvalid(tForm);
  if(!checkRequired(tForm)) return false;
  if(tForm.HearAboutUs.value == "0") return elError(tForm.HearAboutUsOther, "Please explain where you heard about us.");
  if(tForm.ContactTitle.value == "") return elError(tForm.ContactTitle, "Please enter your title");
  if(tForm.ContactFirstName.value == "") return elError(tForm.ContactFirstName, "Please enter your first name");
  if(tForm.ContactLastName.value == "") return elError(tForm.ContactLastName, "Please enter your last name");
  if(tForm.ContactJobTitle.value == "") return elError(tForm.ContactJobTitle, "Please enter your Job Title");
  if(tForm.Trade.value == "") return elError(tForm.Trade, "Please enter business category");
  if(tForm.Location.value == "") return elError(tForm.Location, "Please enter location of the business");
  if(tForm.CompanyCompanyName.value == "") return elError(tForm.CompanyCompanyName, "Please enter your Company Name");
  if(tForm.CompanyStreet.value == "") return elError(tForm.CompanyCompanyName, "Please enter your Company Name");
  if(tForm.CompanyTown.value == "") return elError(tForm.CompanyStreet, "Please enter your Company Name");
  if(!isPhoneNumber(document.form1.ContactPhone.value))  return elError(tForm.ContactPhone, "Please enter a valid Phone number");
  if(!isPhoneNumber(document.form1.CompanyPhone.value))  return elError(tForm.CompanyPhone, "Please enter a valid Phone number");
  if(!isPhoneNumber(document.form1.ContactExt.value))    return elError(tForm.ContactExt, "Please enter a valid Extension number");
  if(!isPhoneNumber(tForm.ContactFax.value))    return elError(tForm.ContactFax, "Please enter a valid Fax number");
  if(!isPhoneNumber(tForm.CompanyFax.value))    return elError(tForm.CompanyFax, "Please enter a valid Fax number");
  if(!isPhoneNumber(tForm.ContactMobile.value)) return elError(tForm.ContactMobile, "Please enter a valid Mobile number");
  if(!isEmail(tForm.ContactEmailAddress.value)) return elError(tForm.ContactEmailAddress, "Please enter a valid E-Mail address");
  if(!isEmail(tForm.CompanyEmailAddress.value))   return elError(tForm.CompanyEmailAddress, "Please enter a valid E-Mail address");
  if(tForm.CompanyDescription.value.length > 255) return elError(tForm.CompanyDescription, "The Business Description cannot be more than 255 characters");
  if(tForm.CompanyCounty.value == "") return elError(tForm.CompanyCounty, "Please enter your company county");
  if(tForm.txtCaptcha.value == "") return elError(tForm.txtCaptcha, "Please enter security text");

  
  return true;
}
function vPasswordRequest()
{
  tForm = document.form1;
  clearInvalid(tForm);
  if(!checkRequired(tForm)) return false;
  if(!isEmail(tForm.EmailAddress.value)) return elError(tForm.EmailAddress, "Please enter a valid E-Mail address");
  return true;
}

function validateFormGen(){
	var strError = "The following items are missing from the form, please complete all fields with an arrow.\n\n";
	var bResult = false;
	var iCount = 1;
	for (cnt=0;cnt<document.getElementById("form1").elements.length;cnt++){
		if(document.getElementById("form1").elements[cnt].ValID != 0){
			if(isEmptyCheck(document.getElementById("form1").elements[cnt].value)){
				if(document.getElementById("form1").elements[cnt].ValID == 2){
					document.getElementById("form1").elements[cnt].className = "Text-Area-invalid";
				}else{
					document.getElementById("form1").elements[cnt].className = "Input-Box-invalid";
				}
				strError = strError + iCount + ". " + document.getElementById("form1").elements[cnt].ValName + "\n"
				bResult = false;
				iCount += 1
			}else{
				if(document.getElementById("form1").elements[cnt].ValID == 2){
					document.getElementById("form1").elements[cnt].className = "Text-Area";
				}else{
					document.getElementById("form1").elements[cnt].className = "Input-Box";
				}
			}
		}
	}
	if(strError != "The following items are missing from the form, please complete all fields with an arrow.\n\n"){
		alert(strError)
		return bResult;
	}else{
		//Loop through validated form and insert values into FormBody to post to A2Mail  - AH 05/07
		var strBody = "<br>"
		for (cnt=0;cnt<document.getElementById("form1").elements.length;cnt++){
			if(document.getElementById("form1").elements[cnt].v == 1)
				strBody += document.getElementById("form1").elements[cnt].ValName + ": " + document.getElementById("form1").elements[cnt].value + "<br>"
		}
		document.getElementById("FormBody").value = strBody;
		return true;
	}
}

function isEmptyCheck(TestString){
	if ((TestString == null) || (TestString.length == 0)) return true;
}
