
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();
     }
}
*/
function selectall()
{
     ele = document.listform.elements

     for(i=0;i<ele.length;i++) 
          if(ele[i].type == 'checkbox')
               ele[i].checked = true
}

/*---------------------------------------------------------------------------------*/
var newwin;

function popup(popupurl,attr)
{
     if(!attr) attr = "top=50,left=100,height=450,width=625,scrollbars=yes,status=no,toolbars=no"
     if(newwin && !newwin.closed)  newwin.focus()
     else newwin = window.open(popupurl,"neww",attr) 
}

function closepopup()
{
     if(newwin && !newwin.closed)  newwin.close()
}

function open_print(appid)
{
     attr = "top=10,left=10,height=460,width=680,scrollbars=yes,status=no,toolbars=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,toolbars=no"
     popup(url,attr)
}

function open_sendmail(email)
{
     url  = '../noheader.php?manage=sendmail&to_mail=' + email;
     attr = "top=10,left=10,height=320,width=480,scrollbars=yes,status=no,toolbars=no"
     popup(url,attr)
}

function open_sendmailuser(email)
{
 
     url  = 'noheader.php?manage=sendmail&action=sendmailuser&to_mail=' + email;
     attr = "top=10,left=10,height=320,width=480,scrollbars=yes,status=no,toolbars=no"
     popup(url,attr); 
}

/*---------------------------------------------------------------------------------*/
function toggledivstyle(whichdiv)
{
     if(document.getElementById(whichdiv))
     {
          if(document.getElementById(whichdiv).style.display == "")
               document.getElementById(whichdiv).style.display = "NONE";
          else
               document.getElementById(whichdiv).style.display = "";
     }
}

var messcolor = 'brown';

function blinkdiv(whichdiv)
{
     if(document.getElementById(whichdiv))
     {
          if (document.getElementById(whichdiv).style.color == messcolor)
             {
                  document.getElementById(whichdiv).style.color = 'white'
             }
          else
             {
                  document.getElementById(whichdiv).style.color = messcolor
             }
     }
}

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 {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

/*---------------------------------------------------------------------------------*/

function getCookie(c_name)
{
     if (document.cookie.length>0)
     {
          c_start=document.cookie.indexOf(c_name + "=")
          if (c_start!=-1)
          {
               c_start=c_start + c_name.length+1
               c_end=document.cookie.indexOf(";",c_start)
               if (c_end==-1) c_end=document.cookie.length
                    return unescape(document.cookie.substring(c_start,c_end))
          }
     }
     return null
}

function setCookie(c_name,value,expiredays)
{
     var exdate=new Date()
     exdate.setDate(expiredays)
     document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : "; expires="+exdate)
}

function checkCookie()
{
     username=getCookie('username')
     if (username!=null)
     {
          alert('Welcome again '+username+'!')
     }
     else
     {
          username=prompt('Please enter your name:',"")
          setCookie('username',username,365)
     }
}

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 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(i=0;i<ele.length;i++)
          if(ele[i].type == 'checkbox')
	 	  {
			  if(ele[i].checked == true)	
			   {
				  ele[i].checked = false
			   }
			   else
	 		   {
				  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 toggledivstyle2(whichdiv)
{
     if(document.getElementById(whichdiv))
     {
          if(document.getElementById(whichdiv).style.display == "")
              { document.getElementById(whichdiv).style.display = "NONE";
              document.getElementById("searchtogdiv").innerHTML="<a href='#' onClick=\"toggledivstyle2('searchdiv');\">Advanced Search</a>";
              window.document.searchfrm.searchfilter.value="off";
              }
          else
              { document.getElementById(whichdiv).style.display = "";
              document.getElementById("searchtogdiv").innerHTML="<a href='#' onClick=\"toggledivstyle2('searchdiv');\">Hide Advanced Search</a>";
               window.document.searchfrm.searchfilter.value="on";
           window.document.searchfrm.searchkeyfil.value="";
          window.document.searchfrm.searchorderfil.value="";
           }
     }
}


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=480,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=480,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 getdivstyle(wid)
{
     return document.getElementById(wid).style.display;
}

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp(strAttributeValue) : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
          //alert(oAttribute + " having " + oAttributeValue + " = " + oAttributeValue.test(oAttribute))
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}

function get_eles(idmain,tagname)
{
     var objmain;

     if(typeof idmain == 'string') objmain = document.getElementById(idmain);
     if(typeof idmain == 'object') objmain = idmain;

     var tag_eles   = objmain.getElementsByTagName(tagname);
     var tag_objs   = new Array();

     for(var i=0;i<tag_eles.length;i++)
     {
          var currentele = tag_eles[i];
          tag_objs.push(currentele);
     }

     return tag_objs;
}

function getItsFullTag(fullcontent,wid)
{
     var reTagPattern    = eval("/<.*" + wid + ".*>/");
     return fullcontent.match(reTagPattern);
}

function check_all(wid)
{
     if(!wid) return false;

     if(typeof wid == 'string') wid = document.getElementById(wid);

     if(typeof wid != 'object') return false;

     var checks     = getElementsByAttribute(wid,"input","type","checkbox");

     for(var checkno in checks)
     {
          var curcheck   = checks[checkno];

          curcheck.checked = true;
     }
}

function uncheckall(wid)
{
     if(!wid) return false;

     if(typeof wid == 'string') wid = document.getElementById(wid);

     if(typeof wid != 'object') return false;

     var checks     = getElementsByAttribute(wid,"input","type","checkbox");

     for(var checkno in checks)
     {
          var curcheck   = checks[checkno];

          curcheck.checked = false;
     }
}

function togglecheckall(wid)
{
     if(!wid) return false;

     if(typeof wid == 'string') wid = document.getElementById(wid);

     if(typeof wid != 'object') return false;

     var checks     = getElementsByAttribute(wid,"input","type","checkbox");

     for(var checkno in checks)
     {
          var curcheck   = checks[checkno];

          curcheck.checked = !(curcheck.checked);
     }
}

function makeSearchString() {
	var args = makeSearchString.arguments;
	var searchString = "";
	var pair;
	for (var i = 0; i < args.length; i++) {
		pair = escape(args[i++]) + "=";
		pair += escape(args[i]);
		searchString += pair + "&";
	}
	return searchString.substring(0, searchString.length - 1);
}

function gatherFormData(form) {
	var formData = '';
	var element;
	
	// For each form element, extract the name and value
	for (var i = 0; i < form.elements.length; i++) {
		element = form.elements[i];
		if (element.type == "text" || element.type == "hidden" || element.type == "password" || element.type == "textarea") formData += "'" + element.name + "', '" + escape(element.value) + "', ";
		else if (element.type.indexOf("select") != -1) {
			for (var j = 0; j < element.options.length; j++) {
				if (element.options[j].selected == true) formData += "'" + element.name + "', '" + element.options[element.selectedIndex].value + "', ";
			}
		}
		else if (element.type == "checkbox" && element.checked) formData += "'" + element.name + "', '" + element.value + "', ";
		else if (element.type == "radio" && element.checked == true) formData += "'" + element.name + "', '" + element.value + "', ";
	}
	
	// Feed strings to makeSearchString() to do 'get' query string conversion
	return (eval("makeSearchString(" + formData.substring(0, formData.length - 2) + ")"));
}