// JavaScript Document
function validateGeneral(formNo){
	var cName = checkRequired("name");
	var cCompany = checkRequired("company");
	var cEmailReq = checkRequired("email");
	var cEmail = checkEmail("email");
	var cContactReq = checkRequired("contact");
	var cContact = checkPhone("contact");
	var cEnquiry = checkRequired("enquiry");
	
	//reset and hide all Verification, Error, Required
	document.getElementById('name-verified').style.display = "none";
	document.getElementById('name-error').style.display = "none";
	document.getElementById('name-required').style.display = "none";
	document.getElementById('company-verified').style.display = "none";
	document.getElementById('company-error').style.display = "none";
	document.getElementById('company-required').style.display = "none";
	document.getElementById('email-verified').style.display = "none";
	document.getElementById('email-error').style.display = "none";
	document.getElementById('email-required').style.display = "none";
	document.getElementById('contact-verified').style.display = "none";
	document.getElementById('contact-error').style.display = "none";
	document.getElementById('contact-required').style.display = "none";
	document.getElementById('enquiry-verified').style.display = "none";
	document.getElementById('enquiry-error').style.display = "none";
	document.getElementById('enquiry-required').style.display = "none";
	
	if(!cName){
			document.getElementById('name-required').style.display = "inline";
	}else{
		document.getElementById('name-verified').style.display = "inline";	
	}
	
	if(!cCompany){
		document.getElementById('company-required').style.display = "inline";
	}else{
		document.getElementById('company-verified').style.display = "inline";	
	}
	
	if(!cEmailReq){
		document.getElementById('email-required').style.display = "inline";
	}else{
		if(!cEmail){
			document.getElementById('email-error').style.display = "inline";
		}else{
			document.getElementById('email-verified').style.display = "inline";	
			}		
	}
	
	if(!cContactReq){
		document.getElementById('contact-required').style.display = "inline";
	}else{
		if(!cContact){
			document.getElementById('contact-error').style.display = "inline";	
		}else{
			document.getElementById('contact-verified').style.display = "inline";	
		}
	}
	
	if(!cEnquiry){
		document.getElementById('enquiry-required').style.display = "inline";	
	}
	
	if(cName && cCompany && cEmailReq && cEmail && cContactReq && cContact && cEnquiry){
		document.forms[formNo].submit();	
	}else{
		return false;
	}
}

function validateAppt(formNo){
	var cName = checkRequired("name");
	var cCompany = checkRequired("company");
	var cEmailReq = checkRequired("email");
	var cEmail = checkEmail("email");
	var cContactReq = checkRequired("contact");
	var cContact = checkPhone("contact");
	var cDate = checkRequired("date");
	var cAddress = checkRequired("address");
	
	//reset and hide all Verification, Error, Required
	document.getElementById('name-verified').style.display = "none";
	document.getElementById('name-error').style.display = "none";
	document.getElementById('name-required').style.display = "none";
	document.getElementById('company-verified').style.display = "none";
	document.getElementById('company-error').style.display = "none";
	document.getElementById('company-required').style.display = "none";
	document.getElementById('email-verified').style.display = "none";
	document.getElementById('email-error').style.display = "none";
	document.getElementById('email-required').style.display = "none";
	document.getElementById('contact-verified').style.display = "none";
	document.getElementById('contact-error').style.display = "none";
	document.getElementById('contact-required').style.display = "none";
	document.getElementById('date-verified').style.display = "none";
	document.getElementById('date-error').style.display = "none";
	document.getElementById('date-required').style.display = "none";
	document.getElementById('address-verified').style.display = "none";
	document.getElementById('address-error').style.display = "none";
	document.getElementById('address-required').style.display = "none";
	
	if(!cName){
			document.getElementById('name-required').style.display = "inline";
	}else{
		document.getElementById('name-verified').style.display = "inline";	
	}
	
	if(!cCompany){
		document.getElementById('company-required').style.display = "inline";
	}else{
		document.getElementById('company-verified').style.display = "inline";	
	}
	
	if(!cEmailReq){
		document.getElementById('email-required').style.display = "inline";
	}else{
		if(!cEmail){
			document.getElementById('email-error').style.display = "inline";
		}else{
			document.getElementById('email-verified').style.display = "inline";	
			}		
	}
	
	if(!cContactReq){
		document.getElementById('contact-required').style.display = "inline";
	}else{
		if(!cContact){
			document.getElementById('contact-error').style.display = "inline";	
		}else{
			document.getElementById('contact-verified').style.display = "inline";	
		}
	}
	
	if(!cDate){
		document.getElementById('date-required').style.display = "inline";	
	}
	
	if(!cAddress){
		document.getElementById('address-required').style.display = "inline";	
	}
	
	if(cName && cCompany && cEmailReq && cEmail && cContactReq && cContact && cDate && cAddress){
		document.forms[formNo].submit();	
	}else{
		return false;
	}
}

function checkRequired(field){
		document.getElementById(field).value = trim(document.getElementById(field).value);
	if(document.getElementById(field).value.length > 0){
		return true;	
	}else{
		return false;	
	}
}

function checkEmail(field){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var returnval=emailfilter.test(document.getElementById(field).value);
	return returnval;
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 8;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkPhone(field){
	var Phone=document.getElementById(field);
	
	if (checkInternationalPhone(Phone.value)==false){
		return false;
	}
	return true;
 }

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}