﻿//bioler care validation

function SendSignup(sPackage, sTitle, sFirstname, sSurname, sEmail, sContact, sAddress) {
	new Ajax.Request('affiliate/Utilities.asmx/SignupBoiler', {
        method: 'get',
    	parameters: 'spackage=' + sPackage + '&stitle=' + sTitle + '&sfirstname=' + sFirstname + '&ssurname=' + sSurname + '&semail=' + sEmail + '&scontact=' + sContact + '&saddress=' + sAddress,
		onSuccess: function() {
		    $('boilerform').innerHTML="<span style='padding:13px; font-size: 18px; color: rgb(65, 59, 137);'>Thank you for your request</span>";
		    //$('boilerform').style.display='none';
			//$('boilerform_finished').style.display='block';			
			
		} } );
}

function validateBoiler() {
    if ( $('product').options[$('product').selectedIndex].value == 'select') {
        new Effect.Shake('product', {duration:0.3, distance:6});
        return false;
    }
    
    if ( $('title').options[$('title').selectedIndex].value == '') {
        new Effect.Shake('title', {duration:0.3, distance:6});
        return false;
    }    

    if( ($('firstname').value == '') || (!validateString($('firstname').value)) ) {
        new Effect.Shake('firstname', {duration:0.3, distance:6});
        return false;    
    }

    if( ($('surname').value == '') || (!validateString($('surname').value)) ) {
        new Effect.Shake('surname', {duration:0.3, distance:6});
        return false;    
    }

    if($('email').value == '' || !validateEmail($('email').value)) {
        new Effect.Shake('email', {duration:0.3, distance:6});
        return false;        
    }
    
    if($('contact').value == '' || !validateUKPhone($('contact').value)) {
        new Effect.Shake('contact', {duration:0.3, distance:6});
        return false;      
    }

    if ($('fromAddressEdit').style.display=='none') {
        new Effect.Shake('movingto', {duration:0.3, distance:6});
        return false;
    }

    if($('fromAddressArea').value == '') {
        new Effect.Shake('fromAddressArea', {duration:0.3, distance:6});
        return false;      
    }

    SendSignup($('product').options[$('product').selectedIndex].value, 
        $('title').options[$('title').selectedIndex].value,
        $('firstname').value,
        $('surname').value,
        $('email').value,
        $('contact').value,
        $('fromAddressArea').value);
    
    return true;
}




//validation functions
function validateString(p) {
    var valString = /^(a-z|A-Z|0-9)*[^#$%^&*()']*$/
    
    if (!valString.test(p)) 
    { return false; }
    
    //  check for only spaces in field
    if (p.replace(/ /g, "") == '')
    { return false; }
    
    return true;
}

function validPostcode(p) {
    var ukPostcode = /^([Gg][Ii][Rr]0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/
    
    if (p.toUpperCase()=='SW1E') {
        return true;
    }
    else if (!ukPostcode.test(p)) { 
        return false; 
    }
    
    return true;
}


function validPostcodePrefix(p) {
    var ukPostcode = /^([Gg][Ii][Rr]0[Aa]{2})|((([A-Za-z][0-9]{1,2})))$/
    
    if (!ukPostcode.test(p))
    { return false; }
    
    return true;
}

function validateUKPhone(x) {
    if (!x.match(/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/))
        return false;
    return true;
}

function validateEmail(x) {
    //^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$
    //[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
    //if (!x.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i))
    if (!x.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,4})+$/))
        return false;    
    return true;
}

//strip prefix from postcode
function stripThePostcodePrefix(p) {
    if (p.length > 3)
    {
        if (p.substring(3,4) == ' ' || p.substring(3,4) == ',')
        { return p.substring(0,3); }
        else
        { return p.substring(0,4); }
    }
    else
    { return p; }
}
