var Total=0
var configArr,sCustomFields

function formatValue(value){
	return Math.round(value*100)/100;
}

function printkit()
{
	var iIndex,myWin,iCount,sString1,len,sString2,sImgLink;
	myWin = window.open('','_kit','width=500,height=500,toolbar,scrollbars')
	//myWin.document.open();
	iCount = 0;
	len = document.frmKitUpgrades.elements.length;
	myWin.document.write("<title>"+getMessage("iPrintVerKit")+"</title>");
	myWin.document.write("<table border='0' cellspacing = '8'><tr><td width='100%' colspan='4' valign='top'><font face='verdana, arial' size='1'><b>");
	sImgLink=document.frmKitAmt.imglink.value 
	if (sImgLink.substring(sImgLink.length - 1, sImgLink.length) != "/" )
	myWin.document.write("<img src='" + document.frmKitAmt.imglink.value + "' align=left>");
	myWin.document.write(document.frmKitAmt.itemname.value);
	myWin.document.write("</b><br><br>");
	myWin.document.write(document.frmKitAmt.description.value);
	if (sKitCall4Price == "T") {
		myWin.document.write("</td></tr><tr><td colspan='3'><font face = 'verdana, arial' size = '1' valign='top'>"+getMessage("iKitPrice")+":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+getMessage("iCallForPrice"));
	} else {
		myWin.document.write("</td></tr><tr><td colspan='2'><font face = 'verdana, arial' size = '1' valign='top'><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+getMessage("iQty")+" : " + iQty + "</td></tr><tr><td colspan='3'><font face = 'verdana, arial' size = 1>"+getMessage("iKitPrice")+":&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + sCurrSymbol);
		myWin.document.write(document.frmKitAmt.txtTotalKitValue.value);
		if (bAddCurrExists != "") {
			myWin.document.write("&nbsp; (" + sAdditionalCurrencySymbol + document.frmKitAmt.txtTotalKitValue_Add_Curr.value +")");
		}
	}
	myWin.document.write("</b></font></td></tr>");
	if (bShowCustomFields) {
		myWin.document.write(sCustomFields);
	}
	for(iIndex=0;iIndex<=parseInt(len)-1;iIndex++)
	{
		if((document.frmKitUpgrades.elements[iIndex].type.substring(0,3) == "sel") || (document.frmKitUpgrades.elements[iIndex].name.substring(0,8) == "upg_name"))
		{		if(iCount==0) {
//					myWin.document.write("<tr><td colspan=4><font face = 'verdana, arial' size = 1 valign=top><b>"+getMessage("iKitItem"));
//					if (sKitClubTotal != "T" && sKitCall4Price != "T") {
//						myWin.document.write(" " + getMessage("iIndividualPrice"));
//					}
//					myWin.document.write(" " + getMessage("iShownBelow")+"</b></font></td></tr>");
				}
				iCount++;
//				myWin.document.write("<tr><td><font face = 'verdana, arial' size = 1>");
//				myWin.document.write(eval("document.frmKitUpgrades.kitname" + iCount + ".value")+"</td></font>");
				//Added to Show individual kit items price
//				if (sKitClubTotal != "T" && sKitCall4Price != "T") {
//					myWin.document.write("<td nowrap><font face = 'verdana, arial' size = 1>" + sCurrSymbol);
//					myWin.document.write(eval("document.frmKitUpgrades.price" + iCount + ".value"));
//					if (bAddCurrExists != "") {
//						myWin.document.write(" &nbsp;(" + sAdditionalCurrencySymbol + eval("document.frmKitUpgrades.Add_Curr_price" + iCount + ".value")+ ")");
//					}
//					myWin.document.write("</td></font>")
//				}
				//Ends Here
//				myWin.document.write("<td><font face = 'verdana, arial' size = 1>");
//				if(document.frmKitUpgrades.elements[iIndex].type.substring(0,3) == "sel"){
//					sString1 = document.frmKitUpgrades.elements[iIndex].options[document.frmKitUpgrades.elements[iIndex].selectedIndex].text;
//					sString2 = sString1.indexOf("[");
//					if(sString2 != -1)
//						sString1 = sString1.substring(0,sString2-1);
//				}	
//				else
//					sString1 = document.frmKitUpgrades.elements[iIndex].value

//				myWin.document.write(sString1);
//				myWin.document.write("</td></tr>");
			}
	}	
	myWin.document.write("</table>");
	myWin.document.close();
	myWin.focus();
}

function printitem(sel)
{
	var code,arr;
	code =eval("document.frmKitUpgrades.sel" + sel + ".options[document.frmKitUpgrades.sel" + sel + ".selectedIndex].value");
	arr = code.split("#");
	if(arr[3]=="T")
		window.open(sNonSecurePath + "printItem.asp?Source=CK&ic=" + arr[0],"winPrint","toolbar,height=400,width=550,scrollbars,resizable");
	else
		alert(getMessage("iMoreInfoNotAvail"));
}

function CalculateTotal(kitprice,clubtotal,sLocaleId,addKitPrice){
	if(sLocaleId == "")
		sLocaleId = iEnglishLocaleId;
	var i,sel_Count;
	var Sum,addSum,temp,addtemp,arr,arr2,totalvalue,addTotalvalue,sKitValue,sAddKitValue,objKitFld,objAddKitFld;
	totalvalue=0;
	addTotalvalue=0;
	configArr=""
	
	len = document.frmKitUpgrades.elements.length;
	if (sKitCall4Price != "T") {
		if(clubtotal=='T'){
			Sum=getEnglishLocale(kitprice,sLocaleId);
			addSum = getEnglishLocale(addKitPrice,sLocaleId);
			
		}
		else{
			Sum = 0.0;
			addSum = 0.0;
		}
			
		sel_Count = 0;
		
		for(i=0;i<=parseInt(len)-1;i++)	{
			if(document.frmKitUpgrades.elements[i].type.substring(0,3) == "sel") {				
				arr =  document.frmKitUpgrades.elements[i].options[document.frmKitUpgrades.elements[i].selectedIndex].value.split("#");
				arr2 = document.frmKitUpgrades.elements[i].options[0].value.split("#");
				if (document.frmKitUpgrades.elements[i].selectedIndex == 0){
					if (clubtotal != 'T'){
						Sum = formatValue(parseFloat(Sum) + parseFloat(getEnglishLocale(arr[1],sLocaleId) * parseInt(arr[2])));
						addSum = formatValue(parseFloat(addSum) + parseFloat(getEnglishLocale(arr[4],sLocaleId) * parseInt(arr[2])));
					}
						
				}
				else{
					if (clubtotal == 'T'){					
						Sum = formatValue(parseFloat(Sum) + parseFloat(getEnglishLocale(arr[1],sLocaleId) * parseInt(arr[2])));
						addSum = formatValue(parseFloat(addSum) + parseFloat(getEnglishLocale(arr[4],sLocaleId) * parseInt(arr[2])));
					}
					else {
						Sum = formatValue(parseFloat(Sum) + parseFloat((parseFloat(getEnglishLocale(arr[1],sLocaleId)) + parseFloat(getEnglishLocale(arr2[1],sLocaleId))) * parseInt(arr2[2])));
						addSum = formatValue(parseFloat(addSum) + parseFloat((parseFloat(getEnglishLocale(arr[4],sLocaleId)) + parseFloat(getEnglishLocale(arr2[4],sLocaleId))) * parseInt(arr2[2])));
					}
				}				
				configArr = configArr + parseInt(document.frmKitUpgrades.elements[i].selectedIndex)+ "#";
				sel_Count = parseInt(sel_Count) + 1;
				if (clubtotal != 'T'){
					if(document.frmKitUpgrades.elements[i].selectedIndex == 0)	{
						temp = getEnglishLocale(arr2[1],sLocaleId)*arr2[2];
						addtemp	=getEnglishLocale(arr2[4],sLocaleId)*arr2[2];
						eval("document.frmKitUpgrades.price" + sel_Count + ".value=" + formatValue(temp));
						fldname="frmKitUpgrades.price" + sel_Count;
						setFormat(fldname);
						objKitFld = eval("document.frmKitUpgrades.price" + sel_Count);						 
						objKitFld.value= getCurrentLocale(objKitFld.value,sLocaleId);
						if (bAddCurrExists != "") {
						eval("document.frmKitUpgrades.Add_Curr_price" + sel_Count + ".value=" + formatValue(addtemp));												
						addfldname="frmKitUpgrades.Add_Curr_price" + sel_Count;
						setFormat(addfldname);
						objAddKitFld = eval("document.frmKitUpgrades.Add_Curr_price" + sel_Count);	
						objAddKitFld.value= getCurrentLocale(objAddKitFld.value,sLocaleId);				
						}
						
					}
					else	{
						temp = getEnglishLocale(arr[1],sLocaleId);
						temp = (parseFloat(temp) + parseFloat(getEnglishLocale(arr2[1],sLocaleId)))*arr2[2];
						addtemp = getEnglishLocale(arr[4],sLocaleId);
						addtemp = (parseFloat(addtemp) + parseFloat(getEnglishLocale(arr2[4],sLocaleId)))*arr2[2];
						eval("document.frmKitUpgrades.price" + sel_Count + ".value=" + formatValue(temp));
						fldname="frmKitUpgrades.price" + sel_Count;
						setFormat(fldname);
						objKitFld = eval("document.frmKitUpgrades.price" + sel_Count);
						objKitFld.value= getCurrentLocale(objKitFld.value,sLocaleId);
						if (bAddCurrExists != "") {
						eval("document.frmKitUpgrades.Add_Curr_price" + sel_Count + ".value=" + formatValue(addtemp));												
						addfldname="frmKitUpgrades.Add_Curr_price" + sel_Count;
						setFormat(addfldname);
						objAddKitFld = eval("document.frmKitUpgrades.Add_Curr_price" + sel_Count);						 
						objAddKitFld.value= getCurrentLocale(objAddKitFld.value,sLocaleId);				
						}
						
						
					}
				}
			}
			
			if(document.frmKitUpgrades.elements[i].name.substring(0,13) == "upgradehidden")	{
				configArr = configArr + 0 + "#"
				sel_Count = parseInt(sel_Count) + 1;
			}
		}
	
		if(clubtotal != 'T'){
			for(i=0;i<=parseInt(len)-1;i++)	{
				if(document.frmKitUpgrades.elements[i].name.substring(0,13) == "upgradehidden")
					totalvalue= totalvalue + parseFloat(getEnglishLocale(document.frmKitUpgrades.elements[i].value,sLocaleId));
				if(document.frmKitUpgrades.elements[i].name.substring(0,22) == "Add_Curr_upgradehidden")
					addTotalvalue= addTotalvalue + parseFloat(getEnglishLocale(document.frmKitUpgrades.elements[i].value,sLocaleId));									
					
			}
		}
		sKitValue=parseFloat(Sum)+ totalvalue;	
		sAddKitValue = parseFloat(addSum)+ addTotalvalue;	
		if(sKitValue < 0 || ( (bAddCurrExists != "") && (sAddKitValue<0) ) )
			alert(getMessage("iValidPriceComb"));
		document.frmKitAmt.txtTotalKitValue.value = sKitValue;
		setFormat("frmKitAmt.txtTotalKitValue");
		stempKitVal = document.frmKitAmt.txtTotalKitValue.value;
		document.frmKitAmt.txtTotalKitValue.value = getCurrentLocale(stempKitVal,sLocaleId);
		//document.frmKitUpgrades.txtTotalKitValue2.value = getCurrentLocale(stempKitVal,sLocaleId);
		if (bAddCurrExists != "") {
		document.frmKitAmt.txtTotalKitValue_Add_Curr.value = sAddKitValue		
		setFormat("frmKitAmt.txtTotalKitValue_Add_Curr");
		stempAddKitVal = document.frmKitAmt.txtTotalKitValue_Add_Curr.value;
		document.frmKitAmt.txtTotalKitValue_Add_Curr.value = getCurrentLocale(stempAddKitVal,sLocaleId);
		}
	}
}

function setFormat(field){
		var i, idiff,strval;
		var m_Field = eval("document." + field);
  	if(m_Field.value.indexOf(".") < 0)
			m_Field.value = m_Field.value + ".00";
		if(m_Field.value.length-m_Field.value.indexOf(".") == 2)
			m_Field.value = m_Field.value + "0";
		else if(m_Field.value.length-m_Field.value.indexOf(".") > 3){
			m_Field.value=m_Field.value.substring(0,m_Field.value.indexOf(".") +3)		
		}
	  idiff=10-m_Field.value.length	
		strval=""
		for(i=0;i< 10;i++)	{
			strval=strval+ " "  
			if (i==idiff-1){
				strval=strval+m_Field.value;
				break;
			}
		}
}

function CheckConfig(todo){
	var retval,iKitqty,iTotalqty,iMaxCount;
	iKitqty=document.frmKitAmt.qty.value
	retval=IsNumeric(iKitqty)
	if(document.frmKitAmt.txtTotalKitValue.value < 0){
		alert(getMessage("iValidPriceComb"));
		return;
	}	
	if(retval == 1)	{
		alert(getMessage("iValidQuantity"));
		document.frmKitAmt.qty.value=1;
		document.frmKitAmt.qty.focus();
		return;
	}
	iKitqty = iKitqty*1;
	iMaxCount = AppiMaxItemcount;
	bMaxItem = AppbMaxItem;
	if ((iMaxCount != "") && (bMaxItem.toLowerCase()=="true")){
		iTotalqty = parseInt(sCartItems)
		if(todo=="update")
			iTotalqty=iTotalqty - parseInt(iQty)
		if((iKitqty + iTotalqty)>parseInt(iMaxCount)){
			alert(getMessage("iTotalQtyExceed") + " " + iMaxCount);
			document.frmKitAmt.qty.value=1;
			document.frmKitAmt.qty.focus();
			return;
		}
	}
	document.frmKitInfo.qty.value = document.frmKitAmt.qty.value;
	document.frmKitInfo.configArr.value = configArr;
	document.frmKitInfo.todo.value = todo;
	document.frmKitInfo.submit();
}

function IsNumeric(value){
	var validate=/(^\d+$)/
	if ((validate.test(value)) && (value > 0))
		return 0; 
	else
		return 1; 
}
