/* 
parkmagic.js: library of javascript for animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    parkmagic_registration: validates the registration form that updates the users web profile
    parkmagic_login: validates the login form that is presented to customers
    parkmagic_reminder: validates the password reminder dialogue (checking that user has entered an email address
    parkmagic_details: validate the 'edit account details' dialogue
    parkmagic_password: validate the 'change password' dialogue
    password_conforms: validate that password entered by user conforms to password policy
    parkmagic_multi_reminder: validate that user has entered either a cell phone or in car display unit number
    parkmagic_contact: basica validation of a subscriber contact and anumber record
    display: pops up a window for printer friendly version of data
    check_amount: validate the top-up dialogue
    check_new_user: check that the checkout registration dialogue has been populated
    cancellation: checks the 'cancellation comment' form - which is an optional feedback form 
    check_topup: checks that user has either selected a valid top up amount or entered a valid amount
    toggle_other: show / hide input box for other topup amount
    check_ivr: ensure that form is complete
    volume_purchasing: test that required fields are completed
    string_syntax: validates a string input
    check_returning: check that the checkout 'login' dialogue has been populated correctly
*/
window.addEvent('load', function(){
    //alert("dhtml setup function");
  page_trigger();
});

function page_trigger(){
    var el=get_el("js_triggers");
    if (el){
       
        eval(el.value);
    }
    MM_preloadImages('../images/nav/account_down.jpg','../images/nav/account_active.jpg','../images/nav/news_down.jpg','../images/nav/news_active.jpg','../images/nav/cities_down.jpg','../images/nav/cities_active.jpg','../images/icd_cta_f2.jpg','../images/top_navigation_r1_c1_f3.jpg','../images/top_navigation_r1_c1_f2.jpg','../images/top_navigation_r1_c1_f4.jpg','../images/top_navigation_r1_c2_f3.jpg','../images/top_navigation_r1_c2_f2.jpg','../images/top_navigation_r1_c2_f4.jpg','../images/top_navigation_r1_c3_f3.jpg','../images/top_navigation_r1_c3_f2.jpg','../images/top_navigation_r1_c3_f4.jpg','../images/top_navigation_r1_c4_f3.jpg','../images/top_navigation_r1_c4_f2.jpg','../images/top_navigation_r1_c4_f4.jpg','../images/subpage_navbar_r1_c2_f3.jpg','../images/subpage_navbar_r1_c2_f2.jpg','../images/subpage_navbar_r1_c2_f4.jpg','../images/subpage_navbar_r1_c4_f3.jpg','../images/subpage_navbar_r1_c4_f2.jpg','../images/subpage_navbar_r1_c4_f4.jpg','../images/subpage_navbar_r1_c6_f3.jpg','../images/subpage_navbar_r1_c6_f2.jpg','../images/subpage_navbar_r1_c6_f4.jpg','../images/subpage_navbar_r1_c8_f3.jpg','../images/subpage_navbar_r1_c8_f2.jpg','../images/subpage_navbar_r1_c8_f4.jpg','../images/subpage_navbar_r1_c10_f3.jpg','../images/subpage_navbar_r1_c10_f2.jpg','../images/subpage_navbar_r1_c10_f4.jpg','../images/subpage_navbar_r1_c12_f3.jpg','../images/subpage_navbar_r1_c12_f2.jpg','../images/subpage_navbar_r1_c12_f4.jpg','../images/login_f2.gif','../images/icd_cta_sm_f2.jpg');
}

//page_setup: perform minor modifications to elements on the page
function page_setup(){
    var el=get_el('price');
    if (el){
        
    }
}
// parkmagic_login: validates the login form that is presented to customers
function parkmagic_login(){
// check that username field has been populated
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter your username or In Car Meter number if this is your first time to use this service!");
        el.focus();
        return false;
    }
    
// check the password entry
    var el=get_el("txt_password");
    if (el.value.length==0){
        alert("please enter your password or cell number if this is your first time to use this service!");
        el.focus();
        return false;
    }
    
    
    
return true;
}

//parkmagic_reminder: validates the password reminder dialogue (checking that user has entered an email address
function parkmagic_reminder(){
// check that username field has been populated
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter the email address you used before with this service!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!string_syntax(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
return true;
    
}


// parkmagic_registration: validates the registration form that updates the users web profile
function parkmagic_registration(){
    var el=get_el("txt_email");
    if (el.value.length==0){
        alert("please enter the email address you wish to use with this service!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!string_syntax(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
    return true;
}

//parkmagic_details: validate the 'edit account details' dialogue
function parkmagic_details(){
    // first name
    var el=get_el("txt_firstname");
    if (el.value.length==0){
        alert("please enter your first name!");
        el.focus();
        return false;
    }
    // surname
    var el=get_el("txt_surname");
    if (el.value.length==0){
        alert("please enter your family name!");
        el.focus();
        return false;
    }
    // address line 1
    var el=get_el("txt_address1");
    if (el.value.length==0){
        alert("please enter your street address!");
        el.focus();
        return false;
    }
    // address line 2
//    var el=get_el("txt_address2");
//    if (el.value.length==0){
//        alert("please enter your street address!");
//        el.focus();
//        return false;
//    }
    // address line 3 ==city
    var el=get_el("txt_address3");
    if (el.value.length==0){
        alert("please enter your city!");
        el.focus();
        return false;
    }
    // address line 4 == state
    var el=get_el("txt_address4");
    if (el.value.length==0){
        alert("please enter your state!");
        el.focus();
        return false;
    }
    // post-code == zip code
    var el=get_el("txt_postcode");
    if (el.value.length==0){
        alert("please enter your zip-code!");
        el.focus();
        return false;
    }
    // username /email
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter your email address (this is used for logging on to your web profile)!");
        el.focus();
        return false;
    }
    var m = el.value;
    if (!string_syntax(m,true)){
        alert("the email address that you have entered contains characters that are not supported by our system please contact technical support!");
        el.focus();
        return false;
    
    }
    // confirm email address
    var el_confirm=get_el("txt_confirm");
    if (el_confirm.value != m){
        alert("your email address is critical for all actions on the web site, please confirm your email address");
        
        el_confirm.focus();
        return false;
        
    }

    // mobile number
    var el=get_el("txt_mobile");
    if (el.value.length==0){
        alert("please enter your cell phone number!");
        el.focus();
        return false;
    }
    var m = el.value;
    // mobile number
    var el=get_el("txt_confirm_mobile");
    if (el.value != m){
        alert("please confirm your cell phone number!");
        el.value="";
        el.focus();
        return false;
    }
    
    // telephone number
//    var el=get_el("txt_telephone");
//    if (el.value.length==0){
//        alert("please enter a land line number!");
//        el.focus();
//        return false;
//    }
    return true;

}


//parkmagic_password: validate the 'change password' dialogue
function parkmagic_password(){
    var el=get_el("txt_password");
    var el_confirm=get_el("txt_confirm");
    if (el.value.length==0){
        alert("please enter a password that is at least 6 characters long!");
        el.value="";
        el_confirm.value="";
        el.focus()
        return false;
        
    }
    if (el.value.length > 12) {
        alert("please enter a password that is no longer than 12 characters in length!");
        el.value="";
        el_confirm.value="";
        el.focus()
        return false;
    }
    if (el.value.length < 6) {
        alert("please enter a password that is at least 6 characters long!");
        el.value="";
        el_confirm.value="";
        el.focus()
        return false;
    }
    var m=el.value;
    if (!string_syntax(m,false)){
        alert("the password that you have entered contains invalid characters, only letters and numbers are supported!");
        el.value="";
        el_confirm.value="";
        el.focus();
        return false;
    }
    if (!password_conforms(m)){
        alert("the password that you enter must contain at least one letter and number!");
        el.value="";
        el_confirm.value="";
        el.focus();
        return false;
    
    }
    var c = el_confirm.value;
    if (c!=m){
        alert("the password and confirm password values that you have entered do not match, please re-enter both values!");
        el_confirm.value="";
        el.value="";
        el.focus();
        return false;
    
    }
    return true;
    
}

//password_conforms: validate that password entered by user conforms to password policy
function password_conforms(value){
    var alpha_chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var numeric_chars="1234567890";
    var has_alpha=false;
    var has_number=false;
    for (var i=0;i<value.length;i++){
        //alert(alpha_chars.indexOf(value.substr(i,1)));
        if (alpha_chars.indexOf(value.substr(i,1))>-1){
            has_alpha=true;
        }
    }
   // alert(has_alpha);
    for (var i=0;i<value.length;i++){
        if (numeric_chars.indexOf(value.substr(i,1))>-1){
            has_number=true;
        }
    }
    if ((has_alpha)&&(has_number)){
        return true;
    }
    else{
        return false;
    }
    
}

//parkmagic_multi_reminder: validate that user has entered either a cell phone or in car display unit number
function parkmagic_multi_reminder(){
    var el_adc=get_el("txt_adc");
    var el_mobile=get_el("txt_mobile");
    if ((el_adc.value.length==0)&&(el_mobile.value.length==0)){
        alert("please enter either a cell phone number or in-car display unit number before proceeding!");
        return false;
    }
    return true;
    
}


//parkmagic_contact: basica validation of a subscriber contact and anumber record
function parkmagic_contact(){
    var el=get_el("txt_username");
    if (el){
        if (el.value.length!=0){// need to check that email address is correct format 
        var s=el.value;
        if (!string_syntax(s,true)){
            alert("the email address you have entered is not supported on this system, please check your entry!");
            el.focus();
            return false;
        
        }
        
        }
        
    }


    return true;
}

// display: pops up a window for printer friendly version of data
function display(url, title){
    var address="window.asp" + url;
    var window_width=0;
    var window_height=0;
    var window_top=0;
    var window_left=0;
    // set width, height and positioning properties based on context
    switch (title){
        case 'RECEIPT': // viewing details of a transaction
            window_width=460;
            window_height=500;
            window_top=0;
            window_left=0;
            break;
        case 'STATEMENT': // viewing a statement
            window_width=600;
            window_height=600;
            window_top=0;
            window_left=0;
            break;
        default:
    }
    
    var use_default=0; // over-ride default properties
    var k=open_new_window(address, title, use_default, window_width,window_height,window_top,window_left);
}

//check_amount: validate the top-up dialogue
function check_amount(){
    var el=get_el("amount");
    if (el.value.length==0){
        alert("please enter the amount that you wish to add to your account!");
        el.focus();
        return false;
    }
    if (isNaN(el.value)){
        alert("please enter a numeric value only when entering the amount that you wish to add to your account!");
        el.focus();
        return false;
    
    }
    return true;
}
//check_new_user: check that the checkout registration dialogue has been populated
function check_new_user(){
    var el=get_el("txt_email");
    if (el.value.length==0){
        alert("please enter the email address you wish to use with this service!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!string_syntax(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
    var el=get_el("opt_terms");
    if (el.checked!=true){
        alert("You must indicate acceptance of our terms and conditions of sale before proceeding!");
        el.focus();
        return false;
        
    }
    return true;

}

//check_returning: check that the checkout 'login' dialogue has been populated correctly
function check_returning(){
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter your email address!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!string_syntax(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
    var el=get_el("txt_password");
    if (el.value.length==0){
        alert("please enter your password!");
        el.value="";
        el.focus()
        return false;
        
    }
    if (el.value.length > 12) {
        alert("Your password can be no longer than 12 characters in length!");
        el.value="";
        el.focus()
        return false;
    }
    if (el.value.length < 6) {
        alert("Your password must be at least 6 characters long!");
        el.value="";
        el.focus()
        return false;
    }
    
    var el=get_el("opt_terms");
    if (el.checked!=true){
        alert("You must indicate acceptance of our terms and conditions of sale before proceeding!");
        el.focus();
        return false;
        
    }
    return true;

}

//cancellation: checks the 'cancellation comment' form - which is an optional feedback form 
function cancellation(){
//    var el=get_el("txt_subject");
//    if (el.value.length==0){
//        alert("please complete the form if you wish to submit your feedback!");
//        el.focus();
//        return false;
//    }
    //txt_detail
    var el=get_el("txt_detail");
    if (el.value.length==0){
        alert("please complete the form if you wish to submit your feedback!");
        el.focus();
        return false;
    }
    
    return true;

}


//print_order: opens a new window ready for printing an order
function print_order(){
    var address='../window.asp?z=print_order';
    var window_width=0;
    var window_height=0;
    var window_top=0;
    var window_left=0;
    
    // set width, height and positioning properties based on context
            window_width=600;
            window_height=600;
            window_top=0;
            window_left=0;
    var use_default=0; // over-ride default properties
    var k=open_new_window(address, 'order', use_default, window_width,window_height,window_top,window_left);
    return false;
}





//string_syntax: validates a string input
function string_syntax(svalue, bmail){
    var str=svalue;
    var sinvalid="!,£,\$,%,',\&";
    
    if (bmail==true){
    	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
    }
    else{
    	var reg1 = /(\.\.)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /([a-zA-Z0-9])|([a-zA-Z])|([0-9])/; // valid
    
    }
    if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    	return true;
    	}
return false;

}

//toggle_other: show / hide input box for other topup amount
function toggle_other(){
//alert("here")
    $('div_other').setStyle=('display','block');
    $('div_other').setStyle=('visibility','visible');
}

//check_ivr: ensure that form is complete
function check_ivr(){
    var el=get_el("txt_adc");
    if (el.value.length==0){
        alert("please enter the ADC (in car display unit number)!");
        el.focus();
        return false;
    }
    if (isNaN(el.value)){
        alert("please enter only numeric values for the ADC (in car display unit number)!");
        el.value="";
        el.focus();
        return false;
    }
    var el=get_el("txt_cell");
    if (el.value.length==0){
        alert("please enter the cell phone number!");
        el.focus();
        return false;
    }
    if (isNaN(el.value)){
        alert("please enter only numeric values for the cell phone number!");
        el.value="";
        el.focus();
        return false;
    }

    return true;
}


//volume_purchasing: test that required fields are completed
function volume_purchasing(){
    var el=get_el("txt_email");
    if (el.value.length==0){
        alert("To help us assist you, please enter your email address!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!string_syntax(s,true)){
        alert("the email address you have entered is not supported on this system, please re-check your entry!");
        el.focus();
        return false;
    
    }
    var el=get_el("txt_name");
    if (el.value.length==0){
        alert("To help us assist you, please enter your name!");
        el.focus();
        return false;
    }
    
    var el=get_el("txt_telephone");
    if (el.value.length==0){
        alert("To help us assist you, please enter a contact telephone number!");
        el.focus();
        return false;
    }
    return true;
}
//check_topup: checks that user has either selected a valid top up amount or entered a valid amount
function check_topup(){
    var value="";
    var el=get_el("opt_amount");
//    alert(el.length)
    for (var i=0; i<el.length;i++){
        if(el[i].checked){
            if(el[i].value=="0"){
                var t=get_el("txt_other");
                if (t.value.length==0){
                    alert("please specify the amount by which you wish to top up!");
                    t.focus();
                    return false;
                }
                if (isNaN(t.value)){
                    alert("please enter only numeric values for the amount by which you wish to top up!");
                    t.value=""
                    t.focus();
                    return false;
                }
                var n=new Number(t.value);
                if (n < 25) {
                    alert("25 dollars is the minimum top up value!");
                    t.value=""
                    t.focus();
                    return false;
                
                }
                value=t.value;
            }
            else{
                value=el[i].value;
            }
      }
    }
    if (value.length!=0){
        var el=get_el("amount");
        el.value=value;
        return true;
    }
    return false;
}

function parkmagic_delegate(){
    // first name
    var el=get_el("txt_firstname");
    if (el.value.length==0){
        alert("please enter the delegate's first name!");
        el.focus();
        return false;
    }
    // surname
    var el=get_el("txt_surname");
    if (el.value.length==0){
        alert("please enter the delegate's family name!");
        el.focus();
        return false;
    }
    // username /email
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter the delegate's  email address (this is used for logging on)!");
        el.focus();
        return false;
    }
    var m = el.value;
    if (!string_syntax(m,true)){
        alert("the email address that you have entered contains characters that are not supported by our system please contact technical support!");
        el.focus();
        return false;
    
    }
    // confirm email address
    var el_confirm=get_el("txt_confirm");
    if (el_confirm.value != m){
        alert("the delegate's email address is critical for all actions on the web site, please confirm your email address");
        
        el_confirm.focus();
        return false;
        
    }
return true


}

//parkmagic_customer: validates that email address has been entered correctly and that the confirm email option matches
function parkmagic_customer(){
    // username /email
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter the customer's  email address (this is used for logging on)!");
        el.focus();
        return false;
    }
    var m = el.value;
    if (!string_syntax(m,true)){
        alert("the email address that you have entered contains characters that are not supported by our system please contact technical support!");
        el.focus();
        return false;
    
    }
    // confirm email address
    var el_confirm=get_el("txt_confirm");
    if (el_confirm.value != m){
        alert("the customer's email address is critical for all actions on the web site, please confirm the email address");
        
        el_confirm.focus();
        return false;
        
    }
    
    var el=get_el("txt_password");
    var el_confirm=get_el("txt_confirm_password");
    if (el.value.length==0){
        alert("please enter a password that is at least 6 characters long!");
        el.value="";
        el_confirm.value="";
        el.focus()
        return false;
        
    }
    if (el.value.length > 12) {
        alert("please enter a password that is no longer than 12 characters in length!");
        el.value="";
        el_confirm.value="";
        el.focus()
        return false;
    }
    if (el.value.length < 6) {
        alert("please enter a password that is at least 6 characters long!");
        el.value="";
        el_confirm.value="";
        el.focus()
        return false;
    }
    var m=el.value;
    if (!string_syntax(m,false)){
        alert("the password that you have entered contains invalid characters, only letters and numbers are supported!");
        el.value="";
        el_confirm.value="";
        el.focus();
        return false;
    }
    if (!password_conforms(m)){
        alert("the password that you enter must contain at least one letter and number!");
        el.value="";
        el_confirm.value="";
        el.focus();
        return false;
    
    }
    var c = el_confirm.value;
    if (c!=m){
        alert("the password and confirm password values that you have entered do not match, please re-enter both values!");
        el_confirm.value="";
        el.value="";
        el.focus();
        return false;
    
    }
    
return true


}


