// Funkcija za serijalizaciju podataka sa formulara
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, "+");
}

// Funkcija se izvrsava kada je DOM spreman
$(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);
    }
  });
  
});


/* ****************************************** */

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 check_all_post(field) {
	for(i=0; i<field.length; i++) {
		field[i].checked = true;
	}
}
function uncheck_all_post(field) {
	for(i=0; i<field.length; i++) {
		field[i].checked = false;
	}
}



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;
    style0.display = "block";
}

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


function credit_refresh()
{
	var url = "credit_refresh.php"
	
	theDIV='div_credit';
	qry = "?";
	makeRequest(url,qry);	
}

function credit_refresh_menu()
{
	var url = "credit_refresh.php"
	
	theDIV='div_credit_menu';
	qry = "?";
	makeRequest(url,qry);	
}

/*
function ads_delete(id)
{
	// document.getElementById('ajax0').style.display = 'block';
	var url = "_action.php"
	
	theDIV='ajax0';
	qry = "?action=ads_delete&id="+id;
	makeRequest(url,qry);	
}
*/

function CheckFormImport() {
	error = false;

	if(document.FormImport.file.value == "") {
		error = true;
	}
	
	if(error) {
		alert_text = "";
		alert_text += "Niste uneli .CSV fajl";
		alert(alert_text);
		return false;
	}
	document.getElementById('searchSubmit').value = 'Sacekajte trenutak...';
	document.getElementById('searchSubmit').disabled = true;
	document.getElementById('img-ajax-loader').style.display = "block";
}

function hide_img_ajax_loader() {
	document.getElementById('img-ajax-loader-usluga_id').style.display = "none";
	document.getElementById('img-ajax-loader-paket_grupa_id').style.display = "none";
// document.getElementById('img-ajax-loader-paket_id').style.display = "none";
}

function show_plus_minus(id_plus, id_minus) {
	document.getElementById(id_plus).style.display = "block";
	document.getElementById(id_minus).style.display = "none";
}

function checkFormAd() {
	error = false;

	if(document.FormAd.category.value == "") error = true;
	if(document.FormAd.title.value == "") error = true;
	if(document.FormAd.description.value == "") error = true;
	if(document.FormAd.contact_selected.value == "") error = true;
	if(document.FormAd.contact.value == "") error = true;
	if(document.FormAd.location.value == "") error = true;
	
	if(error) {
		alert_text = "";
		alert_text += "NISTE POPUNILI ISPRAVNO SVA POLJA U FORMI\n\n";
		if(document.FormAd.category.value == "") alert_text += "- Niste popunili kategoriju oglasa.\n";
		if(document.FormAd.title.value == "") alert_text += "- Niste popunili naslov oglasa.\n";
		if(document.FormAd.description.value == "") alert_text += "- Niste popunili tekst oglasa.\n";
		if(document.FormAd.contact_selected.value == "") alert_text += "- Niste obelezili kontakt na oglasu.\n";
		if(document.FormAd.contact.value == "") alert_text += "- Niste popunili kontakt oglasa.\n";
		if(document.FormAd.location.value == "") alert_text += "- Niste obelezili grad (lokaciju).\n";
		
		alert(alert_text);
		return false;
	}
	document.getElementById('FormAdSubmit').value = 'Sacekajte trenutak...';
	document.getElementById('FormAdSubmit').disabled = true;
	// document.getElementById('img-ajax-loader').style.display = "block";
}
