/*
Check Clear forms function
*/
function checkClearForms()
{
    if( window.location == 'signin_account.php' )
    {
        var i;
        for (i = 0; (i < document.forms.length); i++)
        {
            document.forms[i].reset();
        }
    }
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

home_on                     = new Image(104, 48);
home_on.src                 = "images_small/topnav_home_highlight.png";              
home_off                    = new Image(104, 48);
home_off.src                = "images_small/topnav_home.png"; 

faq_on                      = new Image(93, 48);
faq_on.src                  = "images_small/topnav_faq_highlight.png";              
faq_off                     = new Image(93, 48);
faq_off.src                 = "images_small/topnav_faq.png"; 

factsandfun_on              = new Image(152, 48);
factsandfun_on.src          = "images_small/topnav_factsandfun_highlight.png";              
factsandfun_off             = new Image(152, 48);
factsandfun_off.src         = "images_small/topnav_factsandfun.png"; 

contact_us_on               = new Image(145, 48);
contact_us_on.src           = "images_small/topnav_contactus_highlight.png";              
contact_us_off              = new Image(145, 48);
contact_us_off.src          = "images_small/topnav_contactus.png"; 

customer_service_on         = new Image(210, 48);
customer_service_on.src     = "images_small/topnav_customerservice_highlight.png";              
customer_service_off        = new Image(210, 48);
customer_service_off.src    = "images_small/topnav_customerservice.png"; 

cart_on                     = new Image(157, 61);
cart_on.src                 = "images_small/cartcheckout_highlight.png";              
cart_off                    = new Image(157, 61);
cart_off.src                = "images_small/cartcheckout.png";

dealoftheday_on             = new Image(120, 80);
dealoftheday_on.src         = "images_small/deal_of_the_day_highlight.gif";              
dealoftheday_off            = new Image(120, 80);
dealoftheday_off.src        = "images_small/deal_of_the_day.gif";
 
add_to_cart_on              = new Image(158, 43);
add_to_cart_on.src          = "images_small/button_addtocart_highlight.png";              
add_to_cart_off             = new Image(158, 43);
add_to_cart_off.src         = "images_small/button_addtocart.png"; 
 
continue_shopping_on        = new Image(207, 43);
continue_shopping_on.src    = "images_small/button_continueshopping_highlight.png";              
continue_shopping_off       = new Image(207, 43);
continue_shopping_off.src   = "images_small/button_continueshopping.png"; 
 
checkout_on                 = new Image(121, 42);
checkout_on.src             = "images_small/button_checkout_highlight.png";              
checkout_off                = new Image(121, 42);
checkout_off.src            = "images_small/button_checkout.png"; 
	    
function image_on(imageName)
{
        imageOn   = eval(imageName + "_on.src");
        document [imageName].src = imageOn;
}

function image_off(imageName)
{
        imageOff = eval(imageName + "_off.src");
        document [imageName].src = imageOff;
}

function setProductAttributes(catId, productId, form)
{    
    var sizeId = form.sizeOptions.options[form.sizeOptions.selectedIndex].value;
    var colorId = form.colorOptions.options[form.colorOptions.selectedIndex].value;
       	
	if( ( colorId > 0 ) && ( sizeId > 0 ) )
	{
    	window.location.href = 'index.php?c=' + catId + '&p=' + productId + '&ci=' + colorId + '&si=' + sizeId;
	}
	else if( colorId > 0 )
	{
    	window.location.href = 'index.php?c=' + catId + '&p=' + productId + '&ci=' + colorId;
	}
	else if( sizeId > 0 )
	{
    	window.location.href = 'index.php?c=' + catId + '&p=' + productId + '&si=' + sizeId;
	}
}

function verifyAddToCart(cartUrl, colorId, sizeId)
{
    if( ( colorId == 0 ) || ( sizeId == 0 ) )
    {
        var errorMsg = 'Cannot add to your shopping cart until you do the following:\n';
        if( sizeId == 0 )
        {
            errorMsg += '   Select a SIZE\n';
        }
        if( colorId == 0 )
        {
            errorMsg += '   Select a COLOR';
        }
        alert( errorMsg );
        return;
    }
    window.location.href = cartUrl;
}

function popUp(url,width,height,scroll,winname,toolbar,location)
{
    if (!winname)
     winname = "win";
    if (winname == '')
     winname = "win";
    if (!toolbar)
     toolbar = 0;
    if (!location)
     location = 0;
    sealWin=window.open(url,winname,"toolbar="+toolbar+",location="+location+",directories=0,status=0,menubar=0,scrollbars="+scroll+",resizable=1,width="+width+",height="+height);
    self.name = "mainWin";
    sealWin.focus();
}

function deleteSavedAddress(addrId)
{
	if (confirm('Delete this shipping address?')) {
        window.location.href = 'processAccount.php?uaction=deleteSavedAddress&addrId=' + addrId;
    }
}

function editSavedAddress(addrId)
{
    window.location.href = 'new_address.php?addrId=' + addrId;
}

function makeDefaultAddress(addrId)
{
    window.location.href = 'processAccount.php?uaction=makeDefaultAddress&addrId=' + addrId;
}

function deleteSavedBilling(billId)
{
	if (confirm('Delete this billing address?')) {
        window.location.href = 'processAccount.php?uaction=deleteSavedBilling&billId=' + billId;
    }
}

function editSavedBilling(billId)
{
    window.location.href = 'new_billing.php?billId=' + billId;
}

function makeDefaultBilling(billId)
{
    window.location.href = 'processAccount.php?uaction=makeDefaultBilling&billId=' + billId;
}
