/**
 * site_functions_shopping_cart.js - JavaScript functions for Shopping Cart Functionality.
 * @author OSD :: Victor Leonard
 * @copyright 2005 - 2009 Victor Leonard
 */

/**
* Check item selection.
*/
function check_valid_product() { 
    // Check for selected item(s)
	if ($("#os_qty").val() == "") { 
		alert("You have not entered a Quantity for '"+$("#p_name").val()+"' to add to your Basket.");
	}
	else { 
		$("#form").submit();
	}
}

/**
* Check current quantity.
*/
function check_qty(qty) { 
    if (qty == '0') { 
        alert('You have entered 0 as a quantity.\n\nIf you want 0 of this item - click "Remove".');
    }
    else { 
        $("#qty_change").val('true');
    }
}

/**
* Update the selected items from the Shopping Cart.
*/
function update_selected() { 
    // Check for selected item(s)
	if ($("#qty_change").val() == 'false') { 
		alert('You have not modified any quantities to update your Shopping Basket.\n\n\If you have an item to change to quantity 0 - click "Remove".');
		//return false;
	}
	else { 
		$("#cart [@name='operation']").val('update_items');
		//alert($("#cart [@name='operation']").val());
		$("#cart").submit();
		//return true;
	}
}

/**
* Delete the selected items from the Shopping Cart.
*/
function delete_selected() { 
    // Check for selected item(s)
	if ($("#del_select").val() == 'false') { 
		alert('Please select an item in your Shopping Cart to delete.');
		return false;
	}
	else { 
		$("#cart [@name='operation']").val('delete_items');
		return true;
	}
}

/**
* Clear shopping Shopping Cart and start with a new cart.
*/
function proceed_new_cart() { 
    if (!confirm('You are about to start with a new Shopping Basket!\n\n\'OK\' to Continue!\n\n\'Cancel\' to keep!\n')) {
		return false;
	}
	$("#cart [@name='operation']").val('new_cart');
	$("#cart").submit();
	//return true;
}

/**
* Proceed to Checkout.
*/
function proceed_checkout() { 
    if ($("#select_country").val() == '') { 
        alert('Please select a Country to deliver to.');
        $("#select_country").focus();
        return false;
    }
    else { 
        //alert($("#country_id").val());
        $("#country_id").val($("#select_country").val());
        //alert($("#country_id").val());
        return true;
    }
}

/**
* Check customer details okay.
*/
function check_customer_details(){ 
	//return false;
	var email_val = $("#o_email").val();
	var email_confirm_val = $("#o_email_confirm").val();
	pass = true;
	details = 'Invalid information entered.\n\n';

	var o_date_delivery_array = $("#o_date_delivery").val().split("-");
	var o_date_wedding_array = $("#o_date_wedding").val().split("-");

	var o_date_delivery = new Date();
	o_date_delivery.setFullYear(o_date_delivery_array[2], (o_date_delivery_array[1]-1), o_date_delivery_array[0]);

	var o_date_wedding = new Date();
	o_date_wedding.setFullYear(o_date_wedding_array[2], (o_date_wedding_array[1]-1), o_date_wedding_array[0]);

	//alert("delivery field : "+$("#o_date_delivery").val()+"\n\n"+o_date_delivery_array[2]+""+(o_date_delivery_array[1] - 1)+""+o_date_delivery_array[0]+"\n\nwedding field : "+$("#o_date_wedding").val()+"\n\n"+o_date_wedding_array[2]+""+(o_date_wedding_array[1]-1)+""+o_date_wedding_array[0]);
	//var test = ((o_date_delivery < o_date_wedding) ? "OKAY" : "NOT OKAY");
	//alert(test+"\n\no_date_delivery: "+o_date_delivery+"\n\no_date_wedding: "+o_date_wedding);

	if ($("#o_firstname").val() == '') { 
		details+= '- Please enter your First Name.\n\n';
    	if (pass) $("#o_firstname").focus();
		pass = false;
	}
	if ($("#o_lastname").val() == '') { 
		details+= '- Please enter your Last Name.\n\n';
    	if (pass) $("#o_lastname").focus();
		pass = false;
	}
	if (email_val == '') { 
		details+= '- Please enter an Email Address.\n\n';
    	if (pass) $("#o_email").focus();
		pass = false;
	}
	else if (email_val.indexOf("@") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) $("#o_email").focus();
		pass = false;
	}
	else if (email_val.indexOf(".") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) $("#o_email").focus();
		pass = false;
	}
	if (email_confirm_val == '') { 
		details+= '- Please confirm your Email Address.\n\n';
    	if (pass) $("#o_email_confirm").focus();
		pass = false;
	}
	if (email_val != email_confirm_val) { 
		details+= '- Email Address and Confirm Email Address Do Not Match.\n\n';
    	if (pass) $("#o_email_confirm").focus();
		pass = false;
	}
	if ($("#o_phone").val() == '') { 
		details+= '- Please enter your Phone Number.\n\n';
    	if (pass) $("#o_phone").focus();
		pass = false;
	}
	if ($("#o_address1").val() == '') { 
		details+= '- Please enter your Address Line 1.\n\n';
    	if (pass) $("#o_address1").focus();
		pass = false;
	}
	if ($("#o_city").val() == '') { 
		details+= '- Please enter your City.\n\n';
    	if (pass) $("#o_city").focus();
		pass = false;
	}
	if ($("#o_county").val() == '') { 
		details+= '- Please enter your County.\n\n';
    	if (pass) $("#o_county").focus();
		pass = false;
	}
	if ($("#o_country_id").val() == '') { 
		details+= '- Please select your Country.\n\n';
    	if (pass) $("#o_country_id").focus();
		pass = false;
	}
	if ($("#o_d_firstname").val() == '') { 
		details+= '- Please enter the Delivery First Name.\n\n';
    	if (pass) $("#o_d_firstname").focus();
		pass = false;
	}
	if ($("#o_d_lastname").val() == '') { 
		details+= '- Please enter the Delivery Last Name.\n\n';
    	if (pass) $("#o_d_lastname").focus();
		pass = false;
	}
	if ($("#o_d_address1").val() == '') { 
		details+= '- Please enter the Delivery Address Line 1.\n\n';
    	if (pass) $("#o_d_address1").focus();
		pass = false;
	}
	if ($("#o_d_city").val() == '') { 
		details+= '- Please enter the Delivery City.\n\n';
    	if (pass) $("#o_d_city").focus();
		pass = false;
	}
	if ($("#o_d_county").val() == '') { 
		details+= '- Please enter the Delivery County.\n\n';
    	if (pass) $("#o_d_county").focus();
		pass = false;
	}
	if ($("#o_d_country_id").val() == '') { 
		details+= '- Please select the Delivery Country.\n\n';
    	if (pass) $("#o_d_country_id").focus();
		pass = false;
	}
	//alert("del"+$("#o_date_delivery").val()+" wed"+$("#o_date_wedding").val());
	if ($("#o_date_delivery").val() == "DD-MM-YYYY") { 
		details+= '- Please select a Delivery Date.\n\n';
    	if (pass) $("#o_date_delivery").focus();
		pass = false;
	}
	if ($("#o_date_wedding").val() == "DD-MM-YYYY") { 
		details+= '- Please select a Wedding Date.\n\n';
    	if (pass) $("#o_date_wedding").focus();
		pass = false;
	}
	if (o_date_delivery >= o_date_wedding) { 
		details+= '- Please select a Delivery Date before your Wedding Date.\n\n';
    	if (pass) $("#o_date_delivery").focus();
		pass = false;
	}
	if (!$('#o_confirm').attr('checked')) { 
		details+= '- Please confirm you have read and agreed to Terms and Conditions.\n\n';
    	if (pass) $("#o_confirm").focus();
		pass = false;
	}
	if (pass) { 
		return true;
	}
	else { 
		details+= 'Please correct the above errors before proceeding.    \n\n';
		alert(details);
		return false;
	}
}

/**
* Proceed to Complete Order.
*/
function proceed_complete() { 
    $("#cart_complete").submit();
}

