



var productSelectedHash = new Hash();
productSelectedHash.set('hsp', '0'); 
productSelectedHash.set('dph', '0'); 
productSelectedHash.set('dtv', '0'); 
//productSelectedHash.set('hsp', 'product_102'); 
//productSelectedHash.set('dph', 'product_202'); 
//productSelectedHash.set('dtv', 'product_302'); 

var productsHashHSP = new Hash();
productsHashHSP.set('product_101', '0'); 
//productsHashHSP.set('product_102', '1'); 
productsHashHSP.set('product_102', '0'); 
productsHashHSP.set('product_103', '0'); 
productsHashHSP.set('product_104', '0'); 
productsHashHSP.set('product_105', '0'); 

var productsHashDPH = new Hash();
productsHashDPH.set('product_201', '0'); 
//productsHashDPH.set('product_202', '1'); 
productsHashDPH.set('product_202', '0'); 
productsHashDPH.set('product_203', '0'); 

var productsHashDTV = new Hash();
productsHashDTV.set('product_301', '0'); 
//productsHashDTV.set('product_302', '1'); 
productsHashDTV.set('product_302', '0'); 
productsHashDTV.set('product_303', '0'); 

var versandkosten = new Number(25);


//sprache
var clientlang = 2;	

var clientlangpath = "de";

if(clientlang == 1){
	var clientlangpath = "de";
}
if(clientlang == 2){
	var clientlangpath = "fr";
}
if(clientlang == 3){
	var clientlangpath = "it";
}
if(clientlang == 4){
	var clientlangpath = "en";
}

	
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
	
		
	

Event.onReady(function() {

	//createProductHTML();
	//dynamic creation not possible because of IE Bug. addBehavior does not work on generated code.
	//tmpl objects created for holding the product box codes.

	
	//default checkbox settings	
	//$('cb_product_102').checked = true;
	//$('cb_product_202').checked = true;
	//$('cb_product_302').checked = true;
	
	
	calculateBundle();
	
	$('product_internet_more_mainwrapper').hide();
	$('product_phone_more_mainwrapper').hide();
	$('product_tv_more_mainwrapper').hide();
	$('v_toggle_less').hide();
	
	
	
	
});






function createProductHTML(){
	//**not used because of IE Bugs**

	//productsDetail
	//loop durch produkte array
	//print html mit html template oder create Elements
	
	
	//productsDetail.each(function(pair) {   alert(pair.key + ' = "' + pair.value + '"'); }); 
	
	
/*
	//Example
	<div class="product_wrapper_internet" id="product_104">
    	<div class="box_txt">
        Fiber Power Internet 50
		</div>
        <div class="box_price"><input type="checkbox" class="cb_product_hsp" id="cb_product_104" >
	        CHF 85.-
		</div>
	</div>
*/

	
	

	
	SortIt(initProductArray,1,'Key');
	
	var wodiv = "";
	var woclass = "";

	for( var i=0; i<initProductArray.length; i++){
		//alert(initProductArray[i].Key);
		
		if(initProductArray[i].ProductCat == "hsp" && initProductArray[i].Bundling == "B"){
			//alert('hsp B')
			wodiv = "product_internet_mainwrapper";
			woclass = "product_wrapper_internet";
			
		};
		
		if(initProductArray[i].ProductCat == "hsp" && initProductArray[i].Bundling == "noB"){
			//alert('hsp noB')
			wodiv = "product_internet_more_mainwrapper_productdiv";
			woclass = "product_wrapper_internet_more";
		};
		
		if(initProductArray[i].ProductCat == "dph" && initProductArray[i].Bundling == "B"){
			//alert('dph B')
			wodiv = "product_phone_mainwrapper";
			woclass = "product_wrapper_phone";
		};
		
		if(initProductArray[i].ProductCat == "dph" && initProductArray[i].Bundling == "noB"){
			//alert('dph noB')
			wodiv = "product_phone_more_mainwrapper_productdiv";
			woclass = "product_wrapper_phone_more";
		};
		
		if(initProductArray[i].ProductCat == "dtv" && initProductArray[i].Bundling == "B"){
			//alert('dtv B')
			wodiv = "product_tv_mainwrapper";
			woclass = "product_wrapper_tv";
		};
		
		if(initProductArray[i].ProductCat == "dtv" && initProductArray[i].Bundling == "noB"){
			//alert('dtv noB')
			wodiv = "product_tv_more_mainwrapper_productdiv";
			woclass = "product_wrapper_tv_more";
		};
		
		
		
		var myElementProduct = new Element('div', { 'class': woclass, 'id': initProductArray[i].Key });
		var myElementBoxTxt = new Element('div', { 'class': 'box_txt'}).update(initProductArray[i].TextLong);
		var myElementBoxPrice = new Element('div', { 'class': 'box_price'}).update(' CHF ' + initProductArray[i].Price + '.-');
		var myElementBoxInput = new Element('input', { 'class': 'cb_product_'+initProductArray[i].ProductCat, 'type': 'checkbox', 'id': 'cb_'+initProductArray[i].Key});
		
		myElementBoxPrice.insert({'top':myElementBoxInput});
		
		myElementProduct.insert({'bottom':myElementBoxTxt});
		myElementProduct.insert({'bottom':myElementBoxPrice});
		
		//$('product_internet_mainwrapper').insert({'bottom':myElementProduct});
		$(wodiv).insert({'bottom':myElementProduct});
		
		
	}
	
		
	

	
}


Event.addBehavior({  

	'div.product_wrapper_internet:click' : function(e){ onClickSelectProduct('hsp',this.readAttribute('id'),this) },
	'div.product_wrapper_phone:click' : function(e){ onClickSelectProduct('dph',this.readAttribute('id'),this) },
	'div.product_wrapper_tv:click' : function(e){ onClickSelectProduct('dtv',this.readAttribute('id'),this) },
	
	'#v_toggle:click' : function(e){ onClickToggleProducts(); Event.stop(e); },
	'#v_toggle_less:click' : function(e){ onClickToggleProducts(); Event.stop(e); },

	'div.product_wrapper_internet_more:click' : function(e){ onClickSelectProduct('hsp',this.readAttribute('id'),this) },
	'div.product_wrapper_phone_more:click' : function(e){ onClickSelectProduct('dph',this.readAttribute('id'),this) },
	'div.product_wrapper_tv_more:click' : function(e){ onClickSelectProduct('dtv',this.readAttribute('id'),this) }
	
		
});



function onClickToggleProducts(){

	$('v_toggle_less').toggle();
	
	new Effect.toggle('product_internet_more_mainwrapper', 'slide', 'duration: 0.01');	
	new Effect.toggle('product_phone_more_mainwrapper', 'slide', 'duration: 0.01');	
	new Effect.toggle('product_tv_more_mainwrapper', 'slide', 'duration: 0.01');	

	$('v_toggle').toggle();
		
	
}



function clearCheckboxesCat(pcat){

	//Setze alle Checkboxen auf nicht checked
	$$('.cb_product_'+pcat).each(function(element){
		element.checked = false;
	});
	
		
	if(pcat == "hsp"){
		productsHashHSP.each(function(pair) {   productsHashHSP.set(pair.key,'0');  }); 	
		productSelectedHash.set(pcat,'0');
	}
	if(pcat == "dph"){
		productsHashDPH.each(function(pair) {   productsHashDPH.set(pair.key,'0');  }); 	
		productSelectedHash.set(pcat,'0');
	}
	if(pcat == "dtv"){
		productsHashDTV.each(function(pair) {   productsHashDTV.set(pair.key,'0');  }); 	
		productSelectedHash.set(pcat,'0');
	}
	
	
}


function onClickSelectProduct(pcat,pprodid,me){

	//alert("click handler"+me);
	
	if(pcat == "hsp"){ 
		if(productsHashHSP.get(pprodid) == '1'){
			clearCheckboxesCat(pcat);
		}
		else{
			clearCheckboxesCat(pcat);
			$('cb_'+pprodid).checked = true;
			productsHashHSP.set(pprodid,'1');
			productSelectedHash.set(pcat,pprodid);
		}
	}
	if(pcat == "dph"){ 
		if(productsHashDPH.get(pprodid) == '1'){
			clearCheckboxesCat(pcat);
		}
		else{
			clearCheckboxesCat(pcat);
			$('cb_'+pprodid).checked = true;
			productsHashDPH.set(pprodid,'1');
			productSelectedHash.set(pcat,pprodid);
		}
	}
	if(pcat == "dtv"){ 
		if(productsHashDTV.get(pprodid) == '1'){
			clearCheckboxesCat(pcat);
		}
		else{
			clearCheckboxesCat(pcat);
			$('cb_'+pprodid).checked = true;
			productsHashDTV.set(pprodid,'1');
			productSelectedHash.set(pcat,pprodid);
		}
	}
	
	
	//alert(productSelectedHash.toQueryString());
	//Preis berechnen
	calculateBundle();
	
	//AnmeldeLink mit Preselected Productcode Parameter updaten
	updateRegisterLink();
	
}


	
function updateRegisterLink(){

	//ToDo Einbau Live Stage Switch
	//if config.getValue("LIVE") == "YES"
	
	//UAT Version
	//var anmeldeLinkUrlPre = 'https://order-uat.cablecom.ch/ocp-webapp/prospect.portal?redirectcwp';
	
	//LIVE Version
	var anmeldeLinkUrlPre = 'https://order.cablecom.ch/ocp-webapp/prospect.portal?redirectcwp';
	
	
	var anmeldeLinkUrlPost = '&language='+clientlangpath+'&reccode='+gup('reccode')+'&zanpid='+gup('zanpid')+'';
	
	
	var productCodeHsp = (productSelectedHash.get('hsp') != 0) ? productsDetail.get(productSelectedHash.get('hsp')+'_productcode') : '';
	var productCodeDph = (productSelectedHash.get('dph') != 0) ? productsDetail.get(productSelectedHash.get('dph')+'_productcode') : '';
	var productCodeDtv = (productSelectedHash.get('dtv') != 0) ? productsDetail.get(productSelectedHash.get('dtv')+'_productcode') : '';
	
	var anmeldeLinkUrl = anmeldeLinkUrlPre + '&p1='+productCodeHsp+'&p2='+productCodeDph+'&p3='+productCodeDtv+'' + anmeldeLinkUrlPost;
	
	$('button_anmelden').setAttribute('href', anmeldeLinkUrl);


}


	
function calculateBundle(){

	
	//sind 2P oder 3P gew&#228;hlt?
	
	var anzp = 0;
	var anzPBundling = 0;

	var productList = new Array();
	
	if(productSelectedHash.get('hsp') != '0'){
		productList[anzp] = new Product(productsDetail.get(productSelectedHash.get('hsp')+'_text'),productsDetail.get(productSelectedHash.get('hsp')+'_price'),false,false,0,1,0,productsDetail.get(productSelectedHash.get('hsp')+'_bundling'),productsDetail.get(productSelectedHash.get('hsp')+'_onetimeprice') );
		anzp++;		 
		if(productsDetail.get(productSelectedHash.get('hsp')+'_bundling') == "B"){
			anzPBundling++;
		}
	}
	if(productSelectedHash.get('dph') != '0'){
  		productList[anzp] = new Product(productsDetail.get(productSelectedHash.get('dph')+'_text'),productsDetail.get(productSelectedHash.get('dph')+'_price'),false,false,0,2,0,productsDetail.get(productSelectedHash.get('dph')+'_bundling'),productsDetail.get(productSelectedHash.get('dph')+'_onetimeprice') );
		anzp++;		 
		if(productsDetail.get(productSelectedHash.get('dph')+'_bundling') == "B"){
			anzPBundling++;
		}
	}
	if(productSelectedHash.get('dtv') != '0'){
  		productList[anzp] = new Product(productsDetail.get(productSelectedHash.get('dtv')+'_text'),productsDetail.get(productSelectedHash.get('dtv')+'_price'),false,false,1,3,0,productsDetail.get(productSelectedHash.get('dtv')+'_bundling'),productsDetail.get(productSelectedHash.get('dtv')+'_onetimeprice') );
		anzp++;		 
		if(productsDetail.get(productSelectedHash.get('dtv')+'_bundling') == "B"){
			anzPBundling++;
		}
	}

	//displayProductList();

		
	if(anzp == 0){ 
		setBasketTotal(0)
	};	
	
	if(anzp == 1){ 
		setBasketTotal(productList[0].Price);
	}
	
	if(anzp == 2 || anzp == 3 ){ 
	
			
		if(anzPBundling == 0 || anzPBundling == 1){ 
			var totalPrice = 0;
			productList.each(function(p) {
			   //alert(p.Price);
			   totalPrice+=p.Price;
			}); 
			
			setBasketTotal(totalPrice);
		
		 };	
		
		
			
		if(anzPBundling == 2){ 
		
			SortIt(productList,1,'Bundling',1,'Price',-1,'IsDtv');
			//displayProductList();
			
				
			productList[0].Half = true;
			productList[0].HalfPrice = productList[0].Price / 2;
			
			var totalPrice = productList[0].HalfPrice + productList[1].Price;
			
			
			if(anzp == 3){ 
				var totalPrice = productList[0].HalfPrice + productList[1].Price + productList[2].Price;
			}
			setBasketTotal(totalPrice);
			
			productList.sort(sortBasketOrderHandler);
			//displayProductList();
			
				
		};		
		
			
		if(anzPBundling == 3){ 
		
			SortIt(productList,1,'Bundling',1,'Price',-1,'IsDtv');
			//displayProductList();
			
				
			productList[0].HalfPrice = productList[0].Price;
			//productList[0].Price = 0;
			productList[0].Gratis = true;
			
			var totalPrice = productList[1].Price + productList[2].Price;
			setBasketTotal(totalPrice);
			
			productList.sort(sortBasketOrderHandler);
			
		};
	};
	

	
		
	clearBasketItem('hsp');
	clearBasketItem('dph');
	clearBasketItem('dtv');
	//$('basket_onetime_price').update('CHF ' + new Number('0').toFixed(2));
	$('basket_onetime_price').update('CHF ' + versandkosten.toFixed(2));
	
	
		
	var p2count = 0;
	if(productSelectedHash.get('hsp') != '0'){
		setBasketItem('hsp',productList[p2count].Text,productList[p2count].Price,productList[p2count].Gratis,productList[p2count].Half,productList[p2count].HalfPrice);
		p2count++;
	}
	if(productSelectedHash.get('dph') != '0'){
		setBasketItem('dph',productList[p2count].Text,productList[p2count].Price,productList[p2count].Gratis,productList[p2count].Half,productList[p2count].HalfPrice);
		p2count++;
	}
	if(productSelectedHash.get('dtv') != '0'){
	
	
		
		//for the moment only dtv has onetime prices
		if(productList[p2count].OneTime != 0){
			var onetimeItemPrice = new Number(productList[p2count].OneTime);
			onetimeTotalPrice = onetimeItemPrice + versandkosten;
			$('basket_onetime_price').update( 'CHF ' + onetimeTotalPrice.toFixed(2)  );
			setBasketItem('dtv',productList[p2count].Text,productList[p2count].OneTime,productList[p2count].Gratis,productList[p2count].Half,productList[p2count].HalfPrice);
		}
		else{
			setBasketItem('dtv',productList[p2count].Text,productList[p2count].Price,productList[p2count].Gratis,productList[p2count].Half,productList[p2count].HalfPrice);
		}
		
		p2count++;
	}
	
		
		
	function displayProductList() {
		var alerttext = "";
		for(var i=0; i < productList.length; i++) {
			//alerttext+= "Text = "+productList[i].Text+", Price = "+productList[i].Price+", Gratis = "+productList[i].Gratis+", IsDtv = "+productList[i].IsDtv+", BasketOrder = "+productList[i].BasketOrder+", Half = "+productList[i].Half+", HalfPrice = "+productList[i].HalfPrice+", Bundling = "+productList[i].Bundling+", OneTime = "+productList[i].OneTime + "\n";
			alerttext+= "B:"+productList[i].Bundling+" P:"+productList[i].Price+" D:"+productList[i].IsDtv+"\n";
		}
		alert(alerttext);
	}
	
	
}

	
function Product(Text, Price, Gratis, Half, IsDtv, BasketOrder, HalfPrice, Bundling, OneTime){

	this.Text = Text;
	this.Price = parseInt(parseFloat(Price));
	this.Gratis = Gratis;
	this.Half = Half;
	this.IsDtv = parseInt(parseFloat(IsDtv));
	this.BasketOrder = parseInt(parseFloat(BasketOrder));
	this.HalfPrice = parseInt(parseFloat(HalfPrice));
	this.Bundling = Bundling;
	this.OneTime = parseInt(parseFloat(OneTime));
}



	
function sortBasketOrderHandler(thisObject,thatObject) {	
	if (thisObject.BasketOrder > thatObject.BasketOrder)
	{
		return 1;
	}
	else if (thisObject.BasketOrder < thatObject.BasketOrder)
	{
		return -1;
	}
	return 0;
}



	
function setBasketItem(pcat,ptext,pprice,pgratis,phalf,phalfprice){
	

	if(clientlang == 1){
		var textgratis = "GRATIS";
		var textHalf = "2er-Paket";
	}
	if(clientlang == 2){
		var textgratis = "GRATUIT";
		var textHalf = "paquet de 2";
	}
	if(clientlang == 3){
		var textgratis = "GRATUITA";
		var textHalf = "pacchetto da 2";
	}
	if(clientlang == 4){
		var textgratis = "FREE";
		var textHalf = "2-pack";
	}
	
	if(pcat == "hsp"){
		if(pgratis){
			$('basket_item_hsp_text').update('<span class="gratisprod">'+ptext+'</span>'); 
			$('basket_item_hsp_price').update('<span class="gratisprod">'+textgratis+'</span>'); 
		}
		else if(phalf){
			$('basket_item_hsp_text').update(ptext); 
			$('basket_item_hsp_price').update('CHF ' + new Number(pprice).toFixed(2)); 
			$('basket_item_hsp_discount_text').update('<span class="gratisprod">'+textHalf+'</span>'); 
			$('basket_item_hsp_discount_price').update('<span class="gratisprod">- CHF '+ new Number(phalfprice).toFixed(2) +'</span>'); 
		}
		else{
			$('basket_item_hsp_text').update(ptext); 
			$('basket_item_hsp_price').update(' CHF ' + new Number(pprice).toFixed(2) + '')
		}
	}
	if(pcat == "dph"){
		if(pgratis){
			$('basket_item_dph_text').update('<span class="gratisprod">'+ptext+'</span>'); 
			$('basket_item_dph_price').update('<span class="gratisprod">'+textgratis+'</span>'); 
		}
		else if(phalf){
			$('basket_item_dph_text').update(ptext); 
			$('basket_item_dph_price').update('CHF ' + new Number(pprice).toFixed(2)); 
			$('basket_item_dph_discount_text').update('<span class="gratisprod">'+textHalf+'</span>'); 
			$('basket_item_dph_discount_price').update('<span class="gratisprod">- CHF '+ new Number(phalfprice).toFixed(2) +'</span>'); 
		}
		else{
			$('basket_item_dph_text').update(ptext); 
			$('basket_item_dph_price').update(' CHF ' + new Number(pprice).toFixed(2) + '')
		}
	}
	if(pcat == "dtv"){
		if(pgratis){
			$('basket_item_dtv_text').update('<span class="gratisprod">'+ptext+'</span>'); 
			$('basket_item_dtv_price').update('<span class="gratisprod">'+textgratis+'</span>'); 
		}
		else if(phalf){
			$('basket_item_dtv_text').update(ptext); 
			$('basket_item_dtv_price').update('CHF ' + new Number(pprice).toFixed(2)); 
			$('basket_item_dtv_discount_text').update('<span class="gratisprod">'+textHalf+'</span>'); 
			$('basket_item_dtv_discount_price').update('<span class="gratisprod">- CHF '+ new Number(phalfprice).toFixed(2) +'</span>'); 
		}
		else{
			$('basket_item_dtv_text').update(ptext); 
			$('basket_item_dtv_price').update(' CHF ' + new Number(pprice).toFixed(2) + '')
		}
	}
	
	
};

	
function clearBasketItem(pcat){

	if(pcat == "hsp"){
		$('basket_item_hsp_text').update(''); 
		$('basket_item_hsp_price').update(''); 
		$('basket_item_hsp_discount_text').update(''); 
		$('basket_item_hsp_discount_price').update(''); 
	}

	if(pcat == "dph"){
		$('basket_item_dph_text').update(''); 
		$('basket_item_dph_price').update(''); 
		$('basket_item_dph_discount_text').update(''); 
		$('basket_item_dph_discount_price').update(''); 
	}

	if(pcat == "dtv"){
		$('basket_item_dtv_text').update(''); 
		$('basket_item_dtv_price').update(''); 
		$('basket_item_dtv_discount_text').update(''); 
		$('basket_item_dtv_discount_price').update(''); 
	}

};

	
function setBasketTotal(totalprice){
	
	new Effect.Opacity('basket_total_price', { from: 1.0, to: 0.0, duration: 0.15 });
	setTimeout( "$('basket_total_price').update('CHF ' + new Number("+totalprice+").toFixed(2) ) " ,150); 
	new Effect.Opacity('basket_total_price', { from: 0.0, to: 1.0, duration: 0.15, queue: 'end' })
	
};


	
function SortIt(TheArr,us,u,vs,v,ws,w,xs,x,ys,y,zs,z){
// us-zs: 1=asc, -1=desc.  u-z: column-numbers.  See example

  if(u==undefined){TheArr.sort(Sortsingle);} // if this is a simple array, not multi-dimensional, ie, SortIt(TheArr,1): ascending.
  else{TheArr.sort(Sortmulti);}

  function Sortsingle(a,b){
    var swap=0;
    if(isNaN(a-b)){
      if((isNaN(a))&&(isNaN(b))){swap=(b<a)-(a<b);}
      else {swap=(isNaN(a)?1:-1);}
    }
    else {swap=(a-b);}
    return swap*us;
  }

  function Sortmulti(a,b){
  var swap=0;
    if(isNaN(a[u]-b[u])){
      if((isNaN(a[u]))&&(isNaN(b[u]))){swap=(b[u]<a[u])-(a[u]<b[u]);}
      else{swap=(isNaN(a[u])?1:-1);}
    }
    else{swap=(a[u]-b[u]);}
    if((v==undefined)||(swap!=0)){return swap*us;}
    else{
      if(isNaN(a[v]-b[v])){
        if((isNaN(a[v]))&&(isNaN(b[v]))){swap=(b[v]<a[v])-(a[v]<b[v]);}
        else{swap=(isNaN(a[v])?1:-1);}
      }
      else{swap=(a[v]-b[v]);}
      if((w==undefined)||(swap!=0)){return swap*vs;}
      else{
        if(isNaN(a[w]-b[w])){
          if((isNaN(a[w]))&&(isNaN(b[w]))){swap=(b[w]<a[w])-(a[w]<b[w]);}
          else{swap=(isNaN(a[w])?1:-1);}
        }
        else{swap=(a[w]-b[w]);}
        if((x==undefined)||(swap!=0)){return swap*ws;}
        else{
          if(isNaN(a[x]-b[x])){
            if((isNaN(a[x]))&&(isNaN(b[x]))){swap=(b[x]<a[x])-(a[x]<b[x]);}
            else{swap=(isNaN(a[x])?1:-1);}
          }
          else{swap=(a[x]-b[x]);}
          if((y==undefined)||(swap!=0)){return swap*xs;}
          else{
            if(isNaN(a[y]-b[y])){
              if((isNaN(a[y]))&&(isNaN(b[y]))){swap=(b[y]<a[y])-(a[y]<b[y]);}
              else{swap=(isNaN(a[y])?1:-1);}
            }
            else{swap=(a[y]-b[y]);}
            if((z=undefined)||(swap!=0)){return swap*ys;}
            else{
              if(isNaN(a[z]-b[z])){
                if((isNaN(a[z]))&&(isNaN(b[z]))){swap=(b[z]<a[z])-(a[z]<b[z]);}
                else{swap=(isNaN(a[z])?1:-1);}
              }
              else{swap=(a[z]-b[z]);}
              return swap*zs;
} } } } } } }












