//-----------------------------------------------------------------------------
// global.js
//-----------------------------------------------------------------------------

// parse URL arguments
  var args = new Object();

//-----------------------------------------------------------------------------
// Cookie - get name and value
//-----------------------------------------------------------------------------
function getCookie (name){
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen){
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg){
        var endstr = document.cookie.indexOf (";", j);
        if (endstr == -1)
          endstr = document.cookie.length;
        return unescape(document.cookie.substring(j, endstr));
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
   }
   return null;
}

//-----------------------------------------------------------------------------
// Cookie - set name and value
//-----------------------------------------------------------------------------
function setCookie (name, value){
   var argv = setCookie.arguments;
   var argc = setCookie.arguments.length;
   //var expdate = new Date();
   //expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 31));
   var expdate = null;
   var path = '/';
   var domain = null;
   var secure = false;
   document.cookie = name + "=" + escape (value) +
        ((expdate == null) ? "" : ("; expires=" + expdate.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}


//-----------------------------------------------------------------------------
// Global popup window function
//-----------------------------------------------------------------------------
function window_pop(url, name, nheight, nwidth, resize, scroll, status ) {
	var myloc = window.location.href;
	var locarray = myloc.split("/");
	var tmpurl = url;

	delete locarray[(locarray.length-1)];


	if (tmpurl.substring(0,2)=='..'){
		delete locarray[(locarray.length-2)];
		url = url.substring(3,url.length);
	}

	var arraytext = locarray.join("/");
	
	//strip last / from url (http://localhost:8075//)
	if (tmpurl.substring(0,2)=='..'){
		arraytext = arraytext.substring(0,arraytext.length-1);
	}

	url = arraytext + url;
	var popup = false;
	var postop = screen.availHeight/2 - nheight/2 - 50;
	postop = parseInt(postop);
	var posleft = screen.width/2 - nwidth/2;
	posleft = parseInt(posleft);
	// Config for the window
	var config = 'height'      +  '='         +  nheight     +  ','  +
        	       'width'       +  '='         +  nwidth      +  ','  +
		             'toolbar'      +  '=no,'     +
		             'menubar'      +  '=no,'     +
		             'scrollbars'   +  '='+ scroll +','  +
		             'resizable'    +  '='+ resize +','  +
		             'location'     +  '=no,'      +
		          // 'location'     +  '=yes,'      +
		             'directories'  +  '=no,'      +
		             'status'       +  '='+ status +','  +
		          //   'status'       +  '=yes,'  +
		             'screenX'      +  '=300,'     +
		             'screenY'      +  '=300,'     +
		             'top'          +  '=' + postop + ','     +
		             'left'         +  '=' + posleft;

	if(navigator.appName.indexOf("Net") != -1) {
		if(popup) {
			if(popup.document == '') {
				popup.close();
			}
		}
		popup = eval("window.open(url,name,config='" + config + "')");
		popup.focus();
	}
	else if (navigator.appVersion.indexOf("MSIE") != -1) {
		if (popup) {
			if (popup.closed) {
				popup = eval("window.open(url,name,config='" + config + "')");
			}
			else {
				pop_url = new String(popup.location).replace(/^http:\/\/[a-zA-Z0-9.\-]+(\/.*)$/, "$1");
				if (pop_url != url) {
					popup.location = url;
				}
				popup.focus();
			}
		}
		else {
			popup = eval("window.open(url,name,config='" + config + "')");
		//	alert(popup.name);
			popup.focus();
		}
	}
}

//-----------------------------------------------------------------------------
// Open resizable, scrollable dialog with status
//-----------------------------------------------------------------------------
function popup_status(url, name, nheight, nwidth) {
  window_pop(url, name, nheight, nwidth, 'yes', 'no', 'yes' )
}

//-----------------------------------------------------------------------------
// Open resizable dialog
//-----------------------------------------------------------------------------
function pop_up(url, name, nheight, nwidth) {
  window_pop(url, name, nheight, nwidth, 'yes', 'no', 'no' )
}

//-----------------------------------------------------------------------------
// Open resizable and scrollable dialog
//-----------------------------------------------------------------------------
function popup_scroll(url, name, nheight, nwidth) {
  window_pop(url, name, nheight, nwidth, 'yes', 'yes', 'no' )
}

//-----------------------------------------------------------------------------
// get URL parameters & close pop_up window if necessary
//-----------------------------------------------------------------------------
function popup_close(){
  var myloc=window.location.search;
  myloc = myloc.substring(1,myloc.length);
  var locarray = myloc.split("&");
  for (i=1;i<locarray.length;i++){
    if(locarray[i]=="close=true")
    	{
        	opener.location.reload();
        	parent.close();
        }	
    if(locarray[i]=="close=true_submit")
    	{
        	opener.document.fcontract.submit();
        	parent.close();
        }	        
  }
}

//-----------------------------------------------------------------------------
// Preload Images
//-----------------------------------------------------------------------------
// nemacki --------------------------------------------------------------------
	settingsMenuImage = new Image();
	settingsMenuImage.src = "../images/de/menu_settings_down.gif";

	camerasMenuImage = new Image();
	camerasMenuImage.src = "../images/de/menu_cameras_down.gif";

	usersMenuImage = new Image();
	usersMenuImage.src = "../images/de/menu_users_down.gif";
	
	liveMenuImage = new Image();
	liveMenuImage.src = "../images/de/menu_live_down.gif";
	
	historyMenuImage = new Image();
	historyMenuImage.src = "../images/de/menu_history_down.gif";


// srpski ---------------------------------------------------------------------
	settingsMenuImage = new Image();
	settingsMenuImage.src = "../images/sr/menu_settings_down.gif";

	camerasMenuImage = new Image();
	camerasMenuImage.src = "../images/sr/menu_cameras_down.gif";

	usersMenuImage = new Image();
	usersMenuImage.src = "../images/sr/menu_users_down.gif";
	
	liveMenuImage = new Image();
	liveMenuImage.src = "../images/sr/menu_live_down.gif";
	
	historyMenuImage = new Image();
	historyMenuImage.src = "../images/sr/menu_history_down.gif";







//-----------------------------------------------------------------------------
// RollOver Action buttons
//-----------------------------------------------------------------------------
	function rollover(img_name,img_src) {
		document[img_name].src=img_src;
	}

//-----------------------------------------------------------------------------
// Check Browser type
//-----------------------------------------------------------------------------
function isNetscape(){
   // get browser info.
   appName= navigator.appName;
   appLongVer = navigator.appVersion;
   appVer = appLongVer.substring(0, 1);
   // check for Navigator ver 4+.
   if ((appName == "Netscape") && (appVer >= 4)) return true;
   return false;
}

//-----------------------------------------------------------------------------
// Check Browser type
//-----------------------------------------------------------------------------
function isIE(){
   // get browser info.
   appName= navigator.appName;
   appLongVer = navigator.appVersion;
   appVer = appLongVer.substring(0, 1);
   // check for IE ver 4+.
   if ((appName == "Microsoft Internet Explorer") && (appVer >= 4)) return true;
   return false;
}

//-----------------------------------------------------------------------------
// Check for valid IP address
//-----------------------------------------------------------------------------
function CheckIP (ip){
	var splitIP;
	splitIP = ip.split(".");

	// to check that there are dots between the numbers.
	if (splitIP.length == 1)
		return false;
	// to check that there are only 3 dots between the numbers.
	else if (splitIP.length != 4)
		return false;
	// to check that the each part of the ip has a length, i.e. no 2 dots after each other.
	else if ( (splitIP[0].length == 0) | (splitIP[1].length == 0) | (splitIP[2].length == 0) | (splitIP[3].length == 0) )
		return false;
	// to check that each part has a length not greater than 3 digits.
	else if ( (splitIP[0].length > 3) | (splitIP[1].length > 3) | (splitIP[2].length > 3) | (splitIP[3].length > 3) )
		return false;
	// to check on each part that it is not excceeding 255.
	else if ( (splitIP[0] > 255) | (splitIP[1] > 255) | (splitIP[2] > 255) | (splitIP[3] > 255) )
		return false;
	// to check that the first part's value is not equal to zero.
	else if (splitIP[0] == 0)
		return false;
	// to check that the not all parts value are equal to zero.
	else if ( (splitIP[0] == 0) & (splitIP[1] == 0) & (splitIP[2] == 0) & (splitIP[3] == 0) )
		return false;
	// to check that the not all parts value are equal to 255.
	else if ( (splitIP[0] == 255) & (splitIP[1] == 255) & (splitIP[2] == 255) & (splitIP[3] == 255) )
		return false;
	// otherwise return true, that is IP is valid.
	else
		return true
}

//-----------------------------------------------------------------------------
// Check for valid E-mail
//-----------------------------------------------------------------------------
function isEmail(str) {
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

//-----------------------------------------------------------------------------
// Check character
//-----------------------------------------------------------------------------
function InRange(char, st_char, end_char){
	if( char >= st_char && char <= end_char)
		return true;
	return false;
}
//-----------------------------------------------------------------------------
// Tooltip functions
//-----------------------------------------------------------------------------
function showtip(current,e,text)
{

	if (document.all||document.getElementById)
	{
		thetitle=text.split('<br>')
		if (thetitle.length>1)
		{
			thetitles=''
			for (i=0;i<thetitle.length;i++)
			thetitles+=thetitle[i]
			current.title=thetitles
		}
		else
			current.title=text
	}

	else if (document.layers)
	{
		document.tooltip.document.write('<layer bgColor="white" style="border:1px solid black;font-size:12px;">'+text+'</layer>')
		document.tooltip.document.close()
		document.tooltip.left=e.pageX+5
		document.tooltip.top=e.pageY+5
		document.tooltip.visibility="show"
	}
}

function hidetip()
{
	if (document.layers)
		document.tooltip.visibility="hidden"
}
//-----------------------------------------------------------------------------
// End of Tooltip functions
//-----------------------------------------------------------------------------

function isEmail(who) {
	function isEmpty(who) {
		var testArr=who.split("");
		if(testArr.length==0)
			return true;
		var toggle=0;
		for(var i=0; i<testArr.length; i++) {
			if(testArr[i]==" ") {
				toggle=1;
				break;
			}
		}
		if(toggle)
			return true;
		return false;
	}

	function isValid(who) {
		var invalidChars=new Array("~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?");
		var testArr=who.split("");
		for(var i=0; i<testArr.length; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[i]==invalidChars[j]) {
					return false;
				}
			}
		}
		return true;
	}

	function isfl(who) {
		var invalidChars=new Array("-","_",".");
		var testArr=who.split("");
		which=0;
		for(var i=0; i<2; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[which]==invalidChars[j]) {
					return false;
				}
			}
			which=testArr.length-1;
		}
		return true;
	}

	function isDomain(who) {
		var invalidChars=new Array("-","_",".");
		var testArr=who.split("");
		if(testArr.length<2||testArr.length>4) {
			return false;
		}
		for(var i=0; i<testArr.length; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[i]==invalidChars[j]) {
					return false;
				}
			}
		}
		return true;
	}


	var testArr=who.split("@");
	if(testArr.length<=1||testArr.length>2) {
		return false;
	}
	else {
		if(isValid(testArr[0])&&isfl(testArr[0])&&isValid(testArr[1])) {
			if(!isEmpty(testArr[testArr.length-1])&&!isEmpty(testArr[0])) {
				var testArr2=testArr[testArr.length-1].split(".");
				if(testArr2.length>=2) {
					var toggle=1;
					for(var i=0; i<testArr2.length; i++) {
						if(isEmpty(testArr2[i])||!isfl(testArr2[i])) {
							toggle=0;
							break;
						}
					}
					if(toggle&&isDomain(testArr2[testArr2.length-1]))
						return true;
					return false;
				}
				return false;
			}
		}
	}
}
//-----------------------------------------------------------------------------
// moves keyword from inputbox to listbox
//-----------------------------------------------------------------------------
function addKeyword(form_name, selName, newName) {
  var frm = document.forms[form_name];
  var sel = frm[selName];
  var new_key = frm[newName].value;
  var duplicate = false;
  for (i=0; i<sel.options.length;i++)
    if (sel.options[i].value == new_key) duplicate = true;
    if (new_key=="") duplicate = true;
    if (!duplicate) {
      var opt = new Option(new_key, new_key);
      sel.options[sel.options.length] = opt;
      frm[newName].value = "";
    }
}

//-----------------------------------------------------------------------------
// removes keyword from listbox
//-----------------------------------------------------------------------------
function removeKeyword(form_name, selName){
  var frm = document.forms[form_name];
  var sel = frm[selName];
  while (sel.selectedIndex>-1) {
    //if (navigator.appName.indexOf("Net") != -1)
    //	sel.options[sel.selectedIndex] = null;
    //else if (navigator.appVersion.indexOf("MSIE") != -1)
    //	sel.options.remove(sel.selectedIndex);
    sel.options[sel.selectedIndex] = null;
  }
}

//-----------------------------------------------------------------------------
// moves keyword from combobox to listbox
//-----------------------------------------------------------------------------
function addKeywordCombo(form_name, selName, newName) {
  var frm = document.forms[form_name];
  var sel = frm[selName];
  var new_key = frm[newName].options[frm[newName].selectedIndex].text
  var val = frm[newName].options[frm[newName].selectedIndex].value
  var duplicate = false;
  if (val==0) return;
  for (i=0; i<sel.options.length;i++)
    if (sel.options[i].value == val) duplicate = true;
  if (new_key=="") duplicate = true;
  if (!duplicate) {
    var opt = new Option(new_key, val);
    sel.options[sel.options.length] = opt;
  }
}
//-----------------------------------------------------------------------------
// selects all values from selectbox
//-----------------------------------------------------------------------------
function selectAll(form_name, selName) {
  var frm = document.forms[form_name];
  var sel = frm[selName];
  for (i=0; i<sel.options.length; i++) {
    sel.options[i].selected = true;
  }
}

// End