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

/**
* Check item selection.
*/
function check_item_selection(qty) { 
    var p_id = $("input[@name='p_id']:checked").val();
    var p_id_s = $("input[@name='p_id_s_"+p_id+"']").val();
    p_id_s = ((p_id_s != undefined) ? p_id_s : "N/A");
    //alert("Selected Product ID : "+p_id+"\nOptional Support ID : "+p_id_s);
    $("input[name^='p_id_s_']").attr("checked","");
    $("input[name^='p_id_s_']").attr("disabled","disabled");
    $("input[name^='p_id_s_"+p_id+"']").removeAttr("disabled");
}

/**
* 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 - tick the checkbox to the left of the item name and click "Delete Selected".');
    }
    else { 
        $("#qty_change").val('true');
    }
}

/**
* Check item selection.
*/
function add_product() { 
    var p_id = $("input[@name='p_id']:checked").val();
    // Check for selected item(s)
	if (p_id == undefined) { 
		alert('You have not selected a Product to add.');
		return false;
	}
	else { 
		return 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 Cart.\n\n\If you have an item to change to quantity 0 - tick the checkbox to the left of the item name and click "Delete Selected".');
		return false;
	}
	else { 
		$("#cart [@name='operation']").val('update_items');
		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 Cart!\n\n\'OK\' to Continue!\n\n\'Cancel\' to keep!\n')) {
		return false;
	}
	$("#cart [@name='operation']").val('new_cart');
	return true;
}

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

	if ($("#o_title").val() == '') { 
		details+= '- Please enter your Title.\n\n';
    	if (pass) $("#o_title").focus();
		pass = false;
	}
	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 ($("#o_company").val() == '') { 
		details+= '- Please enter your Company Name.\n\n';
    	if (pass) $("#o_company").focus();
		pass = false;
	}
	if ($("#o_company_reg").val() == '') { 
		details+= '- Please enter your Company Reg Number.\n\n';
    	if (pass) $("#o_company_reg").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_address2").val() == '') { 
		details+= '- Please enter your Address Line 2.\n\n';
    	if (pass) $("#o_address2").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 (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();
}
