// ad form
function check_ad_form()
{
	var msg = 'Nevyplnili ste nasledovné povinné položky:   \n\n';
	var err = 0;
	
	//fields control
	if(document.forms['add_ad'].ad_title.value == '')
	{
		err = 1;
		msg = msg + "- Názov inzerátu\n";
	}
	if(document.forms['add_ad'].ad_type.selectedIndex == 0)
	{
		err = 1;
		msg = msg + "- Typ inzerátu\n";
	}
	if(document.forms['add_ad'].ad_duration.selectedIndex == 0)
	{
		err = 1;
		msg = msg + "- Trvanie\n";
	}
	/*
	if(document.forms['add_ad'].ad_category.selectedIndex == 0)
	{
		err = 1;
		msg = msg + "- Kategória\n";
	}
	*/
	if(document.forms['add_ad'].ad_description.value == '')
	{
		err = 1;
		msg = msg + "- Text inzerátu\n";
	}
	if(document.forms['add_ad'].ad_price.value == '')
	{
		err = 1;
		msg = msg + "- Cena (Mzda)\n";
	}
	if(document.forms['add_ad'].ad_price.value != '')
	{
		if(isNaN(document.forms['add_ad'].ad_price.value) == true || document.forms['add_ad'].ad_price.value.indexOf(".") != -1)
		{
			err = 1;
			msg = msg + "- Cena (Mzda) - zlý formát, vložte kladné číslo, alebo nulu  \n";
		}
	}
	if(document.forms['add_ad'].ad_location.selectedIndex == 0)
	{
		err = 1;
		msg = msg + "- Lokalita\n";
	}
	
	//images
	if(document.forms['add_ad'].pic1_value.value != '')
	{
		var pic1 = document.forms['add_ad'].pic1_value.value
		pic1 = pic1.toLowerCase();
		
		if(pic1.lastIndexOf(".jpg") == -1 &&  pic1.lastIndexOf(".jpeg")== -1)
		{
			err = 1;
			msg = msg + "- Obrázok 1: chybný typ, musí byť JPG\n";
		}
	}
	if(document.forms['add_ad'].pic2_value.value != '')
	{
		var pic2 = document.forms['add_ad'].pic2_value.value
		pic2 = pic2.toLowerCase();
		
		if(pic2.lastIndexOf(".jpg") == -1 &&  pic2.lastIndexOf(".jpeg")== -1)
		{
			err = 1;
			msg = msg + "- Obrázok 2: chybný typ, musí byť JPG\n";
		}
	}
	if(document.forms['add_ad'].pic3_value.value != '')
	{
		var pic3 = document.forms['add_ad'].pic3_value.value
		pic3 = pic3.toLowerCase();
		
		if(pic3.lastIndexOf(".jpg") == -1 &&  pic3.lastIndexOf(".jpeg")== -1)
		{
			err = 1;
			msg = msg + "- Obrázok 3: chybný typ, musí byť JPG\n";
		}
	}
	
	//contact data
	if(document.forms['add_ad'].username.value == '')
	{
		err = 1;
		msg = msg + "- Meno\n";
	}
	if(document.forms['add_ad'].useremail.value == '' && document.forms['add_ad'].userphone.value == '')
	{
		err = 1;
		msg = msg + "- E-mail, alebo telefón.\n";
	}
	if(document.forms['add_ad'].userpassword.value == '')
	{
		err = 1;
		msg = msg + "- Heslo\n";
	}
	if(document.forms['add_ad'].userpassword.value != '')
	{
		if(document.forms['add_ad'].userpassword.value.length < 4)
		{
			err = 1;
			msg = msg + "- Heslo musí mať aspoň 4 znaky\n";
		}
	}
	
	// error occurs
	if(err == 1)
	{
		alert(msg);
		return false;
	}
	else
	{
		document.forms['add_ad'].submit();
		document.forms['add_ad'].submit_button.style.display='none';
	}
}

// AJAX
function pridajInzerat()
{
     var url = 'add_ad.php';

     if (url != 0)
     {
        if (window.ActiveXObject)
        {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
          httpRequest = new XMLHttpRequest();
        }
        httpRequest.open("POST", url, true);
        httpRequest.onreadystatechange= function () {processRequest(); } ;
        httpRequest.send(null);
      }
      else
      {

        document.getElementById("adResult").innerHTML = "";
        
      }
}
function processRequest()
{
  if (httpRequest.readyState == 4)
  {
    if(httpRequest.status == 200)
    {
      var mistoZobrazeni = document.getElementById("adResult");
      mistoZobrazeni.innerHTML = httpRequest.responseText;
    }
    else
    {
        alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}
//~AJAX

//add to favorites
function bookmarksite(title, url)
{
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
	else
		window.sidebar.addPanel(title, url, "");
}

function countChars(field, counter, fieldMax)
{
	val = field.value;
	if(val.length > fieldMax)
	{
		tmp = val.substring(0, fieldMax);
		field.value = tmp;	
	}
	else
	{
		tmp = val;	
	}
	document.getElementById(counter).innerHTML = (fieldMax - tmp.length);
}
function goto_add_ad_form()
{
	var Sel = document.forms['goto_add_ad_form'].category_name;
	var category_url = Sel.options[Sel.selectedIndex].value;
	parent.location = category_url;
}
function newwin(url, name, width, height)
{
	var WindowObjectReference = null;
	if(WindowObjectReference == null || WindowObjectReference.closed)
	{
    	WindowObjectReference = window.open(url, name, "resizable=yes,scrollbars=yes,status=yes,width=" + width + ",height=" + height);
	}
  	else
  	{
    	WindowObjectReference.focus();
    }
}