var lMaxOptions = 0;
var lMinOptions = 0;

//-------------------------------------------------------------------
// isInteger(value)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isInteger(val){
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
	}
//-------------------------------------------------------------------

//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
	}


//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------
function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
	}

//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val){return(parseFloat(val,10)==(val*1));}

function Dollar (val) {  // force to valid dollar amount
	var str,pos,rnd=0;
	if (val < .995) rnd = 1;  // for old Netscape browsers
	str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
	pos = str.indexOf (".");
	if (pos > 0) str = str.substring (rnd, pos + 3);
	return str;
}

function filterNum(str) {
	re = /^\$|,|&nbsp;/g;
	// remove "$" and ","
	var tempHolding = str.replace(re, "");
	return tempHolding.replace("$", "");
}

function vldQuantity(){
if (document.getElementById("txtQuantity").value != '')
	{
	if (!isInteger(document.getElementById("txtQuantity").value)) {
				alert("Quantity must be a positive whole number. No , or . Setting quantity to 1");
				document.getElementById("txtQuantity").value = "1";
				CalculateMainSubTotal();
				return false;
			}
	}
	CalculateMainSubTotal()
}

function vldOptionQuantity(obj,sID,cPrice) {
	if (!isInteger(obj.value)) {
			alert("Quantity must be a positive number. Setting quantity to 1");
			obj.value = "1";
			AddOptionPriceSum(document.getElementById('OptionItems_'+sID).checked,  cPrice, sID)
			return false;
		}
	
	//now update pricing to reflect change of quantity
	AddOptionPriceSum(document.getElementById('OptionItems_'+sID).checked,  cPrice, sID)
}

function AddOptionPriceSum (objCheckedStatus,cPrice,sID) {  // add to current price
	
	var tempNewPrice=0;
	
	//first check if limited on selecting options and have exceeded allowed limit  0=unlimited
	if (lMaxOptions>0)
	    {
	    var iOptionCount=0;
	    var coll = document.getElementById('OptionsBuildTable').getElementsByTagName("INPUT")
		if (coll!=null)
		    {
			for (i=0; i<coll.length; i++) 
				{
				if (coll[i].id.slice(0,12) == 'OptionItems_')
					{
						if (coll[i].checked==true)
						    {
						    iOptionCount += 1
						    }
					}
				}
		    }
		 
		 if (iOptionCount > lMaxOptions)
		    {
		    alert('You can only select ' + lMaxOptions + ' Options with this package')
		    document.getElementById('OptionItems_' + sID).checked=false
		    return
		    }
	    }
	
	//Next need to subtotal item checked = cPrice x qty
	//alert(cPrice)
	if (document.getElementById('OptionQuantity_'+sID) == undefined)
		{
		alert('Problem with pricing quantity.  Click Update Price instead')
		return
		}
		
	var itemQty = document.getElementById('OptionQuantity_'+sID).value
	if (!isNumeric(itemQty))
		{
		document.getElementById('OptionQuantity_'+sID).value = "1";
		itemQty = 1;
		}
	
	if (!isNumeric(cPrice))
		{
		alert('Problem with pricing option.  Click Update Price instead')
		return
		}
	

	//unchecked item so remove price from items subtotals
	if (!objCheckedStatus)
		{
		document.getElementById('OptionLineItemSubTotal_'+sID).value = "0";
		//cPrice *= -1
		} else {
		//update line items subtotal
		document.getElementById('OptionLineItemSubTotal_'+sID).value = Dollar(cPrice*1.0 * itemQty*1.0);
		}
		
	CalculateMainSubTotal()
}

function CalculateMainSubTotal()   {

		var tempNewPrice=0;
		
		//look at any options if they exist to determine subtotal
		if (document.getElementById('OptionsBuildTable') != null)
		{
		    var coll = document.getElementById('OptionsBuildTable').getElementsByTagName("INPUT")
		    if (coll!=null)
		    {
			    for (i=0; i<coll.length; i++) 
				    {
				    if (coll[i].id.slice(0,23) == 'OptionLineItemSubTotal_')
					    {
						    tempNewPrice += (coll[i].value*1.0)
    					
					    }
				    }
		    }
		}

	//show options total in options area
	if (isNumeric(tempNewPrice)==true && document.getElementById('OptionTotal') != null)
		{
		if (tempNewPrice != 0)
			{
			document.getElementById('OptionTotal').innerHTML = '&nbsp;&nbsp;' + formatCurrency(tempNewPrice);
			} else {
			document.getElementById('OptionTotal').innerHTML = '<font style="font-size: 16px;color: #000000;font-weight: bold;">' + formatCurrency(tempNewPrice) + '</font>';
			}
		}
		
	
	//sum totalprice now
    SumTotalPrice(tempNewPrice)

	return
	

}


function SumTotalPrice(cOptionPrice)
{
    var tempBasePrice = 0;
    var tempTotalPrice = 0;
    
    if (document.getElementById('BasePrice') != null)
        {
	    tempBasePrice = filterNum(document.getElementById('BasePrice').innerHTML)
	    }
	
	
	if (!isNumeric(tempBasePrice))
		{
		alert('Problem with pricing option.  Click Update Price instead.')
		return
		}
	
	var BaseQty = document.getElementById("txtQuantity").value;
	if (!isNumeric(BaseQty))
		{
		BaseQty = 1.0;
		}

	document.getElementById('TotalPrice').innerHTML = '&nbsp;&nbsp;' + formatCurrency(cOptionPrice*BaseQty + tempBasePrice*BaseQty);
}

function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
  if(cents<10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function fnSubmit(){
		var strURL = "shopcart/ShopCart.asp";
		var intQuantity = document.getElementById("txtQuantity").value;
		var strrelated='';
		var strbuypattern='';
		var strhotitems= '';
		var strviewed = '';
		var strWarrenty = '';
		
		//make sure minimum option choose
		if (lMinOptions > 0)
		    {
	    var iOptionCount=0;
	    var coll = document.getElementById('OptionsBuildTable').getElementsByTagName("INPUT")
		if (coll!=null)
		    {
			for (i=0; i<coll.length; i++) 
				{
				if (coll[i].id.slice(0,12) == 'OptionItems_')
					{
						if (coll[i].checked==true)
						    {
						    iOptionCount += 1
						    }
					}
				}
		    }
		 
		 if (iOptionCount < lMinOptions)
		    {
		    alert('You must select ' + lMinOptions + ' Options with this package')
		    return
		    }
	    }
		
		
		if (document.getElementById("related")!= undefined)
		{
			if ((document.getElementById("related").length)!= undefined)
			{
				with(document)
					{
					for(var count=0;count<item("related").length;count++)
						{
					    if(item("related")[count].checked == true)
							{
							strrelated = strrelated + item("related")[count].value + ','	
							}
						}
					}
			}
			else
			{
				if(document.getElementById("related").checked == true)
				{
						strrelated = strrelated + document.getElementById("related").value + ','	
				}
			}
		}
		
		
		if (document.getElementById("buypattern")!= undefined)
		{
			if ((document.getElementById("buypattern").length)!= undefined)
			{
				with(document){
					for(var count=0;count<item("buypattern").length;count++){
					    if(item("buypattern")[count].checked == true){
							strbuypattern = strbuypattern + item("buypattern")[count].value + ','	
							//alert(strbuypattern);
							}
						}
					}		
			}
			else
			{	if(document.getElementById("buypattern").checked == true){
						strbuypattern = strbuypattern + document.getElementById("buypattern").value + ','	
				}
			}
		}
		if (document.getElementById("hotitems")!= undefined)
		{
			if ((document.getElementById("hotitems").length)!= undefined)
			{
				with(document){
					for(var count=0;count<item("hotitems").length;count++){
					    if(item("hotitems")[count].checked == true){
							strhotitems = strhotitems + item("hotitems")[count].value + ','	
							}
						}
					}
			}
			else
			{	if(document.getElementById("hotitems").checked == true){
						strhotitems = strhotitems + document.getElementById("hotitems").value + ','	
				}
			}
		}
		
		if (document.getElementById("viewed")!= undefined)
		{	if ((document.getElementById("viewed").length)!= undefined)
			{
				with(document)
					{
					for(var count=0;count<item("viewed").length;count++)
						{
						if(item("viewed")[count].checked == true)
							{
							strviewed = strviewed + item("viewed")[count].value + ','	
							}
						}
					}
			}
			else
			{
				if(document.getElementById("viewed").checked == true)
				{
						strviewed = strviewed + document.getElementById("viewed").value + ','	
				}
			}
		}
		
	if (document.getElementById("Warrenty")!= undefined)
		{	if ((document.getElementById("Warrenty").length)!= undefined)
			{
				with(document)
					{
					for(var count=0;count<item("Warrenty").length;count++)
						{
						if(item("Warrenty")[count].checked == true)
							{
							strWarrenty = strWarrenty + item("Warrenty")[count].value + ','	
							}
						}
					}
			}
			else
			{
				if(document.getElementById("Warrenty").checked == true)
				{
						strWarrenty = strWarrenty + document.getElementById("Warrenty").value + ','	
				}
			}
		}


		if (document.SendCartData != null)
			{
			document.SendCartData.WhatDo.value = 'AddItem'
			document.SendCartData.submit();
			}

		
	}

function fnSubmitCheckNoPrice()
{
    if (document.getElementById('OptionsBuildTable')!= null)
        {
        fnSubmitCheckOptions()
        } else {
        document.SendCartData.WhatDo.value = 'AddItem';
		document.SendCartData.submit();
        }
}	
	
function fnSubmitCheckOptions()
{
	//this function only called when main product does not have any pricing, so must be options
	var bFoundChecked=false;
	//check if options table exists - need select an option if no pricing on main product
	if (document.getElementById('OptionsBuildTable')!= null)
		{
		var coll = document.getElementById('OptionsBuildTable').getElementsByTagName("INPUT")
		if (coll!=null)
		    {
			for (i=0; i<coll.length; i++) 
				{
				if (coll[i].id.slice(0,12) == 'OptionItems_')
					{
						if (coll[i].checked==true)
							{
							bFoundChecked=true;
							break
							}
					
					}
				}
		    }
		
		if (bFoundChecked==true)
		    {
			document.SendCartData.WhatDo.value = 'AddItem';
			document.SendCartData.submit();
		    } else {
			    alert('Select an option before adding item to cart.')
		    }
		}
}


