/* 
cws_commerce.js: library of javascript for managing shopping basket
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
*/
 // check_basket: validate shopping basket before posting
function check_basket(param_checkout){
    var buffer=$('#items').val();
    var array_items=buffer.split(",");
    var qty=""
    for (var i=0;i<array_items.length;i++){
        var q=$('#quantity' + array_items[i]).val();
        if (q.length==0){
            show_error_message("Please enter a valid quantity!",'error_message_panel');
            $('#quantity' + array_items[i]).focus();
            return false;
        }
        if (isNaN(q)){
            show_error_message("Please enter only whole numbers in the quantity!",'error_message_panel');
            $('#quantity' + array_items[i]).focus();
            return false;
        }
        if (parseInt(q)==0){
            show_error_message("Please enter a valid quantity, to remove an item click on the red 'x' !",'error_message_panel');
            $('#quantity' + array_items[i]).focus();
            return false;
        
        }
        if (qty.length!=0){
            qty+=',' 
        }
        qty+= q
    }
    var f=0;
    if (param_checkout==0){
        $('#uids').val(buffer);
        $('#qty').val(qty);
        $('#forcollection').val(f);
    }
    else{
        $('#checkuids').val(buffer);
        $('#checkqty').val(qty);
        $('#checkforcollection').val(f);
    }
    //need to confirm that the customer has calculated shipping correctly
    if ((parseInt($('#basket_delivery_country').val())==0)||(parseInt($('#basket_billing_country').val())==0)){
        alert("Please ensure that you have selected your billing and shipping country!");
        calculator(1);
        return false;
    }
    
    return true;

}

//empty_basket: double check that user wants to empty their shopping basket
function empty_basket(){
    if (confirm("Are you sure you want to delete your shopping basket?")){
        return true;
    }
    return false;
}

 
//calculator: facilitate selection of billing & destination country to enable calculation of freight and tax
function calculator(param_scenario){
    var url=$('#u_base').val() + "calculator-z-_calculator.htm"
    //there are two scenarios by which this function is called; either ON the basket manager page or else via the basket summary panel
    switch(parseInt(param_scenario)){
        case 0: //executing from basket summary
            break;
        case 1: //via the basket manager page
            if ($('div.shipping_calculator').length==0){
                $('#ajax_progress_bar').show();
                $("#shipping_selector").load(url, function(){
                        $('#ajax_progress_bar').hide();
                        $('#shipping_selector').show('slow');
                    });
            }
            else{
                
                        $('#shipping_selector').toggle('slow');
            }            
    }
    
}

//tax_and_shipping: updates hidden fields on basket manager form to match users selection of destination & billing countries
function tax_and_shipping(){
    $('#basket_delivery_country').val($('#list_delivery_country').val());
    $('#basket_billing_country').val($('#list_billing_country').val());
    $('#keep_delivery_country').val($('#list_delivery_country').val());
    $('#keep_billing_country').val($('#list_billing_country').val());
    $('#recalculate_delivery_country').val($('#list_delivery_country').val());
    $('#recalculate_billing_country').val($('#list_billing_country').val());
    $('#checkout_delivery_country').val($('#list_delivery_country').val());
    $('#checkout_billing_country').val($('#list_billing_country').val());

}

//reset_billing_country: triggered if consumer changes their billing country (requires that we re-confirm the billing address
function reset_billing_country(){
    if (!confirm('Are you sure you want to change your billing country?')){
        //reset list back to original selection
        //o_country== original country id
        for (var i=0;i<=$('#billing_country').options.length;i++){
            if (parseInt($('#billing_country').options[i].val())==parseInt($('#obilling_country').val())){
                $('#billing_country').selectedIndex=i;
                return void[0];
            }
        }
    }
    
    //repopulate address box to reflect change to billing country
    var d=new Date();
    var url=$('#u_base').val() + 'asp/fanore.asp?z=_billing_address&country=' + $('#billing_country').val() + '&account=' + $('#billing_id').val() + '&d=' + d 
    
    //window.location.href=url
    $('#billing_address_area').html($('#ajax_progress_bar').html())
    $("#billing_address_area").load(url,'',function(responseText){
        $('#billing_address_area').html(responseText);
        //alert(responseText);
        
        });
    
}


//reset_delivery_country: triggered if consumer changes their delivery country (requires that we re-confirm the delivery address
function reset_delivery_country(){
    if (!confirm('Are you sure you want to change your shipping country?')){
        //reset list back to original selection
        //o_country== original country id
        for (var i=0;i<=$('#delivery_country').options.length;i++){
            if (parseInt($('#delivery_country').options[i].val())==parseInt($('#odelivery_country').val())){
                $('#delivery_country').selectedIndex=i;
                return void[0];
            }
        }
    }
     //repopulate address box to reflect change to billing country
    var d=new Date();
    var url=$('#u_base').val() + 'asp/fanore.asp?z=_delivery_address&country=' + $('#delivery_country').val() + '&account=' + $('#delivery_id').val() + '&d=' + d 
    
    //window.location.href=url
    $('#delivery_address_area').html($('#ajax_progress_bar').html())
    $("#delivery_address_area").load(url,'',function(responseText){
        $('#delivery_address_area').html(responseText);
        //alert(responseText);
        
        });
   
    

}

//validate_customer_login: check customer login dialogue
function validate_customer_login(param_context){
    //email address
    $('#user_name').val(jQuery.trim($('#user_name').val()));
    if ($('#user_name').val().length==0){
        alert("Please enter your email address!");
        $('#user_name').focus();
        return false;
    }
    var e=$('#user_name').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#user_name').focus();
        return false;
    
    }
    //password
    $('#user_pass').val(jQuery.trim($('#user_pass').val()));
    if ($('#user_pass').val().length==0){
        alert("Please enter your password!");
        $('#user_pass').focus();
        return false;
    }
    $('#login_details').val('');
    return true;         
    
}

//validate_registration_form: check that form populated correctly
function validate_registration_form(){
    //email address
    $('#txt_email').val(jQuery.trim($('#txt_email').val()));
    if ($('#txt_email').val().length==0){
        alert("Please enter your email address!");
        $('#txt_email').focus();
        return false;
    }
    var e=$('#txt_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#txt_email').focus();
        return false;
    
    }
    if ($('#billing_country').val().length==0){
         alert("Please select your country of residence!");
        $('#billing_country').focus();
        return false;
    }
    if (parseInt($('#billing_country').val())==0){
         alert("Please select your country of residence from the sub-menu!");
        $('#billing_country').focus();
        return false;
    
    }
    //alert($('#registration_details').val());
    $('#registration_details').val('');
    return true;     
}

//billing: attach click event to checkboxes
function billing(){
    if ($('#billing').length){
        $("#delivery").bind("click", function(event){
            if ($('#delivery').attr('checked')==true){
                $('#same').val('1');
            }
            else{
                $('#same').val('0');
            }
        });
        $("#newsletteroption").bind("click", function(event){
            if ($('#newsletteroption').attr('checked')==true){
                $('#newsletter').val('1');
            }
            else{
                $('#newsletter').val('0');
            }
        });
        
    }
    return void[0];
}

//validate_billing_form: check that we have all required information for billing
function validate_billing_form(){
    var require_zip=parseInt($('#require_zip').val())
    var require_county=parseInt($('#require_county').val())
//alert(require_zip)
//alert(require_county)
    //txt_name
    if ($('#txt_name').val().length==0){
        alert("Please enter your full name!");
        $('#txt_name').focus();
        return false;
    }
    
    //telephone
    var telephone=fix_telephone($('#telephone').val());
    var mobile=fix_telephone($('#mobile').val());
    $('#telephone').val(telephone);
    $('#mobile').val(mobile);
    
    
    if (($('#telephone').val().length==0)&&($('#mobile').val().length==0)){
        alert("Please enter either a landline or mobile number!");
        $('#telephone').focus();
        return false;
    }

    //email
    $('#email').val(jQuery.trim($('#email').val()));
    if ($('#email').val().length==0){
        alert("Please enter your email address!");
        $('#email').focus();
        return false;
    }
    var e=$('#email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#email').focus();
        return false;
    }
    //o_email
    $('#o_email').val(jQuery.trim($('#o_email').val()));
     var o=$('#o_email').val();
     if (o.toLowerCase() != e.toLowerCase()){
        if (!confim('Please confirm that you wish to change your email address')){
           $('#email').val(o); 
        }
     }
    //address1: house address
    if ($('#address1').val().length==0){
        alert("Please enter your full address!");
        $('#address1').focus();
        return false;
    }
    
    //address2: street / town / village
    if ($('#address2').val().length==0){
        alert("Please enter your full address!");
        $('#address2').focus();
        return false;
    }
    if ((require_zip==1)&&(require_county==1)){
        //address3: county
        if ($('#address3').val().length==0){
            alert("Please select your county from the list provided!");
            $('#address3').focus();
            return false;
        }
        //address4: zip/postcode
        if ($('#address4').val().length==0){
            alert("Please enter your postcode!");
            $('#address4').focus();
            return false;
        }
    }
    if ((require_zip==1)&&(require_county==0)){
        //address4: zip/postcode
        if ($('#address4').val().length==0){
            alert("Please enter your postcode!");
            $('#address4').focus();
            return false;
        }
    }
    if ((require_zip==0)&&(require_county==1)){
    
        //address4: zip/postcode
        if ($('#address4').val().length==0){
            alert("Please select your county from the list provided!");
            $('#address4').focus();
            return false;
        }
    
    }
    
    
    //billing_country
    if ($('#billing_country').val().length==0){
         alert("Please select your country of residence!");
        $('#billing_country').focus();
        return false;
    }
    if (parseInt($('#billing_country').val())==0){
         alert("Please select your country of residence from the sub-menu!");
        $('#billing_country').focus();
        return false;
    
    }
    $('#billing_details').val('');
    return true;
}




//validate_billingshipping_form: check that we have all required information for billing
function validate_billingshipping_form(){
    var billing_require_zip=parseInt($('#billing_require_zip').val())
    var billing_require_county=parseInt($('#billing_require_county').val())
    var delivery_require_zip=parseInt($('#delivery_require_zip').val())
    var delivery_require_county=parseInt($('#delivery_require_county').val())
    
    //billing_name
    if ($('#billing_name').val().length==0){
        alert("Please enter your full name!");
        $('#billing_name').focus();
        return false;
    }
    //billing_country
    if ($('#billing_country').val().length==0){
         alert("Please select your country of residence!");
        $('#billing_country').focus();
        return false;
    }
    if (parseInt($('#billing_country').val())==0){
         alert("Please select your country of residence from the sub-menu!");
        $('#billing_country').focus();
        return false;
    
    }
    
    //address1: house address
    if ($('#billing_address1').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address1').focus();
        return false;
    }
    
    //address2: street / town / village
    if ($('#billing_address1').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address1').focus();
        return false;
    }
    if ((billing_require_zip==1)&&(billing_require_county==1)){
        //address3: county
        if ($('#billing_address3').val().length==0){
            alert("Please select your county from the list provided!");
            $('#billing_address3').focus();
            return false;
        }
        //address4: zip/postcode
        if ($('#billing_address4').val().length==0){
            alert("Please enter your postcode!");
            $('#billing_address4').focus();
            return false;
        }
    }
    if ((billing_require_zip==1)&&(billing_require_county==0)){
        //address4: zip/postcode
        if ($('#billing_address4').val().length==0){
            alert("Please enter your postcode!");
            $('#billing_address4').focus();
            return false;
        }
    }
    if ((billing_require_zip==0)&&(billing_require_county==1)){
    
        //address4: zip/postcode
        if ($('#billing_address4').val().length==0){
            alert("Please select your county from the list provided!");
            $('#billing_address4').focus();
            return false;
        }
    
    }
    //telephone
    var telephone=fix_telephone($('#billing_telephone').val());
    var mobile=fix_telephone($('#billing_mobile').val());
    $('#billing_telephone').val(telephone);
    $('#billing_mobile').val(mobile);
    
    
    if (($('#billing_telephone').val().length==0)&&($('#billing_mobile').val().length==0)){
        alert("Please enter either a landline or mobile number!");
        $('#billing_telephone').focus();
        return false;
    }

    //email
    $('#billing_email').val(jQuery.trim($('#billing_email').val()));
    if ($('#billing_email').val().length==0){
        alert("Please enter your email address!");
        $('#billing_email').focus();
        return false;
    }
    var e=$('#billing_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#billing_email').focus();
        return false;
    }
    //o_email
    $('#obilling_email').val(jQuery.trim($('#obilling_email').val()));
     var o=$('#obilling_email').val();
     if (o.toLowerCase() != e.toLowerCase()){
        if (!confim('Please confirm that you wish to change your email address')){
           $('#billing_email').val(o); 
        }
     }
    
    
    
//validate shipping details    
    //delivery_name
    if ($('#delivery_name').val().length==0){
        alert("Please enter the name of the person to whom this is being shipped!");
        $('#delivery_name').focus();
        return false;
    }
    
    //delivery_country
    if ($('#delivery_country').val().length==0){
         alert("Please select the shipping country!");
        $('#delivery_country').focus();
        return false;
    }
    if (parseInt($('#delivery_country').val())==0){
         alert("Please select  the shipping country from the list provided!");
        $('#delivery_country').focus();
        return false;
    
    }

    //address1: house address
    if ($('#delivery_address1').val().length==0){
        alert("Please enter the full shipping address!");
        $('#delivery_address1').focus();
        return false;
    }
    
    //address2: street / town / village
    if ($('#delivery_address2').val().length==0){
        alert("Please enter the full shipping address!");
        $('#delivery_address2').focus();
        return false;
    }
    if ((delivery_require_zip==1)&&(delivery_require_county==1)){
        //address3: county
        if ($('#delivery_address3').val().length==0){
            alert("Please select the county from the list provided!");
            $('#delivery_address3').focus();
            return false;
        }
        //address4: zip/postcode
        if ($('#delivery_address4').val().length==0){
            alert("Please enter the postcode!");
            $('#delivery_address4').focus();
            return false;
        }
    }
    if ((delivery_require_zip==1)&&(delivery_require_county==0)){
        //address4: zip/postcode
        if ($('#delivery_address4').val().length==0){
            alert("Please enter the postcode!");
            $('#delivery_address4').focus();
            return false;
        }
    }
    if ((delivery_require_zip==0)&&(delivery_require_county==1)){
    
        //address4: zip/postcode
        if ($('#delivery_address4').val().length==0){
            alert("Please select the county from the list provided!");
            $('#delivery_address4').focus();
            return false;
        }
    
    }
    
    
    var telephone=fix_telephone($('#delivery_telephone').val());
    $('#delivery_telephone').val(telephone);
    if ($('#delivery_telephone').val().length==0){
        alert("Please enter either a landline or mobile number!");
        $('#delivery_telephone').focus();
        return false;
    }
    
    
    $('#billing_details').val('');
    return true;
}

//copy_billing: copy billing infromation to shipping address
function copy_billing(){
    //shipping country
    var c=$('#billing_country').val()
    var d=$('#delivery_country').val()
    $('#delivery_country').val($('#billing_country').val())
    if (parseInt(c) != parseInt(d)){
        //country has changed
    var d=new Date();
    var url=$('#u_base').val() + 'asp/fanore.asp?z=_delivery_address&country=' + $('#delivery_country').val() + '&account=' + $('#delivery_id').val() + '&d=' + d 
    
    //window.location.href=url
    $('#delivery_address_area').html($('#ajax_progress_bar').html())
    $("#delivery_address_area").load(url,'',function(responseText){
        $('#delivery_address_area').html(responseText);
        //reset remaining details
        $('#delivery_name').val($('#billing_name').val())
        $('#delivery_address1').val($('#billing_address1').val())
        $('#delivery_address2').val($('#billing_address2').val())
        $('#delivery_address3').val($('#billing_address3').val())
        $('#delivery_address4').val($('#billing_address4').val())
        $('#delivery_telephone').val($('#billing_telephone').val())
        
                
        });
        
    
    }
    else{
    //address1
        $('#delivery_name').val($('#billing_name').val())
        $('#delivery_address1').val($('#billing_address1').val())
        $('#delivery_address2').val($('#billing_address2').val())
        $('#delivery_address3').val($('#billing_address3').val())
        $('#delivery_address4').val($('#billing_address4').val())
        $('#delivery_telephone').val($('#billing_telephone').val())
    //address2
    
    }
}













//validate_delivery_form: checks the shipping address before submitting
function validate_delivery_form(){
    var require_zip=parseInt($('#require_zip').val())
    var require_county=parseInt($('#require_county').val())
//alert(require_zip)
//alert(require_county)
    //txt_name
    if ($('#txt_name').val().length==0){
        alert("Please enter the name of the person to whom this is being shipped!");
        $('#txt_name').focus();
        return false;
    }
    
    //address1: house address
    if ($('#address1').val().length==0){
        alert("Please enter the full shipping address!");
        $('#address1').focus();
        return false;
    }
    
    //address2: street / town / village
    if ($('#address2').val().length==0){
        alert("Please enter the full shipping address!");
        $('#address2').focus();
        return false;
    }
    if ((require_zip==1)&&(require_county==1)){
        //address3: county
        if ($('#address3').val().length==0){
            alert("Please select the county from the list provided!");
            $('#address3').focus();
            return false;
        }
        //address4: zip/postcode
        if ($('#address4').val().length==0){
            alert("Please enter the postcode!");
            $('#address4').focus();
            return false;
        }
    }
    if ((require_zip==1)&&(require_county==0)){
        //address4: zip/postcode
        if ($('#address4').val().length==0){
            alert("Please enter the postcode!");
            $('#address4').focus();
            return false;
        }
    }
    if ((require_zip==0)&&(require_county==1)){
    
        //address4: zip/postcode
        if ($('#address4').val().length==0){
            alert("Please select the county from the list provided!");
            $('#address4').focus();
            return false;
        }
    
    }
    
    
    //delivery_country
    if ($('#delivery_country').val().length==0){
         alert("Please select the shipping country!");
        $('#delivery_country').focus();
        return false;
    }
    if (parseInt($('#delivery_country').val())==0){
         alert("Please select  the shipping country from the list provided!");
        $('#delivery_country').focus();
        return false;
    
    }
    var telephone=fix_telephone($('#telephone').val());
    $('#telephone').val(telephone);
    if ($('#telephone').val().length==0){
        alert("Please enter either a landline or mobile number!");
        $('#telephone').focus();
        return false;
    }
    
    $('#delivery_details').val('');
    return true;

}

//cancel_order: confirm that user wishes to cancel the order
function cancel_order(){
    if (!confirm("Are you sure you want to cancel this order ?")){
        return false;
    }
    return true;
}

//check_instructions: validate instructions form
function check_instructions(){
    
    $('#instructions_details').val('');
    return true;


}
//change_currency: javascript trigger to submit currency change
function change_currency(){
    $('#currency_selector').submit();
}
