// $Id$   
Drupal.behaviors.location = function (context) 
{
  	$('a.countryLink:not(.countryLink-processed)', context).click(function () {
    var c_test = this;
		// This function will get exceuted after the ajax request is completed successfully
		var updateStates = function(data) 
		{
		  // The data parameter is a JSON object. The â€œstatesâ€? property is the list of states items that was returned from the server response to the ajax request.
		  $('#div_states').html(data.states);
       $('#div_city').html('<br>');
		 // eraseCookie('country_name');
		 // createCookie('country_name',data.countryname,14);
      $('.countries_block a').removeClass('active');
      $(c_test).addClass('active');
		  Drupal.attachBehaviors();
      return false;
		}
		$.ajax({
		  type: 'POST',
		  url: this.href, // Which url should be handle the ajax request. This is the url defined in the <a> html tag
		  success: updateStates, // The js function that will be called upon success request
		  dataType: 'json', //define the type of data that is going to get back from the server
		  data: 'js=1' //Pass a key/value pair
		});
		return false;  // return false so the navigation stops here and not continue to the page in the link
	}).addClass('countryLink-processed');
	

	
	$('a.stateLink:not(.stateLink-processed)', context).click(function () {
    var s_test = this;
		// This function will get exceuted after the ajax request is completed successfully
		var updateCity = function(data) 
		{
		  // The data parameter is a JSON object. The â€œcityâ€? property is the list of states items that was returned from the server response to the ajax request.
		  $('#div_city').html(data.city);
		//  eraseCookie('region');
		//  createCookie('region',data.regionname,14);
      $('.states_block a').removeClass('active');
      $(s_test).addClass('active');
		  Drupal.attachBehaviors();
      return false;
		}
		$.ajax({
		  type: 'POST',
		  url: this.href, // Which url should be handle the ajax request. This is the url defined in the <a> html tag
		  success: updateCity, // The js function that will be called upon success request
		  dataType: 'json', //define the type of data that is going to get back from the server
		  data: 'js=1' //Pass a key/value pair
		});
		return false;  // return false so the navigation stops here and not continue to the page in the link
	}).addClass('stateLink-processed');
	
	
	$('a.cityLink:not(.cityLink-processed)', context).click(function () {
		// This function will get exceuted after the ajax request is completed successfully
		var updateCookie = function(data) 
		{
		  // The data parameter is a JSON object. The â€œcityâ€? property is the list of citys items that was returned from the server response to the ajax request.
		  //$('#div_city').html(data.city);
		//  eraseCookie('city');
		//  createCookie('city',data.cityname,14);
		  window.location=data.redirec;
		  Drupal.attachBehaviors();
      	  return false;
			
		}
		$.ajax({
		  type: 'POST',
		  url: this.href, // Which url should be handle the ajax request. This is the url defined in the <a> html tag
		  success: updateCookie, // The js function that will be called upon success request
		  dataType: 'json', //define the type of data that is going to get back from the server
		  data: 'js=1' //Pass a key/value pair
		});
		return false;  // return false so the navigation stops here and not continue to the page in the link
	}).addClass('cityLink-processed');
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}