/*
REGISTER.JS
Author : Martin Chaput
*/


var url = 'captcha_code.php?validate_code='; 
var captchaOK = 2; // 2 - not yet checked, 1 - correct, 0 - failed 

function getHTTPObject() { 
    try { 
        req = new XMLHttpRequest(); 
        } catch (err1) 
        { 
            try { 
                req = new ActiveXObject("Msxml12.XMLHTTP"); 
                } catch (err2) 
                { 
                    try { 
                        req = new ActiveXObject("Microsoft.XMLHTTP"); 
                        } catch (err3) 
                        { 
                            req = false; 
                        } 
                } 
        } 
        
        return req; 
} 

var http = getHTTPObject(); // We create the HTTP Object 

function handleHttpResponse() { 
    if (http.readyState == 4) { 
        captchaOK = http.responseText; 
        
        if(captchaOK != 1) { 
            alert('The entered code was not correct. Please try again'); 
            document.register_form.validate_code.value=''; 
            document.register_form.validate_code.focus(); 
            return false;
        } 
        
        document.register_form.submit(); 
    } 
} 

function checkcode(thecode) { 
    http.open("GET", url + escape(thecode), true); 
    http.onreadystatechange = handleHttpResponse; 
    http.send(null); 
} 

function validate_registerform(f)
{
    //                    0           1                2                        3                 4       5					 6
    var ary1 = new Array('Password', 'Email Address', 'Email Address (again)', 'Address Line 1', 'City', 'Zip/Postal Code', 'Phone');
    var ary2 = new Array(f.password.value, f.email.value, f.email_validate.value, f.address_line1.value, f.city.value, f.zip.value, f.phone.value);
    //var ary3 = new Array('password', 'email', 'address_line1', 'city', 'zip');


    //Required : Company name OR Full name
    if (f.company_name.value == '' && (f.first_name.value == '' || f.last_name.value == '')) {
        alert("You need to provide a company name OR your full name to register");
        return false;
    }

    //check if all fields are filled
    for(i=0; i<ary2.length; i++) {
        var check1=IsEmpty(ary2[i]);
        if(!check1) {
            alert("The field '"+ ary1[i]+ "' can not be empty." );
            return false;
        }
    }

    //check if we have a valid email
    var flag=valid_email(ary2[1],ary1[1]);
    if (!flag) return false;

    //check if the two email fields are indentical
    var flag=(f.email.value == f.email_validate.value);
    if (!flag) {
        alert("E-mail address does not verify correctly, please re-enter e-mail address");
        f.email.select();
        f.email.focus();
        return false;
    }


    //check that one state has been selected
    var flag=isSelected(f.state);
    if (!flag)
    {
        alert("Please select a state/province from the list");
        f.state.focus();
        return false;
    }


    //check that one country has been selected
    var flag=isSelected(f.country);
    if (!flag)
    {
        alert("Please select a country from the list");
        f.country.focus();
        return false;
    }


    //check if we have a valid zip for USA & Canada
    var flag=valid_zip(ary2[4],ary1[4]);
    if (!flag) {
        f.zip.select();
        f.zip.focus();
        return false;
    }


    //check that we have a valid password
    var flag=valid_pwd(ary2[0],ary1[0]);
    if (!flag) {
        f.password.select();
        f.password.focus();
        return false;
    }
    
    //check that we have a valid phone number
    var flag=valid_phone(ary2[6],ary1[6]);
    if (!flag) {
        f.phone.select();
        f.phone.focus();
        return false;
    }


    //check that webmaster has agreed to terms & conditions
    var flag=f.checkbox_terms.checked;
    if (!flag)
    {
        alert("You must read & agree to the \n Terms & Conditions to continue.");
        f.checkbox_terms.focus();
        return false;
    }
	

    if (document.getElementById("captcha_div").style.display == "none") {
        f.submit();
		return true
    } else {
        // Now the Ajax CAPTCHA validation 
        checkcode(f.validate_code.value); 
        return true; 
    }
	
	  //if we're, everything went fine
 /*   f.submit();
    return true;*/
}



function valid_email(val, desc) {
    re=/^[0-9a-z]([-_\.]?[0-9a-z])*@[0-9a-z]([-_\.]?[0-9a-z])*\.[a-z]{2,4}$/i;

    spaces = / /gi;
    val = val.replace(spaces, "");

    if(re.test(val))
    {
        return true;
    }
    else
    {
        alert("Invalid '"+ desc + "'.\n");
        return false;
    }
}



function valid_pwd(val, desc)
{
    re=/^[a-z0-9A-Z]{4,15}$/;
    if(re.test(val)) {
        return true;
    } else {
        alert("Invalid '"+ desc + "',\nShould only contain alphanumeric characters.\nMin 4, max 15");
        return false;
    }
}


function valid_phone(val, desc)
{
    re=/^[0-9-_\. ]{4,20}$/;
    if(re.test(val)) {
        return true;
    } else {
        alert("Invalid '"+ desc + "',\nShould only contain numeric characters.\nMin 6, max 20");
        return false;
    }
}


function valid_zip(val,desc)
{
    //check for United States
    if (document.register_form.country.options[document.register_form.country.selectedIndex].value == "840")
    {
        if (val.length != 5 && val.length != 9 )
        {
            alert("Invalid '"+ desc + "'.\n");
            return false;
        }
        else
        {
            return true;
        }
    }

    //check for Canada
    if (document.register_form.country.options[document.register_form.country.selectedIndex].value == "124")
    {
        regexp=/^([a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}[\s]*[-]*[\.]*[0-9]{1}[a-zA-Z]{1}[0-9]{1}){1}$/i;
        if(regexp.test(val))
        {
            return true;
        }
        else
        {
            alert("Invalid '"+ desc + "'.\n");
            return false;
        }
    }
    else
    {
        return true;
    }
}



function IsEmpty(field)
{
    if(field=='')
    {
        return false;
    }
    else
    {
        return true;
    }
}



function isSelected(fieldObject)
{
    if (!fieldObject.options[fieldObject.selectedIndex].value) { return false; }
    else { return true;}
}




function check_inst()
{
    var inst_index = window.document.register_form.inst_mess.selectedIndex;
    var inst = window.document.register_form.inst_mess.options[inst_index].value;

    if (inst == "NONE")
        window.document.register_form.inst_info.disabled = true;
    else
        window.document.register_form.inst_info.disabled = false;
} 
