// 
// 
/**
* Content code
**/
var hide  = true;

/**
* Changes a dynamically generated list
* @param string The name of the list to change
* @param array A javascript array of list options in the form [key,value,text]
* @param string The key to display
* @param string The original key that was selected
* @param string The original item value that was selected
*/
function changeDynaList( listname, source, key, orig_key, orig_val ) {
	var list = eval( 'document.adminForm.' + listname );

	// empty the list
	for (i in list.options.length) {
		list.options[i] = null;
	}
	i = 0;
	for (x in source) {
		if (source[x][0] == key) {
			opt = new Option();
			opt.value = source[x][1];
			opt.text = source[x][2];

			if ((orig_key == key && orig_val == opt.value) || i == 0) {
				opt.selected = true;
			}
			list.options[i++] = opt;
		}
	}
	list.length = i;
}

/**
* Adds a select item(s) from one list to another
*/
function addSelectedToList( frmName, srcListName, tgtListName ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );
	var tgtList = eval( 'form.' + tgtListName );

	var srcLen = srcList.length;
	var tgtLen = tgtList.length;
	var tgt = "x";

	//build array of target items
	for (var i=tgtLen-1; i > -1; i--) {
		tgt += "," + tgtList.options[i].value + ","
	}

	//Pull selected resources and add them to list
	for (var i=srcLen-1; i > -1; i--) {
		if (srcList.options[i].selected && tgt.indexOf( "," + srcList.options[i].value + "," ) == -1) {
			opt = new Option( srcList.options[i].text, srcList.options[i].value );
			tgtList.options[tgtList.length] = opt;
		}
	}
}

function delSelectedFromList( frmName, srcListName ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	var srcLen = srcList.length;

	for (var i=srcLen-1; i > -1; i--) {
		if (srcList.options[i].selected) {
			srcList.options[i] = null;
		}
	}
}

function moveInList( frmName, srcListName, index, to) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );
	var total = srcList.options.length-1;

	if (index == -1) {
		return false;
	}
	if (to == +1 && index == total) {
		return false;
	}
	if (to == -1 && index == 0) {
		return false;
	}

	var items = new Array;
	var values = new Array;

	for (i=total; i >= 0; i--) {
		items[i] = srcList.options[i].text;
		values[i] = srcList.options[i].value;
	}
	for (i = total; i >= 0; i--) {
		if (index == i) {
			srcList.options[i + to] = new Option(items[i],values[i], 0, 1);
			srcList.options[i] = new Option(items[i+to], values[i+to]);
			i--;
		} else {
			srcList.options[i] = new Option(items[i], values[i]);
	   }
	}
	srcList.focus();
}

function setSelectedValue( frmName, srcListName, value ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	var srcLen = srcList.length;

	for (var i=0; i < srcLen; i++) {
		srcList.options[i].selected = false;
		if (srcList.options[i].value == value) {
			srcList.options[i].selected = true;
		}
	}
}

function getSelectedValue( frmName, srcListName ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	i = srcList.selectedIndex;
	if (i != null && i > -1) {
		return srcList.options[i].value;
	} else {
		return null;
	}
}

function chgSelectedValue( frmName, srcListName, value ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	i = srcList.selectedIndex;
	if (i != null && i > -1) {
		srcList.options[i].value = value;
		return true;
	} else {
		return false;
	}
}

// Form specific functions for editting content images

function showImageProps(base_path) {
	form = document.adminForm;
	value = getSelectedValue( 'adminForm', 'imagelist' );
	parts = value.split( '|' );
	form._source.value = parts[0];
	setSelectedValue( 'adminForm', '_align', parts[1] || '' );
	form._alt.value = parts[2] || '';
	form._border.value = parts[3] || '0';

	previewImage( 'imagelist', 'view_imagelist', base_path );
}

function applyImageProps() {
	form = document.adminForm;
	if (!getSelectedValue( 'adminForm', 'imagelist' )) {
		alert( "Select and image from the list" );
		return;
	}
	value = form._source.value + '|'
	+ getSelectedValue( 'adminForm', '_align' ) + '|'
	+ form._alt.value + '|'
	+ parseInt( form._border.value );
	chgSelectedValue( 'adminForm', 'imagelist', value );
}

function previewImage( list, image, base_path ) {
	form = document.adminForm;
	srcList = eval( "form." + list );
	srcImage = eval( "document." + image );
	var fileName = srcList.options[srcList.selectedIndex].text;
	var fileName2 = srcList.options[srcList.selectedIndex].value;
	if (fileName.length == 0 || fileName2.length == 0) {
		srcImage.src = 'images/blank.gif';
	} else {
		srcImage.src = base_path + fileName;
	}
}

/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
*/
function checkAll( n ) {
	var f = document.adminForm;
	var c = f.toggle.checked;
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'f.cb' + i );
		if (cb) {
			cb.checked = c;
			n2++;
		}
	}
	if (c) {
		document.adminForm.boxchecked.value = n2;
	} else {
		document.adminForm.boxchecked.value = 0;
	}
}

/**
*/
function listItemTask( id, task ) {
	var f = document.adminForm;
	cb = eval( 'f.' + id );
	if (cb) {
		cb.checked = true;
		submitbutton(task);
	}
	return false;
}

function isChecked(isitchecked){
	if (isitchecked == true){
		document.adminForm.boxchecked.value++;
	}
	else {
		document.adminForm.boxchecked.value--;
	}
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitbutton(pressbutton) {
	submitform(pressbutton);
}

/**
* Submit the admin form
*/
function submitform(pressbutton){
	document.adminForm.task.value=pressbutton;
	try {
		document.adminForm.onsubmit();
		}
	catch(e){}
	document.adminForm.submit();
}

/**
* Getting radio button that is selected.
*/
function getSelected(allbuttons){
	for (i=0;i<allbuttons.length;i++) {
		if (allbuttons[i].checked) {
			return allbuttons[i].value
		}
	}
}

/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

//==	object glbGetElementById (string strName)
//
//==		returns the element object contained in the document where the name is equal 
//==		to string strName. designed to be cross-browser compatible.

function glbGetElementById (strName) {
	var objRetVal;
	var blnUseForm = false;
	if (document.getElementById) {					//==	Check for getElementById method
		objRetVal = document.getElementById(strName);
	} else {
		if (document.all) {							//==	Check for the document.all collection
			objRetVal = document.all[strName];
		} else {									//==	Search document forms for element
			blnUseForm = true;
		}
	}
	
	if (objRetVal) {
		if (objRetVal.type) {								//==	If object is a radio button we need to access it using
			if (objRetVal.type.toLowerCase() == "radio") {	//==	the document.formname.elementname notation to retrive the radioset array
				eval ("objRetVal = document." + objRetVal.form.name + "." + strName);
			}
		}
	}

	if (blnUseForm) {
		for (var lngFormCounter=0; !objRetVal && lngFormCounter < document.forms.length; lngFormCounter++) {
			for (var lngElementCounter=0; !objRetVal && lngElementCounter < document.forms[lngFormCounter].elements.length; lngElementCounter++) {
				var objElement = document.forms[lngFormCounter].elements[lngElementCounter];
				if (objElement.name == strName) {			//==	Found element 
					objRetVal = document.forms[lngFormCounter].elements[strName];
				}
			}
		}			
	}
	return objRetVal;
}
//==	string getFieldValue (object objField) 
//
//==		returns the value of the select element object passed in to
//==		the function. returns an empty string if the object is null, 
//==		not a select or	does not have a value selected.

function getFieldValue(objField) {
	if (objField) {
		if (objField.type) {
			if (objField.type.toLowerCase() == "select-one") {			//==	Field is a select drop-down. Get the selected option value
				if (objField.selectedIndex > -1) {
					return objField.options[objField.options.selectedIndex].value;
				}
			}
			if (objField.type.toLowerCase() == "radio") {				//==	Field is a radio box, but not checked so don't return a value
				if (!objField.checked) {
					return "";
				}
			}
		}
		if (objField.length && objField[0]) {						//==	Field is an radioset array. Find radio button that is checked
			for (var intOption = 0; intOption < objField.length; intOption++) {	
				if (objField[intOption].checked) {
					return objField[intOption];
				}
			}
		}
		if (objField.value) {								//==	Standard field. Use the value property
			return objField.value;
		}
	}
	return "";
}

//==	bool isValidEmail (string strEmail)
// 
//==		returns true if is string strEmail is a valid email address.
//== 		otherwise returns false.

function isValidEmail(strEmail) {
	var regex = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;

	return regex.test(strEmail);
}

function isValidPassword(strPassword) {

    if (strPassword.length<7 ) return false;
    else return true;
	
}
function isValidNumber(strNumber) {
	var numInput = "+-0123456789(). ";					//==	String containing valid characters
	
	if (strNumber == "") return false;
	
	for(var k = 0; k < strNumber.length; k++) {
		if(numInput.indexOf(strNumber.charAt(k)) == -1)	{
			return false;
		}
	}
	return true;
}

function ok_Phone(phform){
     
      if(isValidNumber(phform)){
            return true;
        }
        else{
            
            return false;
        }
    }
    
function showLogo(strLogo,strSponsor){
	var html="<title>::"+strSponsor+"::</title>";
	html+="<table><tr><td bgcolor='#ffffee' align=center><font color=blue><a href='javascript: self.close()'>Zatvori Prozor</a></font></td></tr>";
	html+="<tr><td><img src='../images/sponsors/"+strLogo+"' border=1></td></tr></table>";
	prozor=window.open("","prozor","location=1,status=1,scrollbars=1,  width=300,height=300");
	prozor.document.write(html);
}
function showPic(strPic,strTitle,strClose){
	
	var html="<title>::"+strTitle+"::</title>";
	html+="<table width=100% height='100%'><tr><td bgcolor='#ffffee' align=center valign=middle><font color=blue><a href='javascript: self.close()'>"+strClose+"</a></font></td></tr>";
	html+="<tr><td><img src='images/big/"+strPic+"' border=0></td></tr></table>";
	
	prozor=window.open("","prozor","location=1,status=1,scrollbars=1,  width=650 ,height=650, toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1  ");
	prozor.document.write(html);
}

function isValidURL(argvalue){
	 if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("http://") == -1)
    return false;
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;

  argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;

  if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  return true;

}

function showdiv(divid){
 
 var divs = document.getElementsByTagName('div'); 
 for(i=0;i<divs.length;i++){
if(divs[i].id.match(divid) ){//if they are 'see' divs
  if (document.getElementById) // DOM3 = IE5, NS6
   divs[i].style.visibility="visible";// show/hide
  else
  if (document.layers) // Netscape 4
   document.layers[divs[i]].display = 'visible';
  else // IE 4
    document.all.divs[i].visibility = 'visible';
} else {
     if (document.getElementById && divs[i].id.indexOf('dstac')!=-1)
      divs[i].style.visibility="hidden";
     else
     if (document.layers && divs[i].id.indexOf('dstac')!=-1) // Netscape 4
     document.divs[i].visibility = 'hidden';
     else 
     if (divs[i].id.indexOf('dstac')!=-1)// IE 4
     document.getElementById(divs[i]).style.visibility = 'hidden';
    }
}
}
function setLyr(obj,lyr)
{
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	newY += 40;
	newX+=20
	var x = new getObj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
	showdiv(lyr);
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	window.status = printstring;
	return curtop;
}


function getObj(name)
{
 if (document.getElementById)
 {
	   this.obj = document.getElementById(name);
	   this.style = document.getElementById(name).style;
 }
 else if (document.all)
 {
	   this.obj = document.all[name];
	   this.style = document.all[name].style;
 }
 else if (document.layers)
 {
	   if (document.layers[name])
	   {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	   }
	   else
	   {
	    this.obj = document.layers.testP.layers[name];
	    this.style = document.layers.testP.layers[name];
	   }
 }
}

function clearForm(){
	form = document.adminForm;
	form.menu_title.value="";
	form.page_title.value="";
	document.getElementById("page_content").innerHTML="";
}

function check_form_kom() {
	
	if ($F('user_name').blank()){
	 alert("Unesite  ime!");
	 $('user_name').focus();
	 return false;
	}
	if ($F('user_email').blank()){
	 alert("Unesite  email!");
	 $('user_email').focus();
	 return false;
	} 
	if(!isValidEmail($F('user_email'))){ 
	 alert("Unesite ispravan email!");
	 $('user_email').focus();
	 return false;		
	}
	if ($F('user_content').blank()){
	 alert("Unesite  komentar!");
	 $('user_content').focus();
	 return false;
	} 

	alert ("Vas komentar je uspesno zabelezen. Hvala.")
	return true;
	
}
function check_form() {
	
	if ($F('ime').blank()){
	 alert("Unesite  ime!");
	 $('ime').focus();
	 return false;
	}
	if ($F('prezime').blank()){
	 alert("Unesite  prezime!");
	 $('prezime').focus();
	 return false;
	}
	if ($F('adresa').blank()){
	 alert("Unesite  adresu!");
	 $('adresa').focus();
	 return false;
	} 	
	if ($F('grad').blank()){
	 alert("Unesite  grad!");
	 $('grad').focus();
	 return false;
	} 	
	if ($F('email').blank()){
	 alert("Unesite  email!");
	 $('email').focus();
	 return false;
	} 
	if(!isValidEmail($F('email'))){ 
	 alert("Unesite ispravan email!");
	 $('email').focus();
	 return false;		
	}

	if ($F('tel_mob').blank()){
	 alert("Unesite  mobilni telefon!");
	 $('tel_mob').focus();
	 return false;
	} 
	/*alert ("Uspesno ste se registrovali. Hvala.");*/
	return true;
	
}

function setrate(val,id_page,ip,vec){

	var url = 'ajax/stars.php';
	var params = "val="+val+"&id_page="+id_page+"&ip="+ip+"&vec="+vec;
	var ajax = new Ajax.Updater(
	{success: 'stars'},
	url,
	{method: 'get', parameters: params, onFailure: reportError});

}
function reportError(request) {
	$F('stars') = "Error";
}

function showImage(obj,id_art){
	
var bigPic=document.getElementById('b');
var link=document.getElementById('link_big');
var link2=document.getElementById('link_big_zoom');
var str = obj.href.replace ("middle", "big");

bigPic.setAttribute('src',obj.href);



link.setAttribute('href',"javascript:showPic('"+id_art+"','Velika slika - In Corner','Zatvori');");
link2.setAttribute('href',"javascript:showPic('"+id_art+"','Velika slika - In Corner','Zatvori');");
/*
	var test_w= "images/big/"+strPic.width;
	var test_h= "images/big/"+strPic.height;

link.setAttribute('onclick',"popupWindow('"+str+"','Velika slika - In Corner', "+test_w+", "+test_w+", NO);");	
*/	

}
  function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }
function changeClass(sender, newClass)
{
	sender.className = newClass;
}
