/*
basketOperations.js

Library to handle shopping basket validation & events
*/

function checkBasket(paramCheckingOut){
    var buffer=$('#items').val();
//    alert(buffer);
    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 (paramCheckingOut==0){
        $('#uids').val(buffer);
        $('#qty').val(qty);
        $('#forcollection').val(f);
    }
    else{
        $('#checkuids').val(buffer);
        $('#checkqty').val(qty);
        $('#checkforcollection').val(f);
    }
    

    
    
    
    return true;

}


//calculator: pop-up a dialogue to facilitate calculation of freight & tax (where applicable)
function calculator(){
    var href=$('#u_base').val() + 'calculator-z-calculator.htm';
    window.location.href=href;
}



//checkCustomerLogin: validate the returning customer login dialogue before posting
function checkCustomerLogin(){
 //user_name
   $('#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;
    }
   
   
 //user_pass
   $('#user_pass').val(jQuery.trim($('#user_pass').val()));
    if ($('#user_pass').val().length==0){
        alert("Please enter your password!");
        $('#user_pass').focus();
        return false;
    }
     if ($('#user_pass').val().length > 10){
        alert("Please check your password!");
        $('#user_pass').focus();
        return false;
    }

 //login_details   
    $('#login_details').val('');
    return true;
}

//validateRegistration: validate the new customer registration dialogue before proceeding
function validateRegistration(){

   //your_name
   $('#your_name').val(jQuery.trim($('#your_name').val()));
    if ($('#your_name').val().length==0){
        alert("Please enter your name!");
        $('#your_name').focus();
        return false;
    }

   //your_email
   $('#your_email').val(jQuery.trim($('#your_email').val()));
    if ($('#your_email').val().length==0){
        alert("Please enter your email address!");
        $('#your_email').focus();
        return false;
    }
    var e=$('#your_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#your_email').focus();
        return false;
    }

   //address1
   $('#address1').val(jQuery.trim($('#address1').val()));
    if ($('#address1').val().length==0){
        alert("Please enter your full address!");
        $('#address1').focus();
        return false;
    }
   
   //address1
   $('#address1').val(jQuery.trim($('#address1').val()));
    if ($('#address1').val().length==0){
        alert("Please enter your full address!");
        $('#address1').focus();
        return false;
    }
   
   //address2
   $('#address2').val(jQuery.trim($('#address2').val()));
    if ($('#address2').val().length==0){
        alert("Please enter your full address!");
        $('#address2').focus();
        return false;
    }
   
   //countryid
   if ($('#countryid').attr('type')=='select-one'){//implies that address 4 is a numeric value > 0
        if (parseInt($('#countryid').val())==0){
            alert("Please select a country from the list provided!");
            $('#countryid').focus();
            return false;
        }
   }
   
    //address4
   //for international addresses this may be either a string or a list box
   if ($('#address4').attr('type')=='select-one'){//implies that address 4 is a numeric value > 0
        if (parseInt($('#address4').val())==0){
            alert("Please select a state / county from the list provided!");
            $('#address4').focus();
            return false;
        }
   }
   
   
   //telephone 
   $('#telephone').val(fix_telephone(jQuery.trim($('#telephone').val())));
    if ($('#telephone').val().length==0){
        alert("Please enter your telephone number!");
        $('#telephone').focus();
        return false;
    }

    $('#registration_details').val('');
    return true;
}



//fix_telephone: removes non-numeric characters from user entry
function fix_telephone(param_value){
    var ret_value='';
    var wip_value=param_value;
    var a_values='1234567890'
    //alert(wip_value.length);
    for (var i=0;i<=wip_value.length;i++){
    //alert(wip_value.charAt(i))
        for (var x=0;x<=a_values.length;x++){
            if (wip_value.charAt(i)==a_values.charAt(x)){
                ret_value+=wip_value.charAt(i)
            }
        }
    } 
    //alert(ret_value);
    return ret_value;   
}

//editAddress: popup editing dialogue for 'address'
function editAddress(addressType){
    var href="";
    switch (addressType){
        case 0: //billing information
            href=$('#u_base').val() + 'editinformation-z-editbilling.htm';
            break;
        case 1: //delivery details
            href=$('#u_base').val() + 'editinformation-z-editdelivery.htm';
            break;
        case 2: //special instructions
            href=$('#u_base').val() + 'editinformation-z-editinstructions.htm';
            break;
        case 3: //gift message   
            href=$('#u_base').val() + 'editinformation-z-editgiftmessage.htm';
            break;
    }
    //alert(href);
    //window.location.href=href;
    $('#dialogue').load(href, function(){
        
//display the modal dialogue
    $("#dialogue").overlay({
        	// some expose tweaks suitable for facebox-looking dialogs
            effect: 'apple',
            speed: 'fast',
            
        	expose: {
        		// you might also consider a "transparent" color for the mask
        		color: '#000',
        		// load mask a little faster
        		loadSpeed: 200,
        		// highly transparent
        		opacity: 0.5
        	},
        	// disable this for modal dialog-type of overlays
        	closeOnClick: true,
            close: $('#cancel'),
            onLoad: function(){
                $('#cancel').bind('click',function(e){
                    e.preventDefault();
                                            $("#dialogue").overlay().close();
                }); //end bind to closing element
                applyCSS();
                //bind to the edit form submission
                $('#save').submit(function(){
                    if (validatePopup(addressType)==true){
                        return true;  
                    }
                    return false;
                    
                });
            },//end on load
        	// we want to use the programming API
        	api: true
        // load it immediately after the construction
        }).load();
 });   
return false;



    

}

//emptyBasket: confirm that customer wishes to empty their shopping basket
function emptyBasket(){
    if (!confirm('Are you sure you want to delete your shopping basket?')){
        return false;
    }
    return true;
}

//validatePopup: validate the pop-up dialogue
function validatePopup(addressType){
    switch(addressType){
        case 0: //billing
        case 1: //delivery
    }
    return true;
}
