//- cartSimple.List FUNCTIONS


function cartSimpleActivateTab(id){
	var curent = 1;

		handleEtape(id);



		if(id==2){
			cartSimpleLoadProductsIframe();
		}

		if(id==3){
			//alert('aici');
			$('nav_next_container').hide();
			cartSimpleLoadPaymentIframe();
		}else{
			$('nav_next_container').show();
		}



		// this while was right bellow handleEtape()
		while($('steptab'+curent)){
			if(curent==id ){
				$('steptab'+curent).addClassName('cart_tab_selected');
				$('steppanel'+curent).show();
			}else{
				$('steptab'+curent).removeClassName('cart_tab_selected');
				$('steppanel'+curent).hide();
			}
			curent++;
		}



}

function cartSimpleUpdateTabsStatus(obj){
	var temp = $(obj).id.split('_');
	delivery_method = temp[1];
	//console.log("DLVR="+delivery_method);
	// cartSimple_count1 = products , cart_count2 = gifts
	if(cart_count2>0)
		$('steptab2').setStyle("color:#fff;");
	else
		$('steptab2').setStyle("color:#b64d7f;");

	//console.log(cartSimple_has_shipped);
	if(cartSimple_has_shipped>0)
		$('steptab3').setStyle("color:#fff;");
	else
		$('steptab3').setStyle("color:#b64d7f;");
}

function cartSimpleValidateTabTransition(newtabid,direction){
	//console.log('simpleValidateTabs newtabid='+newtabid+' ,dir='+direction);
	var ret=false;
	var errmsg = '';
	switch(newtabid){
		case 1: // Authentication
			ret = true;
		break;
		case 2: // Shipping
			//console.log('count1='+cart_count1+', count2='+cart_count2+', delivery_method='+delivery_method+',dir='+direction);
			if(((cart_count1<=0 && cart_count2<=0 ) && direction != 'undefined') || (delivery_method>0 && direction != 'undefined') || (cart_has_shipped<=0)){
				var newtab = direction<0?1:3;
				//console.log('skipping the gifts tab...'+newtab);
				cartSimpleCertainTab(newtab,direction);
				return false;
			}
			ret = true;
		break;
		case 3: // Payment
			//console.log('aici validari');

			// verificare selectie adresa ?
			ret = true;
		break;
	}
	if(errmsg!='')
		alert(errmsg);
	return ret;
}

function cartSimpleCertainTab(tabid,direction){
	var nt = tabid;
	if(cartSimpleValidateTabTransition(nt,direction)){
		if($('steptab'+nt)){
			current_tab = nt;
			cartSimpleActivateTab(current_tab);
		}
	}
}
function cartSimpleNextTab(){
	var nt = current_tab + 1;
	if(cartSimpleValidateTabTransition(nt,1)){
		if($('steptab'+nt)){
			current_tab = nt;
			cartSimpleActivateTab(current_tab);
		}
	}
}

function cartSimplePrevTab(){
	var nt = current_tab - 1;
	if(nt>1){
		if(cartSimpleValidateTabTransition(nt,-1)){
			if($('steptab'+nt)){
				current_tab = nt;
				cartSimpleActivateTab(current_tab);
			}
		}
	}else{
		//alert(ROOT_WWW + "index.php?object=cart&method=List");
		window.top.location = ROOT_WWW + "/index.php?object=cart&method=SelectDelivery";
	}
}

function cartSimpleShowAddAddressForm(newaddress_form){
	$('toggler_'+newaddress_form).hide();
	$('div_'+newaddress_form).show();
}

function cartSimpleCancelAddAddress(newaddress_form){
	$(newaddress_form).reset();
	$('div_'+newaddress_form).hide();
	$('toggler_'+newaddress_form).show();
}

function cartSimpleSubmitAddAddress(newaddress_form){
	var city = $(newaddress_form).elements['rec_city'] ? $(newaddress_form).elements['rec_city'].value : '';
	var zip = $(newaddress_form).elements['rec_zip'] ? $(newaddress_form).elements['rec_zip'].value : '';
	var address1 = $(newaddress_form).elements['rec_address1'] ? $(newaddress_form).elements['rec_address1'].value : '';
	var address2 = $(newaddress_form).elements['rec_address2'] ? $(newaddress_form).elements['rec_address2'].value : '';
	if($(newaddress_form).elements['rec_addressid'])
		var addressid = $(newaddress_form).elements['rec_addressid'].value;
	if($(newaddress_form).elements['rec_boxname'])
		var boxname = $(newaddress_form).elements['rec_boxname'].value;
	cartSimpleCreateNewAddressBox(city,zip,address1,address2,addressid,boxname);
}

function cartSimpleCreateNewAddressBox(city,zip,address1,address2,addressid,boxname){
	var url = 'index.php?object=cart&method=NewAddress&city='+city+'&zip='+zip+'&address1='+address1+'&address2='+address2+'&addressid='+addressid+'&boxname='+boxname+'&boxtype=1&cartid=0';
	var self = this;
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: cartSimpleCreateNewBoxExecute
	});
	$('div_newaddress_form').hide();
	$('toggler_newaddress_form').show();
}


function cartSimpleCreateNewBoxExecute(transport){
	var response = eval('('+transport.responseText+')');
	var id = response.id;
	if(parseInt(id)>0){
		var parent = $('myaddresses');
		var contents = "<input type='radio' name='the_address' id='address_"+id+"' style='float:left;padding-top:2px;' onclick='cartSimpleSelectAddress("+id+");' value='"+id+"'>";
		contents += "<label for='address_"+id+"' style='cursor:pointer;font-weight:bold;float:left;'>" + response.city + "</label><br>";
		contents += response.address1 + ' ' + response.address2 + ' - ' + response.zip;
		var div = new Element('div',{'id':'address_'+id}).update(contents);
		div.setStyle('padding-bottom:7px;');
		parent.appendChild(div);
	}else{
		switch(parseInt(id)){
			case 0:	alert("Error: You are not logged in. The address was not added!");			break;
			case -1:alert("Error: You did not specify the City. The address was not added!");	break;
			case -2:alert("Error: You did not specify the Zip Code. The address was not added!");break;
			case -3:alert("Error: You did not specify the Address. The address was not added!");break;
		}
	}
}


function cartSimpleUpdateBoxForm(){
	if($('rec_addressid')){
		var addressid = $('rec_addressid').value;
		if(addressid>0 || addressid==''){
			$('support_rec_city').hide();
			$('support_rec_address1').hide();
			$('support_rec_address2').hide();
			$('support_rec_zip').hide();
		}else{
			$('support_rec_city').show();
			$('support_rec_address1').show();
			$('support_rec_address2').show();
			$('support_rec_zip').show();
		}
	}
}

function cartSimpleTabs_addresses(){
	$('pickups').hide();
	$('addresses').show();
	$('div_newaddress_form').hide();
	$('toggler_newaddress_form').show();
}

function cartSimpleTabs_pickups(){
	$('addresses').hide();
	$('pickups').show();
	$('div_newaddress_form').hide();
	$('toggler_newaddress_form').hide();
}




function cartSimpleLoadProductsIframe(){
	//$('loading').show();
	$('iframe1').src = "index.php?object=cart&method=ShippingIframeSimple&whattoshow=products";
	$('iframe1').observe('Load',function(){
		//$('loading').hide();
	});
}


function cartSimpleLoadPaymentIframe(){
	//$('loading').show();
	$('iframe3').src = "about:blank";
	setTimeout('$("iframe3").src = "index.php?object=cart&method=PayIframe";',100);
	//$('iframe3').src = "index.php?object=cart&method=PayIframe";
	$('iframe3').observe('Load',function(){
		//$('loading').hide();
	});
}

function cartSimpleadminTOGGLE(cartSimpleid){
	$('orderdetails_'+cartSimpleid).toggle();
	if($('orderdetails_'+cartSimpleid).visible()){
		$('orderheader_'+cartSimpleid).setStyle('background:#C6006F;color:#fff;');
	}else{
		$('orderheader_'+cartSimpleid).setStyle('background:#fff;color:#000;');
	}
}

function cartSimpleSelectAddress(addressid){
	var url = "index.php?object=cart&method=SetAllToAddress&addressid="+addressid;
	new Ajax.Request(url, {
		  method: 'post'
		  ,onSuccess: function(transport){}
	});
}
