/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function $(id){
    return document.getElementById(id);
}

function focusField(thisObject, id){
    if(thisObject.value.length >= 3){
        $(id).focus()
    }
}

function validatePage( args ){
    var id = '';
    for(j = 0 ; j < args.length; j++){
        id = args[j];
        //alert(id);
        if(!validateItemById(id)){
            return false;
        }
    }
    return true;
}

function validateForm(){
    var args = new Array('first_name', 'last_name', 'email', 'phone','captchachar1');
    return validatePage(args);
}

function validateForm2(){
    var args = new Array('first_name2', 'last_name2', 'email2', 'phone2', 'captchachar1');
    return validatePage(args);
	
}
var xh1;
function get_xh()
{
	if(window.XMLHttpRequest)
		xh1 = new XMLHttpRequest();
	else if(window.ActiveXObject)
		xh1 = new ActiveXObject("Microsoft.XMLHTTP");
	else
	{
		alert("Your Browser Does Not Support AJAX");
		xh1 = null;
		return; 
	}
}

function checkCode(txt)
{
	var b = validateForm2();
	//alert(b);
	if(b==true)
	{
		get_xh();
		var p;
		//alert(atp);
		//alert(atp.options[atp.selectedIndex].value);
		
		p="code.php?a="+txt.value;
		//alert(p);
		xh1.open("GET",p,true);
		xh1.send(null);
		xh1.onreadystatechange = function()
		{
			if(xh1.readyState==4)
			{	
				var msg=xh1.responseText;
				//alert(msg);
				if(msg=="0")
				{
					alert("Invalide Captcha...");	
					txt.focus();
					setTimeout("txt.focus()", 1);
				}
			}
		};
	}
	
}

function validateEmail(string){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(string) == false)
	{
		return false;
	}
	
    //if(string.indexOf('@') > 0 && string.indexOf('.') > 2){
//        return true;
//    }
    return true;
}

function validateRequired(string){
    if(string == '' || string == undefined){
        return false;
    }
    return true;
    /*if(string.length <= 0){
        return false;
    }
    return true;
    */
}

function validateRadioSelected( id ){
    if($(id).checked == false){
        return false;
    }
    return true;
}

function validateNumber( string ){
    for(i = 0; i < string.length; i++){
        if(isNumber(string.substr(i, 1))){

        } else {
			
            return false;
        }
    }
    return true;
}

function validateCharacters( string ){
    for(i = 0; i < string.length; i++){
        if(isChar(string.substr(i, 1))){

        } else {
            return false;
        }
    }
    return true;
}

function validateAlphaNumeric( string ){
    for(i = 0; i < string.length; i++){
        if(isNumberChar(string.substr(i, 1))){
            
        } else {
            return false;
        }
    }
    return true;
}

function isNumber (c){

    if(isNaN(c)){
        return false;
    }

    return true;

}

function isNumberChar(c){
    var re=/^[A-z0-9]+$/;
    if(!re.test(c) ) {
        return false;
    }
    return true;
}

function isChar(c){

    var re=/^[A-z]+$/;
    if(!re.test(c) ) {
        return false;
    }
    return true;
}


function validateItemById( id ){
    var isError = false;
//	alert($('vercap').value);
    switch(id){
        case 'first_name2':
            if(!validateRequired($('first_name2').value)){
                alert('Please enter your first name.');
                isError = true;
            }
            if(!validateCharacters($('first_name2').value)){
                alert('Please enter only letters for your first name.');
                isError = true;
            }
            break;
        case 'last_name2':
            if(!validateRequired($('last_name2').value)){
                alert('Please enter your last name.');
                isError = true;
            }
            if(!validateCharacters($('last_name2').value)){
                alert('Please enter only letters for your last name.');
                isError = true;
            }
            break;
        case 'phone2':
            if(!validateRequired($('phone2').value)){
                alert('Please enter your phone number.');
                isError = true;
            }
			if(!validateNumber($('phone2').value))
			{
				alert('Please enter only number.');
                isError = true;
			}
            break;
        case 'email2':
            if(!validateRequired($('email2').value)){
                alert('Please enter your email.');
                isError = true;
            }
            if(!validateEmail($('email2').value)){
                alert('Please enter your email the email format email@domain.com.');
                isError = true;
            }
            break;
		case 'captchachar1':
			 //alert($('vercap').value);
			 if(!validateRequired($('captchachar1').value)){
                alert('Please enter verification code.');
                isError = true;
             }
	         /*if ($('captchachar1').value != $('vercap').value){				 
				 alert('Please enter verification code as shown in the graphic.');
				 isError = true;
			 }*/
			 break;			
			 
			 
    }
    if(isError){
        focus(id);
        $(id).style.backgroundColor = "#ffb9b9";
        return false;
    } else if(id != 'phone2' && id != null){
        $(id).style.backgroundColor = "#ffffff";
    }
    return true;
}
function validNumber(evt)
      {
//		  alert(evt.which);
         var key_code = (evt.which) ? evt.which : event.keyCode
         if ( key_code<=31 || (key_code>=48 && key_code<=57)||(key_code>=37 && key_code<=40)||key_code==46)
            return true;

         return false;
      }

