var div_load = '';
var load_shopcart = "";
function shopping_change(tipo,id,action){
	if(div_load==""){
		variables="&action_item="+action;
		switch(tipo){
			case 'golf': 	variables+="&golf_item="+id+"&twilight=&2pax_buggy=";
							break;
			case 'golf-twilight': 	variables+="&golf_item="+id+"&twilight=1&2pax_buggy=";
							break;
			case 'golf-2pax_buggy': 	variables+="&golf_item="+id+"&2pax_buggy=1&twilight=";
							break;
			case 'hotel': 	variables+="&hotel_item="+id;
							break;
			case 'car': 	variables+="&car_item="+id;
							break;
			case 'all': if(confirm("Do you really want to clear your order?")){
							variables+="&all_item=1";
						}
						break;
		}
		div_load = "div_shopcart";
		MakeRequest('/shopcart/shopcart_change.php',variables);
	}
}
function add_golf(course){
	load_shopcart="head_cart_golf";
	variables="linha_tipo=golf&course_id="+course+"&del_linha_id=&date_game="+document.getElementById('date-feat-gc').value+"&course_item_id=";
	for(var k=0; k<document.getElementsByName('gc_item').length; k++){
		if(document.getElementsByName('gc_item').item(k).checked){
			variables+=document.getElementsByName('gc_item').item(k).value;
			k=document.getElementsByName('gc_item').length;
		}
	}
	document.getElementById('overbox').style.display='block';
	div_load="tailormade";
	MakeRequest('/shopcart/golf.php',variables);
}
function add_green(course){
	load_shopcart="head_cart_golf";
	variables="linha_tipo=golf&course_id="+course+"&del_linha_id=";
	document.getElementById('overbox').style.display='block';
	div_load="tailormade";
	MakeRequest('/shopcart/golf.php',variables);
}
function add_pack(pack){
	load_shopcart="head_cart_golf";
	variables="linha_tipo=golf&pack="+pack+"&del_linha_id=";
	document.getElementById('overbox').style.display='block';
	div_load="tailormade";
	MakeRequest('/shopcart/golf.php',variables);
}
function add_hotel(hotel){
	load_shopcart="head_cart_hotel";
	variables="linha_tipo=hotel&hotel_id="+hotel+"&del_linha_id=";
	document.getElementById('overbox').style.display='block';
	div_load="tailormade";
	MakeRequest('/shopcart/hotel.php',variables);
}
function add_car(car){
	load_shopcart="head_cart_car";
	variables="linha_tipo=car&car_id="+car+"&del_linha_id=";
	document.getElementById('overbox').style.display='block';
	div_load="tailormade";
	MakeRequest('/shopcart/car.php',variables);
}
function muda_linha(item_form, opera, id){
	variables="";
	switch(item_form){
		case 'golf': 	variables+="linha_tipo=golf"; ender="golf";
						break;
		case 'golf_pack':	variables+="linha_tipo=golf_pack"; ender="golf";
							break;
		case 'hotel': 	variables+="linha_tipo=hotel"; ender="hotel";
						break;
		case 'car': 	variables+="linha_tipo=car"; ender="car";
						break;
	}
	document.getElementById('overbox').style.display='block';
	div_load="tailormade";
	if(opera=='del')	variables+='&del_linha_id='+id; else variables+='&del_linha_id=';
	MakeRequest('/shopcart/'+ender+'.php',variables);
}
function transfers(id){
	div_load="loading";
	if(id=="from_to_location"){
		tmp_text="tipo=transfer&linha=0&coluna="+id+"&valor="+document.getElementById(id).value;
		MakeRequest('/shopcart/save_values.php',tmp_text);
	}
	else{
		if(document.getElementById(id).checked==true){
			tmp_text="tipo=transfer&linha=0&coluna="+id+"&valor=1";
			MakeRequest('/shopcart/save_values.php',tmp_text);
		}
		else{
			tmp_text="tipo=transfer&linha=0&coluna="+id+"&valor=0";
			MakeRequest('/shopcart/save_values.php',tmp_text);
		}
	}
}
function nl2br(text){
	text = escape(text);
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}else{
		re_nlchar = '<br />';
	}
	formatedText = unescape( text.replace(re_nlchar,'<br />') );
	stripslashedText = unescape( formatedText.replace(/"/g,"''") );
	return stripslashedText;
}
function save_change(tipo,linha,coluna){
//	alert(coluna+''+linha);
	div_load="loading";
	if(coluna=='newsletter'){
		if(document.getElementById('newsletter0').checked==true)
			document.getElementById('newsletter0').value='1';
		else
			document.getElementById('newsletter0').value='0';
	}
	if( (coluna=='gc-pack')||(coluna=='gc-id')||(coluna=='gc-date')||(coluna=='gitem-id')||(coluna=='gc-players') ){
		document.getElementById('overbox').style.display='block';
		div_load="linha_golf"+linha;
	}
	if(coluna=='h-id')
		div_load="loading";
	if(coluna=='car-id')
		div_load="loading";
	tmp_text="tipo="+tipo+"&linha="+linha+"&coluna="+coluna+"&valor="+nl2br(document.getElementById(coluna+''+linha).value);
	MakeRequest('/shopcart/save_values.php',tmp_text);
}
function hideShopCart(){
	if(div_load==""){
		document.getElementById('overbox').style.display='block';
		div_load="shopcart";
		variables="";
		MakeRequest('/shopcart/shopcart.php',variables);
	}
}
function open_shopcart(){
	if(div_load==""){
		document.getElementById('overbox').style.display='block';
		load_shopcart="head_cart_golf";
		div_load="tailormade";
		variables="";
		MakeRequest('/shopcart/golf.php',variables);
	}
}
function displayShopCart(){
	if(document.getElementById("tailormade").innerHTML=="")
		open_shopcart();
	else
		hideShopCart();
}
function open_hotel(){
	if(div_load==""){
		document.getElementById('overbox').style.display='block';
		div_load="tailormade";
		load_shopcart="head_cart_hotel";
		variables="";
		MakeRequest('/shopcart/hotel.php',variables);
	}
}
function open_car(){
	if(div_load==""){
		document.getElementById('overbox').style.display='block';
		div_load="tailormade";
		load_shopcart="head_cart_car";
		variables="";
		MakeRequest('/shopcart/car.php',variables);
	}
}
function open_details(){
	if(div_load==""){
		document.getElementById('overbox').style.display='block';
		div_load="tailormade";
		load_shopcart="head_cart_info";
		variables="";
		MakeRequest('/shopcart/personal.php',variables);
	}
}
//TRIMS LEADING WHITE SPACES FROM STRINGS
function leftTrim(str){
	strEmpty = true;
	strArray = str.split("");
	str = "";
	for(iPrivate=0;iPrivate<strArray.length;iPrivate++){
		if(strArray[iPrivate] == " " && strEmpty){
			strArray[iPrivate] = "";
		}else{
			strEmpty = false;
		}
		str += strArray[iPrivate];
	}
	return str;
}
function rightTrim(str){
	strEmpty = true;
	strArray = str.split("");
	strArray.reverse();
	str = "";
	for(iPrivate=0;iPrivate<strArray.length;iPrivate++){
		if(strArray[iPrivate] == " " && strEmpty){
			strArray[iPrivate] = "";
		}else{
			strEmpty = false;
		}
		str += strArray[iPrivate];
	}
	strArray = str.split("");
	strArray.reverse();
	str = "";
	for(iPrivate=0;iPrivate<strArray.length;iPrivate++){
		str += strArray[iPrivate];
	}
	return str;	
}
function strTrim(str){
	str = leftTrim(str);
	str = rightTrim(str);
	return str;
}
// VALIDATES EMAILS
function validateEmail(str) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(str) == false) {
      return false;
   }
   return true
}
// VALIDADES THE FORM
function mailer(fields2check){
	if(div_load==""){
		fields = fields2check.split("|");
		for(i=0;i<fields.length;i++){
			fieldName = fields[i].split("_")[0];
			fieldAlert = fields[i].split("_")[1];
			fieldId = document.getElementById(fieldName);
	
			if(!fieldId.value || fieldId.value == fieldAlert || strTrim(fieldId.value) == "")
			{ fieldId.value = fieldAlert; fieldId.focus(); fieldId.select(); return; }
			if(fieldName == 'email0'){
				if(!validateEmail(fieldId.value) && fieldId.value != "fieldAlert"){ document.getElementById('invalidEmail').style.display = "inline"; fieldId.focus(); fieldId.select(); return; }else{ document.getElementById('invalidEmail').style.display = "none"; }
			}
		}
		div_load="tailormade";
		document.getElementById(div_load).innerHTML="";
		document.getElementById('overbox').style.display='block';
		variables="";
		MakeRequest('/shopcart/send.php',variables);
	}
}
function HandleResponse(response){
	if((div_load=="loading")||(div_load=="")){
		document.getElementById('overbox').style.display='none';
		div_load="";
	}
	else{
		document.getElementById(div_load).innerHTML=response;
		document.getElementById('overbox').style.display='none';
		div_load="";
	}
	if(load_shopcart != ""){
		document.getElementById("head_car_title").innerHTML="Hide Shopping Cart";
		document.getElementById("head_cart_golf").className="";
		document.getElementById("head_cart_hotel").className="";
		document.getElementById("head_cart_car").className="";
		document.getElementById("head_cart_info").className="";
		document.getElementById(load_shopcart).className="active";
		load_shopcart="";
	}
}
