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);
}

function isNumeric(val){return(parseFloat(val,10)==(val*1));}

function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
	}
	return true;
}

function isEmail(val){
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val)) return false;
	else return true;
}

function confirm_alert(string) {
	cf = confirm(string);
	if(cf) return true;
	else return false;
}

function show_div(field) 
{
	var style0 = document.getElementById(field).style;
	if(style0.display == 'none') {
	    style0.display = "block";
	} else {
	    style0.display = "none";
	}
}


function formSerialize(form) {
  var inputs = [];
  // Uzmi sva polja za unos sa formulara
  $(":input", form).each(function() {
    // Ignorisi polja koja su disabled
    if ($(this).attr("disabled") != true) {
      switch (this.type) {
        case "button":
          break;
        case "reset":
          break;
        case "submit":
          break;
        case "radio":
          if (this.checked) {
            inputs.push(encodeURIComponent(this.name) + "=" + encodeURIComponent(this.value));
          }
          break;
        default:
          inputs.push(encodeURIComponent(this.name) + "=" + encodeURIComponent(this.value));              
      }
    }
  });
  return inputs.join("&").replace(/%20/g, "+");
}


$(document).ready(function() {

  // Fix za vertical scrollbar page shift problem u Operi
  $("body").height($(window).height()-40);

  // Default tekst za search box
  $("#menu-search input#m2searchinput").toggleVal();

  // Prikaz menija sa uslugama
  $("#menu-main").find(".dropdown").each(function() {  
    var dropdown = $(this);
    var position = dropdown.position(position);
    position.top += 11;
    dropdown.find(".dropdown-link").hover(function() {
      dropdown.find(".dropdown-menu").css({ top: position.top, left: position.left }).show();
    }, function() {
      dropdown.find(".dropdown-menu").hide();
    });
    dropdown.find(".dropdown-menu").hover(function() {
      dropdown.find(".dropdown-menu").css({ top: position.top, left: position.left }).show();
    }, function() {
      dropdown.find(".dropdown-menu").hide();
    });
    dropdown.find(".dropdown-menu li a").click(function() {
      dropdown.find(".dropdown-menu").hide();
    });
  });  
    
  // Podesi osnovne parametre za validaciju svih formulara
  $.validator.setDefaults({
    event: "blur",
    errorElement: "small",
    errorPlacement: function(error, element) {
      element.after(error);
    }
  });
  
});



/********* ajax refresh *********/


var theDIV; // we will set this to an id that actually exists in the page

  var http_request = false;
  function fromServer()
  {
    if( http_request.readyState==4 )
    {
      if( http_request.status==200 )
      {
// results from call to the server appear here
// in http_request.responseText;
        e = document.getElementById(theDIV);
        e.innerHTML = http_request.responseText;
      }
      else
      {
// uncomment next line if you want to show any error messages
        //alert('error: ' + http_request.responseText);
      }
    }
  }

  function makeRequest(url, parameters) 
  {
    http_request = false;
    if( window.XMLHttpRequest ) 
    { 
      // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) 
      { http_request.overrideMimeType('text/html');
      }
    } 
    else if (window.ActiveXObject) 
    { // IE
      try 
      { http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) 
      { try
        { http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
    }
    if (!http_request) 
    {
// uncomment next line if you want to show any error messages
      //alert('Cannot create XMLHTTP instance');
      return false;
    }
    http_request.onreadystatechange = fromServer;
    http_request.open('GET', url + parameters, true);
    http_request.send(null);
  }


  function searchKontinent(n)
  {
    var url = "searchform-ajax.php"

    theDIV='search';
    qry = "?slug_kontinent=" + n;
    makeRequest(url,qry);
  }

  function searchDestinacijaDrzava(n)
  {
    var url = "searchform-ajax.php"

    theDIV='search';
    qry = "?slug_destinacija_drzava=" + n;
    makeRequest(url,qry);
  }

  function searchDestinacijaMesto(n)
  {
    var url = "searchform-ajax.php"

    theDIV='search';
    qry = "?slug_destinacija_mesto=" + n;
    makeRequest(url,qry);
  }

  function searchAgencijaNaziv(n)
  {
    var url = "searchform-ajax.php"

    theDIV='search';
    qry = "?slug_naziv=" + n;
    makeRequest(url,qry);
  }

  function searchAgencijaGrad(n)
  {
    var url = "searchform-ajax.php"

    theDIV='search';
    qry = "?slug_grad=" + n;
    makeRequest(url,qry);
  }

  function searchReset(slug_kontinent, slug_destinacija_drzava)
  {
    var url = "searchform-ajax.php"

    theDIV='search';
    qry = "?slug_kontinent=" + slug_kontinent + "&slug_destinacija_drzava=" + slug_destinacija_drzava + "&slug_destinacija_mesto=-&slug_naziv=-&slug_grad=-";
    makeRequest(url,qry);
  }
  
  function gup( name )
  {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
      return "";
    else
      return results[1];
  }
  