// JavaScript Document
var extracost = 0.0;
var extraship = 0.0;
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

function ajax_do (url) {
        // Does URL begin with http?
        if (url.substring(0, 4) != 'http') {
                url = base_url + url;
        }

        // Create new JS element
        var jsel = document.createElement('SCRIPT');
        jsel.type = 'text/javascript';
        jsel.src = url;
	    // Append JS element (therefore executing the 'AJAX' call)
        document.body.appendChild (jsel);
}

function CalcExtraCosts(OpValue)
{
	var NewValue = OpValue;
	var CostPos = NewValue.indexOf('$$');
	var tmpCostInfo;
	if (CostPos > 0)
	{               
		NewValue = NewValue.substring(0,CostPos);
   		//Check if the extra costs also contains extra shipping cost.
		tmpCostInfo = OpValue.substring(CostPos+2,OpValue.length);
		var ShipPos = tmpCostInfo.indexOf('$');
		if (ShipPos > 0) //additional Shipping cost 
		{
			var ShipInfo = tmpCostInfo.substring(ShipPos+1,tmpCostInfo.length);
			tmpCostInfo = tmpCostInfo.substring(0,ShipPos);
	        extraship = extraship + parseFloat(ShipInfo);			
		}		
        extracost = extracost + parseFloat(tmpCostInfo);
	}
	return NewValue;
}

function stripspecial(Value)
{
	newval = Value.replace('\'','\\\'');
	return newval;
}

function ProcessToCart(theform) {
	extracost = 0;
	extraship = 0;
	var Specs1 = '';
	var Specs2 = '';
	var returnval=true //by default, allow form submission
	for (i=0; i<theform.elements.length; i++)
	{
		//Check if "Required Options" has values.
		if (theform.elements[i].name.indexOf('RS_')==0)
		{
			var Op_Name = theform.elements[i].name;
			var Op_Value = theform.elements[i].value;
			Op_Value = CalcExtraCosts(Op_Value);
			Op_Name = Op_Name.replace('RS_','');
			Op_Name = Op_Name.replace('_',' ');
			if (Op_Value.length==0)
			{
				//Display different message for "Text" Box
				if (theform.elements[i].type=='text')
					alert('Please enter '+Op_Name+'.');
				else
					alert('Please select '+Op_Name+'.');
				theform.elements[i].focus();
				return(false);  //Abort the command
			}
			else
			{
				if (Specs1.length<160)
					Specs1 = Specs1 + ' ( ' + Op_Name + ': '+ Op_Value+' ) ';
				else
					Specs2 = Specs2 + ' ( ' + Op_Name + ': '+ Op_Value+' ) ';
			};
		};
		//Build the spec list values
		if (theform.elements[i].name.indexOf('OS_')==0)
		{
			var Op_Name = theform.elements[i].name;
			var Op_Value = theform.elements[i].value;
			Op_Value = CalcExtraCosts(Op_Value);
			Op_Name = Op_Name.replace('OS_','');
			Op_Name = Op_Name.replace('_',' ');
			if (Specs1.length<160)
				Specs1 = Specs1 + ' ( ' + Op_Name + ': '+ Op_Value+' ) ';
			else
				Specs2 = Specs2 + ' ( ' + Op_Name + ': '+ Op_Value+' ) ';
		};
	};
	theform.os0.value = Specs1;     
	if (Specs2.length > 0) {
		theform.op1.value = 'More Specs: ';
		theform.os1.value = Specs2;
	}
	theform.extracost.value = extracost;
	theform.extraship.value = extraship;
//	tb_show('Bring Your Dog Home Cart','viewcart.php?action=add&item_name='+escape(theform.item_name.value)+'&item_number='+theform.item_number.value+'&amount='+theform.amount.value+'&shipping='+theform.shipping.value+'&extraship='+theform.extraship.value+'&extracost='+theform.extracost.value+'&os0='+theform.os0.value+'&os1='+theform.os1.value+'&keepThis=true&TB_iframe=true&height=500&width=700',false)
	return(true);
 }

 
function ShowCart(){
	tb_show('Bring Your Dog Home Cart','viewcart.php?keepThis=true&TB_iframe=true&height=500&width=700',false)
}
