// JavaScript Document
function chkCFLength(tArea,MsgDiv)
{
	tArea.value = tArea.value.replace(/(^ +| +$)/,'');
	document.getElementById(MsgDiv).innerHTML = 'You entered ' + tArea.value.length + ' characters out of 428 max length.';				
};

function countCFLength(tArea,MsgDiv)
{
	document.getElementById(MsgDiv).innerHTML = 'You entered ' + tArea.value.length + ' characters out of 428 max length.';
	if(tArea.value.length >428)
	{	
		tArea.value = tArea.value.substring(0,428);	
		document.getElementById(MsgDiv).innerHTML = 'You entered ' + tArea.value.length + ' characters out of 428 max length.';								
	}
};


function ShowItemOfCat()
{
	Val = document.frmShowCat.ddCats.options[document.frmShowCat.ddCats.selectedIndex].value;
	if(Val.length>0){
		Vals = Val.split(":");
		document.frmShowCat.shop.value = Vals[0];
		document.frmShowCat.cat.value = Vals[1];
		document.frmShowCat.cart.value = Vals[2];
		document.frmShowCat.submit();
	}
};

function SetProperProductOption(TheForm)
{
	var Qty = TheForm.elements.quantity.value;
	var Elements_Length = TheForm.elements.length;
	
	if(isNaN(Qty) || Qty =="" || Qty <1)
		Qty = 1;
	else
		Qty = Math.ceil(Qty);
	for(i=0; i<Elements_Length; i++)
	{
//		alert(TheForm.elements[i].name);
		if(TheForm.elements[i].type =='select-one')
		{
			var Options_Length = TheForm.elements[i].options.length;
			for (op=0; op < TheForm.elements[i].options.length; op++)
			{
				//	alert(TheForm.elements[i].options[op].value);
					var Start_End_Qty = TheForm.elements[i].options[op].value.split('~');
					if(Start_End_Qty.length >1)
					{
						var StartQty = Math.ceil(Start_End_Qty[1])
						var EndQty = Math.ceil(Start_End_Qty[2])
						if(StartQty >=0 && EndQty >StartQty)
						{
							if(Qty >=StartQty && Qty <=EndQty)
							{
									//alert('values exits');
									TheForm.elements[i].options[op].selected  = true;
									TheForm.elements[i].options[op].value = Start_End_Qty[0];
							}
						}
					//	alert("User's given value " + Qty);
					//	alert('Start Qty! '+ Start_End_Qty[1]);
					//	alert('End Qty! '+ Start_End_Qty[2]);
					}
			}

			for (opt=0; opt < TheForm.elements[i].options.length; opt++)
			{
					var Start_End_Qty = TheForm.elements[i].options[opt].value.split('~');
					if(Start_End_Qty.length >1)
					{
							TheForm.elements[i].options[opt].value = Start_End_Qty[0];
					}
			}

		}
	}
};

function AutoUpdatePrice(frm)
{

	var getValFloated  = new Number();
	
    var PickPrice       = frm.elements['hiddenAutoUpdatePrice'].value; 
    var spanPriceHolder = frm.elements['hiddenProdIdAllotted2Elements'].value;
    spanPriceHolder     = ("spanPriceHolder"+spanPriceHolder);  

    if(PickPrice){
		
        var isCalculationMade = false;  // IN CASE OF FASLE ACTUAL PRICE WILL BE PLACED AGAIN
		for (i=0; i<frm.elements.length; i++)
        {
            
			if(frm.elements[i].type == 'select-one'){
                
              var GetOptionString = frm.elements[i].options[frm.elements[i].selectedIndex].innerText || frm.elements[i].options[frm.elements[i].selectedIndex].textContent;
              GetOptionString     = GetOptionString.split('(');
															
				
//			  if(GetOptionString.length && GetOptionString.length >1)
				var GetLastIndex = GetOptionString.length-1;
				if(GetLastIndex){
				
				 var GetOptionVal = GetOptionString[GetLastIndex];
				 GetOptionVal     = GetOptionVal.replace("(",""); 
				 GetOptionVal     = GetOptionVal.replace(")",""); 
				 GetOptionVal     = GetOptionVal.replace("$",""); 
				 GetOptionVal     = GetOptionVal.replace(" ","");

				 if(GetOptionVal.charAt(0) == '-'){

					 isCalculationMade = true;
					 GetOptionVal  = GetOptionVal.replace("-",""); 
					 getValFloated = parseFloat(PickPrice) - parseFloat(GetOptionVal);					 
					 getValFloated = getValFloated.toFixed(2);	
					 
					 if(typeof(document.getElementById(spanPriceHolder).innerText) != "undefined"){
						 document.getElementById(spanPriceHolder).innerText   = "$" + getValFloated;
					 } else {
						 document.getElementById(spanPriceHolder).textContent = "$" + getValFloated;
					 }
											 
					 PickPrice = getValFloated;
					 
				 } else if (GetOptionVal.charAt(0) == '+'){
					 
					 isCalculationMade = true;
					 GetOptionVal  = GetOptionVal.replace("+",""); 
					 getValFloated = parseFloat(PickPrice) + parseFloat(GetOptionVal);					 
					 getValFloated = getValFloated.toFixed(2);	
					 
					 if(typeof(document.getElementById(spanPriceHolder).innerText) != "undefined"){
						 document.getElementById(spanPriceHolder).innerText   = "$" + getValFloated;
					 } else {
						 document.getElementById(spanPriceHolder).textContent = "$" + getValFloated;
					 }
					 
					 PickPrice = getValFloated;
					 
				 } else if(GetOptionVal.charAt(0) == 'x'){
					 
					 isCalculationMade = true;
					 GetOptionVal = GetOptionVal.replace("x","");
					 getValFloated = parseFloat(PickPrice) * parseFloat(GetOptionVal);					 
					 getValFloated = getValFloated.toFixed(2);	
					 
					 if(typeof(document.getElementById(spanPriceHolder).innerText) != "undefined"){
						 document.getElementById(spanPriceHolder).innerText   = "$" + getValFloated;
					 } else {
						 document.getElementById(spanPriceHolder).textContent = "$" + getValFloated;
					 }
					 
					 PickPrice = getValFloated;
					 
				 } else if(!isNaN(GetOptionVal.charAt(0))){
				
					 isCalculationMade = true;
					 getValFloated = parseFloat(GetOptionVal);					 
					 getValFloated = getValFloated.toFixed(2);
					 
					 if(typeof(document.getElementById(spanPriceHolder).innerText) != "undefined"){
						 document.getElementById(spanPriceHolder).innerText   = "$" + getValFloated;
					 } else {
						 document.getElementById(spanPriceHolder).textContent = "$" + getValFloated;
					 }
					 
					 PickPrice = getValFloated;
					 
				 }
				
				}
				
			}
                
        }
		
		if(!isCalculationMade){

			 if(typeof(document.getElementById(spanPriceHolder).innerText) != "undefined"){
				 document.getElementById(spanPriceHolder).innerText   = "$" + PickPrice;
			 } else {
				 document.getElementById(spanPriceHolder).textContent = "$" + PickPrice;
			 }
			
		}
		
    }
	
};