var defaultSearchStr = 'Enter Keyword or Item #';

jQuery(document).ready(function () {
    
    jQuery.each(jQuery('.addtocartlink'), function (key, value) {
	jQuery(this).unbind("click");
	jQuery(this).click(function (e) {
	    e.preventDefault();
	    
	    var product = jQuery(this).attr("id");
	    var addqty = jQuery('#quantity').val();
	    if (!addqty) addqty = 1
	    var prodname;

	    var matches = product.match(/addtocart(.*)/i);
	    if (matches)
		product = matches[1];
	    if (product)
		prodname = jQuery('#prod' + product).val();
		
	    var rxproduct = jQuery('#rxprod' + product);
	    
	    jQuery('#shoppingcart').html('Your shopping cart is updating...');
	    
	    jQuery.ajax({
		url: "index.pl",
		data: { page: 'scart',
			act: 'scadd',
			qty: addqty,
			product: product},
		cache: false,
		success: function(html){
		jQuery("#shoppingcart").html(html);
		}
	    });
	    
	    var html = '';
	    if (rxproduct.val())
		{
		html = "You are adding a product which requires a doctor's prescription.<br>";
		}
	    html += 'Added <stong>' + prodname + '</strong> to your shopping cart.<br> ' +
		    'Close this window to continue shopping.';
	    
	    var $dialog = $('<div id="shoppingcartdlg"></div>')
		.html(html)
		.dialog({autoOpen: false,
			modal: true,
			title: 'Shopping cart update',
			buttons: {"OK": function() {$(this).dialog("close");}}
			});
	    $dialog.dialog('open');
	    
	    return false;
	});
    });
    
    jQuery.each(jQuery('.scartremovelink'), function (key, value) {
	jQuery(this).click(function(e) {
	    e.preventDefault();
	    var product = jQuery(this).attr("id");
	    
	    var matches = product.match(/remfromcart(.*)/i);
	    if (matches) {
		product = matches[1];
	    }
	    
	    jQuery('#shoppingcart').html('Your shopping cart is updating...');
	    
	    jQuery.ajax({
		url: "index.pl",
		data: { page: 'scart',
			act: 'scremove',
			qty: 1,
			product: product},
		cache: false,
		success: function(html){
		jQuery("#shoppingcart").html(html);
		}
	    });
	});
    });
    
    jQuery.each(jQuery('.deleteorder'), function (key, value) {
	jQuery(this).click(function(e) {
	    e.preventDefault();
	    var corder, order;
	    var orderdetails = jQuery(this).attr("id");
	    
	    var matches = orderdetails.match(/orderno_([0-9]+)_cno_([0-9]+)/i);
	    if (matches) {
		order = matches[1];
		corder = matches[2];
	    }
	    
	    var $dialog = $('<div id="deleteorderdlg"></div>')
		.html('Are you sure that you want to remove SO#' +
		      order + '  from the list?')
		.dialog({autoOpen: false,
			modal: true,
			title: 'Delete Order',
			buttons: {
			    "Yes": function() {
				    jQuery('#del_corder').val(corder);
				    jQuery('#del_so').submit();
				    },   
			    "No": function() {
				    $( this ).dialog( 'close' );
				    }
			    }
			});
	    $dialog.dialog('open');
    
	    return false;
	});
    });
    
    jQuery.each(jQuery('.deleteinvoice'), function (key, value) {
	jQuery(this).click(function(e) {
	    e.preventDefault();
	    var cinvoice, invoice;
	    var invoicedetails = jQuery(this).attr("id");

	    var matches = invoicedetails.match(/invoice_([0-9]+)_inno_([0-9]+)/i);
	    if (matches) {
		cinvoice = matches[1];
		invoice = matches[2];
	    }
	    
	    var $dialog = $('<div id="deleteinvoicedlg"></div>')
		.html('Are you sure that you want to remove Invoice #' +
		      invoice + '  from the list?')
		.dialog({autoOpen: false,
			modal: true,
			title: 'Delete Invoice',
			buttons: {
			    "Yes": function() {
				    jQuery('#del_invoice').val(cinvoice);
				    jQuery('#del_inv').submit();
				    },   
			    "No": function() {
				    $( this ).dialog( 'close' );
				    }
			    }
			});
	    $dialog.dialog('open');
    
	    return false;
	});
    });
    
    jQuery.each(jQuery('.addfavorite, .delfavlink'), function (key, value){
	jQuery(this).click(function (e) {
	    window.location = jQuery(this).attr('href');
	});
    });
    
    jQuery('#swords').click(function (e) {
	if (jQuery('#swords').val() == defaultSearchStr)
	    jQuery('#swords').val("");
	jQuery('#swords').css('color','#000000');
    });
    
    jQuery('#go').click(function (e) {
	var searchstr = jQuery('#swords').val();
	if (!searchstr.length || searchstr == defaultSearchStr)
	    return false;
    });
    
    jQuery('.checkout, .signupbtn, #viewcart').click(function () {
	window.location = jQuery(this).attr('href');
    });
    
    jQuery('.cancelorderbtn').click(function (e) {
	e.preventDefault();
	
	var cancelloc = jQuery('.cancelorderbtn').attr('href');
	var $dialog = $('<div id="cancelorderdlg"></div>')
		.html('Do you really want to cancel this order?')
		.dialog({autoOpen: false,
			modal: true,
			title: 'Cancel order',
			buttons: {
			    "Yes": function() {
				    window.location = cancelloc; 
				    },   
			    "No": function() {
				    $( this ).dialog( 'close' );
				    }
			    }
			});
	$dialog.dialog('open');
	
	return false;
    });
    
    jQuery('#confirmbtn').click(function () {
	var $dialog = $('<div id="orderconfirmdlg"></div>')
		.html('Your order is being processed, please wait... ')
		.dialog({autoOpen: false,
			modal: true,
			title: 'Processing order'});
	$dialog.dialog('open');
    });
    
    jQuery('#shippingbtn').click(function () {
	var phone = jQuery('#phone').val();
	var altphone = jQuery('#alt_phone').val();
	var fax = jQuery('#fax').val();
	
	var msg;
	
	if (phone && !phone.match(/\d{3}\-\d{3}\-\d{4}/))
	    {
	    msg = "Invalid phone format. " +
		"Please enter your phone number using the following format XXX-XXX-XXXX!"
	    open_shipadressdlg(msg);
	    return false;
	    }
	    
	if (!altphone.match(/^\s*$/) && !altphone.match(/\d{3}\-\d{3}\-\d{4}/))
	    {
	    msg = "Invalid alternative phone format. " +
		"Please enter your alternative phone number using the following format XXX-XXX-XXXX!"
	    open_shipadressdlg(msg);
	    return false;
	    }
	    
	if (!fax.match(/^\s*$/) && !fax.match(/\d{3}\-\d{3}\-\d{4}/))
	    {
	    msg = "Invalid fax format. " +
		"Please enter your fax number using the following format XXX-XXX-XXXX!";
	    open_shipadressdlg(msg);
	    return false;
	    }
	    
	jQuery('#add_shipping_info').submit();
    });
    
    function open_shipadressdlg (msg)
	{
	var $dialog = $('<div id="shipaddressdlg"></div>')
		.html(msg)
		.dialog({autoOpen: false,
			modal: true,
			title: 'Shipping Address Error',
			buttons: {"OK": function() {$(this).dialog("close");}}
			});
        $dialog.dialog('open');
	}
});

var wklib =  function() {
    var private_var;

    function private_method() {
    }
    
    return {
	method_1 : function() {
		// do stuff here
	},
	method_2 : function() {
		// do stuff here
	}
    };
}();

