
function validateme()
{

    var name = document.getElementById('first_name').value;
	var email = document.getElementById('email').value;
	var comment = document.getElementById('description').value;
	var code = document.getElementById('code').value;
	
	if(name == '' || !leadspace(name))
	{
		alert("Please enter your name");
		document.getElementById('first_name').focus();
		return false;
	}

	if(email == '' || !leadspace(email))
	{
		alert("Please enter email address");
		document.getElementById('email').focus();
		return false;
	}
	else if(!checkEmail(document.getElementById('email'),"Please enter valid email address",0))
	{
		return false;
	}


	if(comment == '' ||!leadspace(comment))
	{
	alert("Please enter your comment");
	document.getElementById('description').focus();
	return false;
	}

	if(code== ''||!leadspace(code))
	{
	alert("Please enter code");
	document.getElementById('code').focus();
	return false;
	}
	return true;	

}


function validate(captcha)
{
    var captchaValue = captcha;   
    var fname = document.getElementById('first_name').value;
    var Email = document.getElementById('email').value;
    var comment = document.getElementById('description').value;
    var phoneNum = document.getElementById('phone').value;
  

	
    if(fname == '' || !leadspace(fname))
    {
        alert("Please enter your name");
        document.getElementById('first_name').focus();
        return false;
    }

    if(Email == '' || !leadspace(Email))
    {
        alert("Please enter email address");
        document.getElementById('email').focus();
        return false;
    }
    else if(!checkEmail(document.getElementById('email'),"Please enter valid email address",0))
    {
        return false;
    }
    
    if (checkInternationalPhone(phoneNum)==false){
        alert("Please enter a valid phone number");        
        document.getElementById('phone').focus();    
        return false;
    }

    if(comment == '' ||!leadspace(comment))
    {
        alert("Please enter your comment");
        document.getElementById('description').focus();
        return false;
    }
    
 

    if(document.contactus.security.value != captchaValue)
    {
        alert('Incorrect security code please enter once agian.')
        document.contactus.security.focus();
        return false;
    }
  
    return true;	

}

function leadspace(fieldvalue)
{
    var string = fieldvalue;
    if (string.charAt('0') == ' ')
    {
        return false;
    }
    return true;
}
// Function to validate Email address format
function checkEmail(fld,msg,opt)
{
    if(opt=="0" || fld.value!="")
    {
        var emailStr=fld.value;
        var checkTLD=1;
        var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

        var emailPat=/^(.+)@(.+)$/;

        var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

        var validChars="\[^\\s" + specialChars + "\]";

        var quotedUser="(\"[^\"]*\")";

        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

        var atom=validChars + '+';

        var word="(" + atom + "|" + quotedUser + ")";

        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

        var matchArray=emailStr.match(emailPat);

        if (matchArray==null)
        {
            fld.focus();
            fld.select();
            alert(msg);
            return false;
        }

        var user=matchArray[1];
        var domain=matchArray[2];

        for (i=0; i<user.length; i++)
        {
            if (user.charCodeAt(i)>127)
            {
                fld.focus();
                fld.select();
                alert(msg);
                return false;
            }
        }

        for (i=0; i<domain.length; i++)
        {
            if (domain.charCodeAt(i)>127)
            {
                fld.focus();
                fld.select();
                alert(msg);
                return false;
            }
        }

        // See if "user" is valid

        if (user.match(userPat)==null)
        {
            // user is not valid
            fld.focus();
            fld.select();
            alert(msg);
            return false;
        }

        var IPArray=domain.match(ipDomainPat);
        if (IPArray!=null)
        {
            for (var i=1;i<=4;i++)
            {
                if (IPArray[i]>255)
                {
                    fld.focus();
                    fld.select();
                    alert(msg);
                    return false;
                }
            }
            return true;
        }


        var atomPat=new RegExp("^" + atom + "$");
        var domArr=domain.split(".");
        var len=domArr.length;
        for (i=0;i<len;i++)
        {
            if (domArr[i].search(atomPat)==-1)
            {
                fld.focus();
                fld.select();
                alert(msg);
                return false;
            }
        }

        if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
        {
            fld.focus();
            fld.select();
            alert(msg);
            return false;
        }

        if (len<2)
        {
            fld.focus();
            fld.select();
            alert(msg);
            return false;
        }
        return true;
    }
    return true;
}
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 digits = "0123456789";
    var phoneNumberDelimiters = "()- ";
    var validWorldPhoneChars = phoneNumberDelimiters + "+";
    var minDigitsInIPhoneNumber = 10;
    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 validatemehome()
{

    var name = document.getElementById('first_name').value;
	var email = document.getElementById('email').value;
	var comment = document.getElementById('description').value;
	var code = document.getElementById('code').value;
	
	if(name == '' || !leadspace(name))
	{
		alert("Please enter your name");
		document.getElementById('first_name').focus();
		return false;
	}

	if(email == '' || !leadspace(email))
	{
		alert("Please enter email address");
		document.getElementById('email').focus();
		return false;
	}
	else if(!checkEmail(document.getElementById('email'),"Please enter valid email address",0))
	{
		return false;
	}


	if(comment == '' ||!leadspace(comment))
	{
	alert("Please enter your comment");
	document.getElementById('description').focus();
	return false;
	}

	if(code== ''||!leadspace(code))
	{
	alert("Please enter code");
	document.getElementById('code').focus();
	return false;
	}
	else
	{
		document.getElementById('homecontact').submit();
	}
	return true;	

}


                                                                                                                                                   

                                                                            

