﻿var ItemList="",PreItemList="",OptionItemList="",kitList="",RemoveItemList="",EWTList="";
var shoppingItem = {
	itemList : {},
	hisAction : {'radio' : {}, 'checkbox' : {}, 'select' : {}},
	addonTotal : 0
};

var isLoaded=false;
var isChecked=true;

var itemInfoBase = function() {
	this.item = null;
	this.price = 0
};

var AddMoreItem = function(o) {
	var tmpValue = null;
	var item = null;
	var name = null;
	var type = null;
	var price = 0;
	var needUpdate = false;
	var removelist=null;
	switch(o.type) {
	case 'radio':
	case 'hidden':
		tmpValue = o.value.split('$');
		item = tmpValue[0];
		price = tmpValue[1].replace(/[^\d]/ig,'') - 0;
		
		
		if(shoppingItem.hisAction['radio']!=null)
		{
			if (shoppingItem.hisAction['radio'][o.name] != null) {			
				if (shoppingItem.hisAction['radio'][o.name]['item'] == item) {
					o.checked = false;							
				} else {
					needUpdate = true;
				};
				if(shoppingItem.addonTotal !=0)
				{
					shoppingItem.addonTotal -= shoppingItem.hisAction['radio'][o.name]['price'];
					delete shoppingItem.itemList[shoppingItem.hisAction['radio'][o.name]['item']];
				};
	
			} else {
				needUpdate = true;
			};
		
			shoppingItem.hisAction['radio'][o.name] = new itemInfoBase();
				
			if (needUpdate) {			
				shoppingItem.hisAction['radio'][o.name]['price'] = price;
				shoppingItem.hisAction['radio'][o.name]['item'] = item;
			};
		};
		break;

	case 'checkbox':
		tmpValue = o.value.split('$');
		item = tmpValue[0];		
		price = tmpValue[1].replace(/[^\d]/ig,'') - 0;
		
		if(item.indexOf(".")>-1){
			var vremove = item.split(".");
			removelist = $(vremove[0]+"_"+vremove[1]);
		
			if(removelist){
				if(o.checked){
					RemoveItemList=removelist.value;
				}else{
					RemoveItemList = "";
				};
			};
		};
		
		if(!Web.Environment.Browser.isSafari()){
			if (shoppingItem.itemList.propertyIsEnumerable(item)) {	
				if (!o.checked) {
					if(shoppingItem.addonTotal !=0)	{						
						shoppingItem.addonTotal -= price;
						delete shoppingItem.itemList[item];
					};
				} else {
					needUpdate = true;
				};
			} else {
					needUpdate = true;
			};
		}else{
			if (!o.checked) {
				if(shoppingItem.addonTotal !=0)
				{
					shoppingItem.addonTotal -= price;
					delete shoppingItem.itemList[item];
				};
			} else {
				needUpdate = true;
			};
		};

		break;

	case 'select-one':		
		tmpValue = o.options[o.selectedIndex].value.split('$');
		item = tmpValue[0];
		price = tmpValue[1].replace(/[^\d]/ig,'') - 0;
		

		var chkname = o.name.substr(0,o.name.length-2);
		var ochk = $(chkname);
		var oimg = $(item);
		
		if(oimg){		
			oimg.style.display="block";
		};
		if(item=="0"){
			if(ochk){
				ochk.checked = false;
				};			
			}else{
				if(ochk){
					ochk.checked = true;
				};
		};
		if($(chkname+"_p")){
			var priceHTML;
			priceHTML = "$"+ tmpValue[1];
			if(tmpValue.length==3){
				if(tmpValue[1]!=tmpValue[2]){
					priceHTML = "$"+ tmpValue[1]+"&nbsp;<span style=\"text-decoration: line-through;color:#7a7a7a;\">$"+	tmpValue[2]+"</span>";
				};
			};
			$(chkname+"_p").innerHTML =priceHTML;
		};
		
		ShowItemDetail(o,item);
		
		if(shoppingItem.hisAction['select']!=null)
		{
			if (shoppingItem.hisAction['select'][o.name] != null) {
				if(shoppingItem.addonTotal !=0)
				{
					shoppingItem.addonTotal -= shoppingItem.hisAction['select'][o.name]['price'];
					delete shoppingItem.itemList[shoppingItem.hisAction['select'][o.name]['item']];
				};
			};
		};

		shoppingItem.hisAction['select'][o.name] = new itemInfoBase();
		shoppingItem.hisAction['select'][o.name]['price'] = price;
		shoppingItem.hisAction['select'][o.name]['item'] = item;
		needUpdate = true;
		break;

	default:
		break;
	};

	if (needUpdate) {
		shoppingItem.itemList[item] = true;		
		shoppingItem.addonTotal += price;
	};
	
	var tmpAddonTotal = shoppingItem.addonTotal + '';		
	if (tmpAddonTotal.length == 1) {tmpAddonTotal = '00' + tmpAddonTotal}
	if (tmpAddonTotal.length == 2) {tmpAddonTotal = '0' + tmpAddonTotal}
	
	var tmpLen = tmpAddonTotal.length;		
	tmpArray = [];
	for (var i = 0; i < tmpLen; ++i) {
		tmpArray.push(tmpAddonTotal.charAt(i));
	}	;
	var tmpAddon = [tmpArray[--tmpLen], tmpArray[--tmpLen],'.'];
	for (var i = 1; i<= tmpLen; ++i) {
		tmpAddon.push(tmpArray[tmpLen-i]);
		if (i % 3 == 0 && i < tmpLen) {
			tmpAddon.push(',');
		};
	};
	tmpAddon.reverse();
	document.getElementById('addontotal').innerHTML = tmpAddon.join('');
	AutoCheckAddItemtoCartEssential(o);
};

var AutoCheckAddItemtoCartEssential = function(o){
    var chkAddItemtoCart = $("AddItemToCart");
	if(chkAddItemtoCart){ 
		if(o==chkAddItemtoCart){
			return;
		};
		
		var needChecked=false;
        var mainItem=document.frmPrdDetail.MainItemList.value;
       
        for(var item in shoppingItem.itemList ){
			if(location.href.indexOf("SNET-")!=-1){
				if(item != "0" && item.indexOf("SNET-")==-1 && item!=mainItem){
					needChecked=true;
					break;
				};		
			}else{
				if(item != "0" && item!=mainItem){
					needChecked=true;
					break;
				};
            };
		};
		chkAddItemtoCart.checked = needChecked;
	
		if(isLoaded){
			var isInList=false;
			for(var item in shoppingItem.itemList ){
				if(item==mainItem){
					isInList=true;
					break;
				};
			};

			if(needChecked){
				if(!isInList){
					AddMoreItem(chkAddItemtoCart,1);
				};
			}else{
				if(isInList){
					AddMoreItem(chkAddItemtoCart,1);
				};
			};
		};	
    };
};

var ClearAddOnsEssential = function(iInput,vIsReset){
    var bAddItemtoCartState = $("AddItemToCart").checked;
    ClearAddOns(iInput);    
    if(vIsReset){        
       bAddItemtoCartState = false;        
   };
   $("AddItemToCart").checked = bAddItemtoCartState;
};


var ClearAddOns = function (iInput)
{
	/* Reset object value */
	ItemList="";
	PreItemList="";
	OptionItemList="";
	RemoveItemList = "";
	if(iInput==0){
		EWTList = "";
	};
	kitList="";
	if(iInput==0){
		shoppingItem = {
		itemList : {},
		hisAction : {'radio' : {}, 'checkbox' : {}, 'select' : {}},
		addonTotal : 0
		};
	};
	var elems = document.frmPrdDetail.elements;
	/* reset all controls	*/
	for (var i=0; i<elems.length; i++) {	    
	    if( elems[i].name=="AddItemToCart"){
	        continue;
	    }
	    
		if (elems[i].type == 'radio' || elems[i].type == 'checkbox') {
			if( iInput ==1 && elems[i].name=="EWTRadio"){
				continue;				
			}else{
			    elems[i].checked = false;
			};
		};
		if(elems[i].type=='select-one'){
			elems[i].selectedIndex=0;
			AddMoreItem(elems[i]);
			ShowItemDetail(elems[i],"0");
		};
	};
	if(iInput==0){	    
		document.getElementById('addontotal').innerHTML = "0.00";
	};
};

var SelectAllAddOns = function (){
	var elems = document.frmPrdDetail.elements;
	/* reset checkbox	*/
	for (var i=0; i<elems.length; i++) {
		if (elems[i].type == 'checkbox') {
			if(elems[i].checked == true){
				elems[i].checked = false;
				AddMoreItem(elems[i]);
				};
			};
	};	

	for (var i=0; i<elems.length; i++) {
		if ( elems[i].type == 'checkbox') {
			elems[i].checked = true;
			AddMoreItem(elems[i],1);
			};
	};

};

var AddAndCheckoutEssential=function(vItem,vDEPA,vOrderType,vCartNo,vIsAddtoCart,vIsB2B){
    if(!vIsAddtoCart){        
        vItem="";
   }
     AddAndCheckout(vItem,vDEPA,vOrderType,vCartNo,"1",vIsB2B);
}

var AddAndCheckout = function (vItem,vDEPA,vOrderType,vCartNo,isEssentialPage,vIsB2B) {
	var flagAdd = false;
	var itemList = [],ewtList=[];
	var elems = document.frmPrdDetail.elements;
	var login = Web.StateManager.Cookies.get(Web.StateManager.Cookies.Name.LOGIN,"CONTACTWITH");

	if($("AddItemToCart")){
	    if($("AddItemToCart").checked && vItem!="0"){
			if($("combo1")){
				if(!$("combo1").checked){
					itemList.push(vItem);
				};
			}else{
				itemList.push(vItem);
			};
	    };
	};

	/*  unchecked all controls	*/
	for (var i=0; i<elems.length; i++) {
		if (elems[i].type == 'radio' || elems[i].type == 'checkbox') {
			elems[i].checked = false;			
		};
	};	
	
	for (var o in shoppingItem.itemList) {
		if(o!="0"){	
			if(o.indexOf("SNET-")!=-1){			
				ewtList.push(o);
			}else{
				itemList.push(o);
			};
		};
	};
	if(itemList=="0"){
		itemList = "";
	};

	var oldEWTList = (isEssentialPage=="1")?null:$("EWTList");
	var aEWT=[];
	if(oldEWTList && oldEWTList.value.length>0){
		aEWT = oldEWTList.value.split(",");
	};
	
	for(var i=0;i<aEWT.length;i++){
		for(var j=0;j<ewtList.length;j++){
			if(aEWT[i]==ewtList[j]){
				delete aEWT[i];
				delete ewtList[j];
			};
		};
	};

	if(aEWT.length>0){
		if(RemoveItemList.length>0){			
			RemoveItemList += ",";
			RemoveItemList +=  aEWT.length>1?aEWT.join('|9999,'):aEWT[0]+'|9999';
		}else{
			RemoveItemList = aEWT.length>1?aEWT.join('|9999,'):aEWT[0]+'|9999';
		};	
	};
	EWTList = ewtList==""?"":ewtList.join(',');
	
	if(aEWT.length>0 &&EWTList =="" ){EWTList ="SNET-0_"+vItem+"$0.00"; };
	
	document.frmPrdDetail.ItemList.value = itemList==""?"":itemList.join(',');		
	document.frmPrdDetail.RemoveItemList.value=RemoveItemList;
	document.frmPrdDetail.EWTList.value=EWTList;
	document.frmPrdDetail.action += "?DEPA=" + vDEPA + "&submit=ADD&action=checkout&CartNo="+vCartNo;
	
	/* if B2B,check customer login */
	if(vIsB2B == "1" && String.isNullOrEmpty(login)){
		var wces = Web.Config.Environment.SSLPage;
		var url = wces.Login + "?toJump=1&NextPage=" + wces.AddTocart
			+ "?DEPA=" + vDEPA + "&submit=ADD&action=checkout&CartNo=" + vCartNo
			+ "&ItemList=" + document.frmPrdDetail.ItemList.value
			+ "&OptionItemList=" + document.frmPrdDetail.OptionItemList.value
			+ "&PreItemList=" + document.frmPrdDetail.PreItemList.value
			+ "&MainItemList=" + document.frmPrdDetail.MainItemList.value
			+ "&RemoveItemList=" + RemoveItemList
			+ "&EWTList=" + EWTList
		window.location.href = url;
	}else{
		document.frmPrdDetail.submit();
	};
};

var ShowItemDetail = function (o,item){	
	if(o.type != 'select-one'){
		return;
	};
	for(var ilen=1;ilen<o.options.length;ilen++){
		tmpValue = o.options[ilen].value.split("$");
		if(item != tmpValue[0]){
			if($(tmpValue[0])){
				$(tmpValue[0]).style.display="none";
			};
		};
	};
};

var CancelCheckbox = function (o){
	if(!o.checked){
		var objs = $(o.name+ "_s");		
		if(objs){
			objs.selectedIndex=0;
			AddMoreItem(objs);
			ShowItemDetail(objs,"0");
		};
	};
};

var initEssentialCheckbox=function(){
	var o=$("AddItemToCart");
	if(o){
		o.checked=true;
		AddMoreItem(o,1);
	};
	isLoaded=true;
};

var AddSNET=function(id){
   if($(id)){
		if(location.href.indexOf("SNET-")!=-1){
			AddMoreItem($(id),1);
		};
   };
};
