var baseurl ='http://www.passportjunkie.com/';


$( document ).ready( function(){
	
	
	
		$("#CitiesIndexForm").submit(function () {	
 		
			if($(this).validation()==true)
			{	
	     	 if((document.getElementById("From_City").value)==(document.getElementById("To_City").value))
	     	 { 
			 	alert('From City and To City cannot be same.');
			 	return false; 
	     	 }
			  return true; 
			}
			return false; 	
	});
	
	/*
	
		$("#emailPopForm").submit(function () {	
 		
			if($(this).validation()==true)
			{		      	   
	   var email_field = document.getElementById("TO").value;
				var email = email_field.split(',');
				var len   = email.length;					
					for (var i = 0; i < email.length; i++) 
					{
					   if (!checkEmail(email[i])) 
					   {
					    document.getElementById("TO").focus();
					    return false;
					   }
					}
					
	   if(checkEmail(document.getElementById("FROM").value))
			 {			 
			  if(document.getElementById("a_falg").value==0)
   		{   		 
   		 checkCap(document.getElementById("Verification_Code").value);	   		 
   		 return false;   		 
   		}	 
   		return true;
			 }
			 else
			 { 
			  document.getElementById("FROM").focus();
			  return false;	 
			 }	
			}		
			return false; 	
	});
	*/

	
	$('.continent-links a').click(function(){
		var id = $(this).attr('id');
		len = id.length;
		id = id.substring(0,len-1);
		
		
			if(id=="europe")
		{  
		 	$(this).parent().parent().find('a').removeClass('active');
	  	$(this).addClass('active');
			 $('.map').show();
    $('#' + id).hide();
		}
		
		if(id=="n-america")
		{ 
		 	$(this).parent().parent().find('a').removeClass('active');
		  $(this).addClass('active');
			 $('.map').show();
    $('#' + id).hide();
		}			
		
		return false;
	})
	
	
	
	$('.continent a').click(function(){
		var id = $(this).attr('id');		
		len = id.length;
		id = id.substring(0,len-1);
		
	
		
		if(id=="europe")
		{ 	
		  $('.continent-links').find('a').removeClass('active');
		  $('#europe1').addClass('active'); 
			 $('.map').show();
    $('#' + id).hide();
		}
		
		if(id=="n-america")
		{ 
		  $('.continent-links').find('a').removeClass('active');
		  $('#n-america1').addClass('active'); 
			 $('.map').show();
    $('#' + id).hide();
		}			
			
		return false;
	})
	

	
	
});



/* Global Functions */

(function($) {

$.fn.validation = function() {		
 var error = 0;
	$('.required', this).each(function() {																														
		var input = $(':input', this).attr("value");		
		var display_name = $(':input', this).attr("id");		
		input = jQuery.trim(input);
		
		if (input == "") {
			//$('span.error-message', this).remove();
			//$(this).append('<span class="error-message"><span class="error"></span></span>');
		//	$('span.error', this).html(' mandatory field.');
			$(':input', this).addClass("error-highlight");
			display_name = ((display_name.replace('_',' ')).replace('_',' ')).replace('_',' ');
			error++;
			$(':input', this).val('');
			$(':input', this).focus();
			alert(display_name+' is a required field');			
		 return false;		
		} else {
			$('span.error-message', this).remove();
			$(':input', this).removeClass("error-highlight");
		}
	});
	
	if (error == 0) {
		return true;
	} else {
		return false;
	}
};



})(jQuery);





//used in add contact, to check availability of a specific email
function checkCap(cap)
{
	 $.get(baseurl+"cities/checkCap/" + cap, function(data){		
	  
	  var val = data.split("<");				
	  if(val[0]=="")	  
	  {	
	     document.getElementById("a_falg").value=1;	 	     
	     $('form:Emailfom').submit(); 
	  }
	  else
	  {	   
	   alert('Verification Code doesn\'t match.'); 
	  }
		});		
}



/* email address validation */
function checkEmail(emailString)
{
	splitVal = emailString.split('@');
	
	if(splitVal.length <= 1) 
	{
		alert("Please Enter A Valid Email Address ");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) 
	{
		alert("Please Enter A Valid Email Address ");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) 
	{
		alert("Please Enter A Valid Email Address ");
		return false;
	}
	
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) 
	{
		alert("Please Enter A Valid Email Address ");
		return false;
	}
	return true;
}


//mutiple email checking

function multiEmail(email_field) 
{
var email = email_field.split(',');
var len   = email.length;

for (var i = 0; i < email.length; i++) {
   if (!checkEmail(email[i])) {
    return false;
   }
}

return true;
} 

function getregion()
{
 var city = document.getElementById("Region").value;
 getcitiesresult(city);
}

function getcitiesresult(city)
 {
	  $.get(baseurl+"cities/getCities/" + city, function(data){	
	  if (data != '' ){	  
   $("#To_City").html(data).show();
   $("#From_City").html(data).show();   
  }
		});		
}

//used in add contact, to check availability of a specific email
function getcities(city)
 {
   $("#To_City").hide();
   $("#From_City").hide();
   $("#resultto").html('Retrieving ...');
   $("#resultfrom").html('Retrieving ...');
	  $.get(baseurl+"cities/getCities/" + city, function(data){	
	  if (data != '' ){	  
   $("#To_City").html(data).show();
   $("#From_City").html(data).show();
   $("#resultto").html('');
   $("#resultfrom").html('');
  }
		});		
}

function valicheck()
{
 if(document.getElementById("TO").value=="")
 {
  alert('TO field is a required field');
  document.getElementById("TO").focus();
  return false;
 }
 
 if(document.getElementById("FROM").value=="")
 {
  alert('FROM field is a required field');
  document.getElementById("FROM").focus();
  return false;
 }
 
 if(document.getElementById("Verification_Code").value=="")
 {
  alert('Code field is a required field');
  document.getElementById("Verification_Code").focus();
  return false;
 }
 
 
    var email_field = document.getElementById("TO").value;
				var email = email_field.split(',');
				var len   = email.length;					
					for (var i = 0; i < email.length; i++) 
					{
					   if (!checkEmail(email[i])) 
					   {
					    document.getElementById("TO").focus();
					    return false;
					   }
					}
					
					if(!checkEmail(document.getElementById("FROM").value))
					{
					 document.getElementById("FROM").focus();
					 return false;
					}
					
					 checkCap(document.getElementById("Verification_Code").value);	 
						return false;
					
}


	function facekey(aid,did,cid)
	{
	  $.get(baseurl+"cities/sharefacebook/"+aid+"/"+did+"/"+cid+"/", function(data){		
	  
	  var val = data.split("<");				
	  if(val[0]=="")	  
	  {	  }
	  else
	  {	  }
		});			  
	}