
/*---------------------- Window Functions ------------------------*/
var newwin;
var new_win = null;

function popup(popupurl,attr)
{
     if(!attr) attr = "top=50,left=200,height=450,width=625,scrollbars=yes,status=no,toolbar=no,menubar=no";
     if(newwin && !newwin.closed)  newwin.focus();
     else newwin = window.open(popupurl,"neww",attr);
}

function closepopup()
{
     if(newwin && !newwin.closed)  newwin.close();
}

function open_window(url)
{
     new_win = window.open ("", "EventViewer","left=200,top=100,toolbar=no,width=380,height=340,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
     new_win.location.href = url;
     new_win.focus();
}

/*-------------------- HTML Element Functions --------------------------------*/

function get_all_elements(wtype)
{
     var eles  = new Array();

     var elecount = 0;

     var elem  = document.forms[0].elements;

     for(var i=0;i<elem.length;i++)
     {
          if(elem[i].type == wtype)
          {
               eles[elecount] = elem[i];
               elecount++;
          }
     }

     return eles;
}

function getSelectedRadio(buttonGroup)
{
     // returns the array number of the selected radio button or -1 if no button is selected

     if (buttonGroup[0]) // if the button group is an array (one button is not an array)
     {
          for (var i=0; i<buttonGroup.length; i++)
          {
               if (buttonGroup[i].checked)
               {
                    return i
               }
          }
     }
     else
     {
          if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
     }

     // if we get to this point, no radio button is selected
     return -1;
}

function getSelectedRadioValue(buttonGroup)
{
     // returns the value of the selected radio button or "" if no button is selected

     var i = getSelectedRadio(buttonGroup);

     if (i == -1)
     {
          return "";
     }
     else
     {
          // Make sure the button group is an array (not just one button)

          if (buttonGroup[i])
          {
               return buttonGroup[i].value;
          }
          else
          {
               // The button group is just the one button, and it is checked
               return buttonGroup.value;
          }
     }
}

function check_radio_by_value(buttonGroup,selvalue)
{
     if (buttonGroup[0])
     {
          for (var i=0; i<buttonGroup.length; i++)
          {
               if (buttonGroup[i].value == selvalue)
               {
                    buttonGroup[i].checked = true;
               }
          }
     }
     else
     {
          buttonGroup.checked = true;
     }
}

function selectall()
{
     ele = document.listform.elements;

     for(var i=0;i<ele.length;i++)
          if(ele[i].type == 'checkbox')
               ele[i].checked = true;
}

/*----------------- String Functions ---------------------*/

function getRandomString()
{
     // between 0 - 1
     var rndNum = Math.random();

     // rndNum from 0 - 1000
     rndNum = parseInt(rndNum * 1000);

     // rndNum from 33 - 127 
     rndNum = (rndNum % 94) + 33;

     //How convert random number to string:

     textChar = String.fromCharCode(rndNum);

     return textChar;
}

/*----------------- Cookie Functions ---------------------*/

function checkCookie()
{
     var randstr    = getRandomString();
     var cookname   = "PowerIT & INT" + randstr;

     setCookie("Developer",cookname,1);

     if(getCookie("Developer") != cookname)
     {
          alert("Cookie Disabled");
          return false;
     }
     else
     {
          //alert("Cookie Enabled");
     }

     delCookie("Developer");
     
     return true;
}

function setCookie(name,value,days)
{
     var expires    = "";
     var date       = new Date();
     
     if (days)
	{
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}

	document.cookie     = name+"="+value+expires+"; path=/";
}

function getCookie(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 delCookie(name)
{
	setCookie(name,"",-1);
}

/*----------------- HTML Div element Functions ---------------------*/

var messcolor = 'brown';

function blinkdiv(whichdiv)
{
     var obj  = document.getElementById(whichdiv);

     if(obj) obj.style.color = (obj.style.color == messcolor)?'white':messcolor;
}

function toggledivstyle(whichdiv)
{
     var obj  = document.getElementById(whichdiv);

     if(obj) obj.style.display = (obj.style.display == "")?"NONE":"";
}

function settexttoformdiv(whichdiv,texttoset,mode,whichfrm)
{
     with(whichfrm)
     {
          var obj  = document.getElementById(whichdiv);

     	if(mode == 'set') obj.innerHTML = texttoset;
     	if(mode == 'add') obj.innerHTML += texttoset;
     }
}

function settexttodiv(whichdiv,texttoset,mode)
{
     var obj  = document.getElementById(whichdiv);

	if(!mode) mode = 'set';
     
	if(mode == 'set') obj.innerHTML = texttoset;
	if(mode == 'add') obj.innerHTML += texttoset;
}

function getdivtext(whichdiv)
{
     return document.getElementById(whichdiv).innerHTML;
}

function getformdivtext(whichdiv,whichfrm)
{
	with(whichfrm)
	{
		return document.getElementById(whichdiv).innerHTML;
	}
}

function setdivstyle(whichdiv,flag)
{
     if(flag == true)
          document.getElementById(whichdiv).style.display = "";
     else
          document.getElementById(whichdiv).style.display = "NONE";
}

function showdiv(whichdiv)
{
     var obj  = document.getElementById(whichdiv);

     if(obj)   obj.style.display = "";
}

function hidediv(whichdiv)
{
     var obj  = document.getElementById(whichdiv);

     if(obj)   obj.style.display = "NONE";
}

/*----------------- User specific Functions ---------------------*/

function showTxt()
{
     strTips = 'tips'+document.gtpchoice_frm.district.value;
     strTips2 = 'tips'+document.gtpchoice_frm.district.value+'a';
     document.getElementById(strTips).style.visibility='visible';
     document.getElementById(strTips2).style.visibility='visible';
}

function removeTxt(strTips)
{
     strTips = 'tips'+document.gtpchoice_frm.district.value;
     strTips2 = 'tips'+document.gtpchoice_frm.district.value+'a';
     document.getElementById(strTips).style.visibility='hidden';
     document.getElementById(strTips2).style.visibility='hidden';
}

function hidesearch()
{
     document.searchfrm.style.display = "NONE";
}

function open_print(appid)
{
     attr = "top=10,left=10,height=460,width=680,scrollbars=yes,status=no,toolbar=no,menubar=no";
     popup("application_print.php?app_id=" + appid,attr);
}

function open_contactus()
{
     url  = 'noheader.php?manage=contactus';
     attr = "top=10,left=10,height=320,width=480,scrollbars=yes,status=no,toolbar=no,menubar=no";
     popup(url,attr);
}

function open_pref(rel)
{
     url  = rel + 'cal_prefs.php';
     attr = "top=50,left=100,height=420,width=380,scrollbars=yes,status=no,toolbar=no,menubar=no";
     popup(url,attr);
}

function open_export(rel)
{
     url  = rel + 'cal_export.php';
     attr = "top=50,left=100,height=420,width=380,scrollbars=yes,status=no,toolbar=no,menubar=no";
     popup(url,attr);
}

/*--------------- Page Nav ------------------------*/

function nav(inp)
{
     if(document.searchfrm)

     with(document.searchfrm)
     {
          if(inp == -1)
          {
               if(sCurrentPage.value == 1)
               {
                    alert("You are already in First Page");
                    return false;
               }
               sCurrentPage.value = 1;
          }
          else
          if(inp == -2)
          {
               if(sCurrentPage.value == 1)
               {
                    alert("Previous Page Not Available");
                    return false;
               }
               sCurrentPage.value = parseInt(sCurrentPage.value) - 1;
          }
          else
          if(inp == -3)
          {
               if(sCurrentPage.value == sTotalPages.value)
               {
                    alert("Next Page Not Available");
                    return false;
               }
               sCurrentPage.value = parseInt(sCurrentPage.value) + 1;
          }
          else
          if(inp == -4)
          {
               if(sCurrentPage.value == sTotalPages.value)
               {
                    alert("You are already in the Last Page");
                    return false;
               }
               sCurrentPage.value = sTotalPages.value;
          }
          else
          if(sCurrentPage.value == inp)
          {
               alert("This is your current page");
               return false;
          }
          else
          {
               sCurrentPage.value = inp;
          }
          //alert(sCurrentPage.value);
          submit();
          return false;
     }
}

function orderby(inp)
{
     if(document.searchfrm)
     with(document.searchfrm)
     {
          if(sOrderby.value.match("ASC") && sOrderby.value.match(inp))
               sOrderby.value = inp + " DESC";
          else sOrderby.value = inp + " ASC";
          submit();
     }
}

/*------------- Browser Functions ---------------------*/

function getBrowserDetails()
{
     var name       = navigator.appName;
     var version    = parseInt(navigator.appVersion);
     var agent      = navigator.userAgent;

     var details    = new Array();

     details   = name + "," + version + "," + agent

     return details;
}

function getBrowser()
{
     var details    = getBrowserDetails();

     var reIE = "Microsoft Internet Explorer";
     var reFF = "Mozilla";
     var reSa = "Safari";

     if(details.match(reIE)) return "IE";
     if(details.match(reFF)) return "FF";
     if(details.match(reSA)) return "SA";

     return "other";
}

function windowLoaded(evt)
{
     window.alert = function (varx) {

          settexttodiv("alerter_text",varx);
          showdiv("alerter");
          return false;
     }
}

/*-------------- Array Functions ---------------------*/

function in_array(arr,val)
{    
     if(typeof(arr) != 'object' || arr.length < 1) return false;

     for(i=0;i<arr.length;i++)
     {
          if(arr[i] == val) return true;
     }

     return false;
}

/*----------- Forms ----------------------------*/

function jsperform(wmanage,waction,wid)
{
     with(document.dummyfrm)
     {
          manage.value = wmanage;
          action.value = waction;
          perfid.value = wid;

          submit();
     }
}

/*----------------------------------------------------*/

/* works with IE 4+, NS4, NS6 and other Gecko, OmniWeb, Opera (4) 5+,
iCab, IceBrowser, Esape 4, HotJava 3, Konqueror and, as long as they
follow the standards, loads more */

var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1)

function findCoords(e)
{
     var posX = 0, posY = 0;

     if( !e )
     {
          e = window.event;
     }

     if( !e )
     {
          return [ 0, 0 ];
     }

     if( typeof( e.pageX ) == 'number' )
     {
          posX = e.pageX; posY = e.pageY;
     }
     else
     {
          if( typeof( e.clientX ) == 'number' )
          {
               posX = e.clientX; posY = e.clientY;

               if( document.body && !( window.opera || window.debug || navigator.vendor == 'KDE' ) )
               {
                    if( typeof( document.body.scrollTop ) == 'number' )
                    {
                         posX += document.body.scrollLeft; posY += document.body.scrollTop;
                    }
               }

               if( document.documentElement && !( window.opera || window.debug || navigator.vendor == 'KDE' ) )
               {
                    if( typeof( document.documentElement.scrollTop ) == 'number' )
                    {
                    posX += document.documentElement.scrollLeft; posY += document.documentElement.scrollTop;
                    }
               }
          }
     }
     return [ posX, posY ];
}

function isint(numb)
{
     if(numb.match(/[^0-9]/)) return false; return true;
}

function perform_action(wform,waction,wid)
{
     frm.action.value         = waction;
     frm.perform_id.value     = wid;
     frm.submit();
}

function getCurrentScript()
{
     var cururl          = window.location.href;
     var pathparts       = cururl.split("?");
     var curlocation     = pathparts[0];
     var pathvars        = curlocation.split("/");
     var curscript       = pathvars[pathvars.length-1];

     return curscript;
}

function getArrayKey(arr,val)
{    
     if(typeof(arr) != 'object' || arr.length < 1) return -1;

     for(var i=0;i<arr.length;i++)
     {
          if(arr[i] == val) return i;
     }

     return -1;
}

