var g_lQty = 0;
var g_lPrice = 0;

function passColor(colorname,iNumber){
	var passedColor=0;
	var idx;
	if(typeof(iNumber)=="undefined") iNumber=10;
	for (i = 0; i <= iNumber; i++){
		if (eval ("document.all.tdcolor" + i)){
		eval ("document.all.tdcolor" +i+ ".style.background =\"#FFFFFF\"");
		}
	}

	if (colorname=="0"){
		return;
	}
	passedColor=eval("document.frmProduct.c" + colorname + ".value");
	countColors=Math.floor(passedColor.length/8);

	var colorValues=new Array(30);
	for (i = 0; i < 30-1; i++){
		 colorValues[i] = "";
	}

	for (var i=0;i<countColors;i++){
		idx=CharName(passedColor,",");
			if (idx==""){
				idx=passedColor;
			}
		colorValues[i]=idx;
		passedColor=passedColor.substr(8,passedColor.length-1);
	}

	for (var i=0;i<countColors;i++){
		eval ("document.all.tdcolor" +i+ ".style.background =\"" + colorValues[i] + "\"");
	}
}

function CharName(sInf,sKey){
	var iPos=0;
	var iPosStop=0;
   	var sVal=""; //Return value
	iPos=sInf.indexOf(sKey);
	if (iPos==-1){
		sVal="";
	}
	else{
		sVal=sInf.substr(0,iPos+sKey.length-1);
		iPosStop=sVal.indexOf(';');
			if (iPosStop==-1){
			//End of Info string
				sVal=sVal;
			}
			else{
				//Retrieve information
				sVal=sVal.substr(0,iPosStop);
			}
	}
	return sVal;
}

function AvlCheck(sAct,sClose,sSelectUser,itemID){
	if (typeof(itemID)=='undefined') itemID="";
	var lColor= 0; var lSize=0;
	var sAvl="NoCheck";
	var bLimit= "False";
	var lQtySel= isNaN(parseInt(ctrVal('qty'+itemID)))? 0: parseInt(ctrVal('qty'+itemID));
	var lAvl=0;
	var sMsg="";
	var sMsgAct="";

	if (lQtySel<=0){
		alert('Selected quantity is not valid. Please enter a value greater or equal to 1.');
		ctrVal('qty' + itemID,'1');
		return;
	}
	
	sAvl=ctrVal('txtAvl' + itemID);
	bLimit=ctrVal('txtLimit' + itemID);

	lColor=ctrVal('cboColor' + itemID);
	lColor=isNaN(parseInt(lColor))? 0: parseInt(lColor);
	lSize=ctrVal('cboSize' + itemID);
	lSize=isNaN(parseInt(lSize))? 0: parseInt(lSize);
	
	if(typeof(sClose)!='undefined' && sClose!=''){
		if(!g_bIsDirty || (g_bIsDirty && !confirm('Do you want to save the changes you made?'))){
			document.location=sClose;
			return;
		}
	}

	if(typeof(document.frmProduct.txtTblID)=="object" && document.frmProduct.txtTblID.value==1){ 
		sMsg=EventChk(document.frmProduct,lQtySel,sAvl);
		if (sMsg!="") {alert(sMsg);	return;}
	}
	else{
		if(sAvl=='outofstock') sMsgAct="and select another product";
		if(sAvl!="" && sAvl!='NoCheck'){
			sAvl=GetValueByKey(sAvl,"s_" + lSize + "_c_" + lColor + "=");
			lAvl=isNaN(parseInt(sAvl))? 0: parseInt(sAvl);
		
			if (lAvl>0){
				sMsg="Currently only " + lAvl + " are available.\n" 
				sMsgAct="to limit your order to " + lAvl;
			}	
			else{
				sMsg= "We are temporarily out of stock.\n" 
				sMsgAct= sMsgAct!=''? sMsgAct : "to select another size or color";
			}
		
			if (bLimit=='False'){
					sMsg=sMsg + "Press OK if you would like us to place " + (lQtySel - lAvl) + " on backorder for you, \n" +
							"otherwise press Cancel " + sMsgAct; 
				}
				else  	sMsg= sMsg + (lAvl>0? "Please, order another Quantity." : "Please, try another Size or Color");
							
		
			if (sAvl!='NoCheck'){
				if (lAvl<lQtySel){
					if (bLimit=='True') {
						alert(sMsg);
						return;
					}
					if (mdOpen(sMsg,'350','150','   OK   ','   Cancel   ','','',''))  sAct=sAct + "&bo=yes";
					else{
						sAct=sAct + "&bo=no";
						if(lAvl==0)return;
					}
				}
			}
		}
	}
	if (typeof(document.frmProduct.GroupPrice)!='undefined') PriceCheck(); //Check ItemPrice upon GroupPrices/Promotions Admin part
	if (typeof(sSelectUser)!='undefined' && sSelectUser!='')	mdOpen('Do you want to add item for another user?','250','120','Yes','No','','','','Select User','','','document.frmProduct.action=\'' + sAct + '&sel=yes\'; document.frmProduct.submit();','document.frmProduct.action=\''+sAct +'\'; document.frmProduct.submit();');
	else{ document.frmProduct.action= sAct; document.frmProduct.submit();}
}

function ctrVal(ctrl,val){
	var sval="";
	try{ sval=document.all(ctrl).value; if (typeof(val)!='undefined')document.all(ctrl).value=val;}
	catch (e){}
	return sval;
}

function EventChk(frm,lQtySel,sAvl){
	var lAvl=0;
	var sMsg="";
	var lEventLimit=0;
	var lItemLimit=0;
	var sItem=""
	
	if (typeof(frm.Description)=="object") sItem=eval('frm.Description.value');
	if (typeof(frm.txtEventlimit)=="object") lEventLimit=eval('frm.txtEventlimit.value');
	if (typeof(frm.txtUserLimit)=="object") lItemLimit=eval('frm.txtUserLimit.value');
		
	if (sAvl!='NoCheck' && Math.floor(lQtySel) > Math.floor(sAvl)){
		if (sAvl==0)	sMsg="We are sorry but " + sItem + " registration is sold out";
		else  sMsg= "We are sorry but only " +  Math.floor(sAvl) + " " + sItem + " registrations remain available.";
	}
	if (Math.floor(lQtySel) > Math.floor(lItemLimit) && Math.floor(lItemLimit)>0){
		sMsg="Please, correct the " + sItem + " registrations.\r The limit you should not exceed is " + Math.floor(lItemLimit);
	}
	if(Math.floor(lQtySel) > Math.floor(lEventLimit) && Math.floor(lEventLimit)>0){
		sMsg="Please, correct " + sItem + " registrations.\r Event registrations should not exceed " + lEventLimit;
	}
	if (sMsg!='') frm.qty.focus();
	return sMsg;
}

function GetValueByKey(sInf,sKey){
    var iPos=0;
    var iPosStop=0;
    var sVal=""; //Return value
    iPos=sInf.indexOf(sKey);
    if (iPos==-1){
        sVal="";
    }
    else{
        sVal=sInf.substr(iPos+sKey.length,sInf.length);
        iPosStop=sVal.indexOf(';;');
            if (iPosStop==-1){
            //End of Info string
                sVal=sVal;
            }
            else{
                //Retrieve information
                sVal=sVal.substr(0,iPosStop);
            }
        }
   return sVal;
}

function printArticle()
{
  if (window.print)	setTimeout('window.print();',200);
  else alert("Press 'Ctrl+p' on your keyboard to print article.")
}

function PriceCheck() {
	var res = compPrices();

	if(frmProduct.qty.value && (res!=0 || g_lPrice>0)){
		if(mdOpen('<b>Please select:</b>','250','130',' Re-calculate Price ',' Leave Price As-Is ','','','','Item Group & Promotion Pricing','')){frmProduct.useGroup.value='yes';frmProduct.AdminChg.value='false';}
		else{frmProduct.AdminChg.value='true'; frmProduct.useGroup.value='no';}
		return;
	}
}

function compPrices() {
	if(frmProduct.GroupPrice.value!=frmProduct.TextBoxPrice.value) return 1;
	if(typeof(frmProduct.Promo)!='undefined') return 2; //PromotionCodes applied, ask for recalculation
	return 0;	
}
