function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/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 ('$' + num + '.' + cents);
	}
	
function trim(s)
{	s =  s.replace( /^\s*/, "" )
	s =  s.replace( /\s*$/, "" );
	return s.length;
}


function test(src) 
{
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
}


function totalTickets(ele) 
{
		var frm, oQty, oPrice, qty=0, ttlqty=0, price=0, ttlprice=0, fee, promoText='',promoPrice, promoQty, promoTotal, strSubName;
		var frm = document.frmTicketBox;
		var oQty = frm.quantity;
		var oPrice = frm.price;
		var oDiscountRate = frm.discountrate ;
		var addcharges = frm.cy_addcharges ;


		if (isNaN(ele.value))
		{
			alert('You must enter a numeric value between 0 and 999');
			ele.value = 0;
			ele.focus();
		} 
		else
		{
			if (ele.value == "" || ele.value < 0 )
			{
				alert('You must enter a numeric value between 0 and 999');
				ele.value=0;
				ele.focus();
			}
		}

	for (var i=0;i < frm.totalvariants.value;i++)
	{
		
		if ( frm.totalvariants.value == 1 ) 
		{
			
			qty = parseInt(frm.quantity.value);
			minperorder	= parseInt(frm.minperorder.value);			
			maxperorder = parseInt(frm.maxperorder.value);
			uprice = parseFloat(frm.price.value) - parseFloat(frm.discountrate.value);
		}
		else
		{
			qty = parseInt(oQty[i].value);
			uprice = parseFloat(oPrice[i].value) - parseFloat(oDiscountRate[i].value);
			minperorder = parseInt(frm.minperorder[i].value);
			maxperorder = parseInt(frm.maxperorder[i].value);
		}
		if( parseFloat(price).toString() == "NaN" ) 
		{	price=0;
		}
		if( parseInt(qty).toString() == "NaN" ) 
		{	qty=0;
		}	
		//alert(' Unit Price : ' + price);
		//alert(' quantity : ' + qty);
		//alert ('max per order ' + maxperorder);
		if ( qty > 0 && qty < minperorder ) 
		{
			alert('A minimum of ' + minperorder + ' should be ordered for this type. ');
			if ( frm.totalvariants.value == 1 ) 
			{
				frm.quantity.value = 0;
			}
			else
			{
				frm.quantity[i].value = 0;
			}
			qty = 0;
		}
		if ( qty > maxperorder ) 
		{
			alert('Only a maximum of ' + maxperorder + ' allowed for this type. ');
			if ( frm.totalvariants.value == 1 ) 
			{
				frm.quantity.value = maxperorder;
			}
			else
			{
				frm.quantity[i].value = maxperorder;
			}
			qty = maxperorder;
		}
		price = 	uprice * qty;
		
		ttlprice = ttlprice + price ;
		//alert('total ' + ttlprice);
		frm.camount.value=ttlprice;
		if (frm.isMac.value == "True")
		{
			
		}
		else
		{
			divid='divsubtotal'+ i;
			if (document.all)
			{ //ie4+
				document.all[divid].innerHTML=formatCurrency(price);
			}
			else 
			{
				if (document.layers)
				{ //ns4+  
					
					with (document.layers[divid].document) {
					write(formatCurrency(price));
					close();
					}
				}
				else 
				{
					divelement = document.getElementById(divid);
					divelement.innerHTML = formatCurrency(price);
				}
			}	
		}
		
			//Update Total Price Section
		if (frm.isMac.value == "True")
		{
		
		} 
		else 
		{
		
		
		}
	}
	addcharges = parseInt(frm.cy_addcharges.value);
	if (  isNaN(addcharges) ) 
	{
		addcharges = 0 ;
	}
	ttlprice = ttlprice + addcharges ;
	
	divid='divtotal';
	if (document.all)
	{ //ie4+
			document.all[divid].innerHTML=formatCurrency(ttlprice);
	}
	else 
	{ 
		if (document.layers)//ns4+  
		{	
			with (document.layers[divid].document) {
				write(formatCurrency(ttlprice));
				close();
			}
		}
		else
		{
			divid='divtotal';
			if (document.all)
			{ //ie4+
				document.all[divid].innerHTML=formatCurrency(ttlprice);
			}
			else 
			{ 
				if (document.layers)//ns4+  
				{	
					with (document.layers[divid].document) {
					write(formatCurrency(ttlprice));
					close();
					}
				}
				else
				{
					divelement = document.getElementById(divid);
					divelement.innerHTML = formatCurrency(price);
				}
			}			
			divelement = document.getElementById(divid);
			divelement.innerHTML = formatCurrency(price);
		}
	}
}


function message(form)
{
alert ("Please note that your Order value for Christams products is below $30. So please add to your cart to take your order size for Christmas products to $30 or above.");
return false;
}


function validategiftcert(form)
{	
	if (trim(form.yname.value)==0)
	{
		alert("Please enter Your name");
		form.yname.focus();
		return false;
	}
	if (test(form.yemail.value)==false)
	{
		alert("Please enter Your valid eMail Address");
		form.yemail.focus();
		return false;
	}
	if (trim(form.gname.value)==0)
	{
		alert("Please Gift to Name");
		form.gname.focus();
		return false;
	}
	if (test(form.gemail.value)==false)
	{
		alert("Please enter the Valid Gift to eMail Address");
		form.gemail.focus();
		return false;
	}
	
	if (form.gemail.value==form.yemail.value)
	{	
		alert("Please note that the Gift Certificate is not valid for own use. (Please see Gift Certificate: Conditions)");
		form.gemail.focus();
		return false;
	}
	if ((form.gmsg.value.length) > 250)
	{
		alert("The gift message cannot exceed 250 letters.");
		form.gmsg.focus();
		return false;
	}
	
}


function trunc(s)
{	s =  s.replace( /^\s*/, "" )
	s =  s.replace( /\s*$/, "" );
	return s
}

	
function validatebilling(form)
{   //inputok = true;
	{	
		if (form.first_name.value=="")
		{
			alert("Please provide your first name.");
			form.first_name.focus();
			return false;
		}
		if (form.last_name.value=="")
		{
			alert("Please provide your last name.");
			form.last_name.focus();
			return false;
			
		}
		if (form.address_line1.value=="")
		{
			alert("Please provide your address");
			form.address_line1.focus();
			return false;
			
		}
		
		if (form.tel_number.value=="")
		{
			alert("Please provide your phone number.");
			form.city.focus();
			return false;
		
		}
		
		if (form.city.value=="")
		{
			alert("Please provide your city name");
			form.city.focus();
			return false;
		
		}
		if (form.region_code.value=="")
		{
			alert("Please provide your state name");
			form.region_code.focus();
			return false;
			
		}
		if (form.postal_code.value=="" ) //|| (!IsNumeric(form.postal_code.value))
		{
			alert("Please provide your zip code");
			form.postal_code.focus();
			return false;
			
		}
		
		if (form.cardnumber.value < 0 || form.cardnumber.value == "" || (!IsNumeric(form.cardnumber.value)) )
		{  alert("Please provide valid credit card number");
			form.cardnumber.focus();
			return false;  
		}
   }    



function IsNumeric(strString)
{  var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
    {
       strChar = strString.charAt(i);
       if (strValidChars.indexOf(strChar) == -1)
       {
         blnResult = false;
       }
     }
  return blnResult;
 }
/*if (inputok)
    {  
     return true;
     }
    else
    {  return false;
    } */
  return true;  
}
function IsNumeric(strString)
{
 var strValidChars = "0123456789";
 var strChar;
 var blnResult = true;
 //  test strString consists of valid characters listed above
 for (i = 0; i < strString.length && blnResult == true; i++)
    {
       strChar = strString.charAt(i);
       if (strValidChars.indexOf(strChar) == -1)
       {
         blnResult = false;
       }
     }
     return blnResult;
     }  


function validateshipping(form)
{
alert ("Please note that importation of apparel and accessories such as belts, shoes, jewelry etc., for commercial purpose can attract customs duty under US Customs Service.");
}