// JavaScript Document
var present_area_covered;

function areas_expanded(arg){
	itineraryId=arg;
	$('#main_div').html("<table width='0%' border='0'><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2'><strong  >Select Travel Areas</strong><br> Select areas that your holiday will take you to. This helps us narrow down our list of travel agents to those who cover your destination.<br>Be as general or specific as you choose</td></tr><tr><td colspan='2' width='100%' style=' padding:10px;' id='areascoveres_td'><div  id='areas_main_div' style='border:1px solid #ccc; height:450px; overflow:auto;;'> </div></td></tr><tr align='right'><td align='right' style='text-align:right; padding-right:10px'><input type='image' src='"+ROOT_PATH+"images/save.gif' onclick='areas_submit()'></td></tr></table>");
	$.get(ROOT_PATH+'aj_request.php?aj=user.aj_areas_expand&itineraryId='+itineraryId,'', function(response){
																								   
		eval("var tot = "+response);
		var arr=tot['tot_pla'];
		var pla_db=tot['pla_db'];

		$(arr).each(function(i, n){
			var res=n['places'];
			var  html='';
			
			$(res).each(function(j,o){
						html += "<div ><span style='overflow:hidden' title='"+o['placename1']+"'><input type='checkbox' id='check_id"+o['placeID']+"' name='area_check' value='"+o['placeID']+"' /><a href='javascript:void(0)'  class='place-explorer' id = 'area_link"+o['placeID']+"' onclick=\"areascovered_explore('"+o['placeID']+"')\" >"+o['placename1']+"</a></span><div style='margin-left:10px; display:none' id='areas_div"+o['placeID']+"'></div><div id='areas_div2"+o['placeID']+"'></div></div>";
			});
			
			if(n['placeId']==''){
				$("#areas_main_div").html(html);
			}else{
				$("#areas_div"+n['placeId']).html(html);
				$("#areas_div"+n['placeId']).css('display', '');
				$("#areas_div2"+n['placeId']).css('display','none');
			}
		});
			
		$(pla_db).each(function(i,n){ $("#check_id"+n).attr('checked',true); })	
	});
}



function areascovered_explore(placeId){
	if($("#areas_div"+placeId).html()==''){
		$.get(ROOT_PATH+'aj_request.php?aj=places.aj_explorer&id='+placeId,'',areascovered_exp_response);
	}else{
		if($("#areas_div"+placeId).css('display')!='none'){
			$("#areas_div"+placeId).css('display','none');
			$("#areas_div2"+placeId).css('display','');
		}else{
			$("#areas_div"+placeId).css('display','');
			$("#areas_div2"+placeId).css('display','none');
		}
	}
}

function areascovered_exp_response(response){
	eval("var arr = "+response);
	var res=arr['places'];
	var html='';
	for(var i=0; i<res.length; i++){
		html += "<div ><span style='overflow:hidden' title='"+res[i]['placename1']+"'><input type='checkbox' id='check_id"+res[i]['placeID']+"' name='area_check' value='"+res[i]['placeID']+"' /><a href='javascript:void(0)'  class='place-explorer' id = 'area_link"+res[i]['placeID']+"' onclick=\"areascovered_explore('"+res[i]['placeID']+"')\" >"+res[i]['placename1']+"</a></span><div style='margin-left:10px; display:none' id='areas_div"+res[i]['placeID']+"'></div><div id='areas_div2"+res[i]['placeID']+"'></div></div>";
	}
	$("#areas_div"+arr['placeId']).html(html);
	$("#areas_div"+arr['placeId']).css('display','');
	$("#areas_div2"+arr['placeId']).css('display','none');
}


function areas_submit(){
	var id_string='';
	var k=[];
	var area_checks= document.getElementsByName('area_check');
	var wishlistId=$('input[name=area_check]').each(function(){	 if($(this).is(':checked')) k.push($(this).val());  });
	if(k.length > 0){
		$.ajax({type: "POST", url: ROOT_PATH+'aj_request.php?aj=user.addareas_itiner', data:'placeId='+Url.encode(k.join(','))+'&itineraryId='+itineraryId, success:function(resp){
		if(resp=='0'){
			$('#errormsg_areascovered').html('');
			if(checkUserLog())
			{
				setLocation("showPage","journeyplanner.filter_cp_items","itineraryId="+itineraryId);
			}
			
		}else{
			$('#errormsg_areascovered').html('<span class=error>Update Failed Try Again!!</span>');
		}
																																											 }
 ,async: false});
	
	}else{
		alert("Please select Areas where you need to plan your holiday")
	}
}