/**
 * @package swclib
 * @category javascript
 * @filesource
 * @author Chris Poirier <chris@swcconsulting.ca>
 * @copyright Copyright &copy; 2005, SWC Consulting, Ltd.
 **/

/**
 * Open a submit.php window.
 *
 * Creates a new window with the given name, and loads submit.php into the window.
 *
 * @access public
 * @param string name The name of the window to create.
 * @return boolean True on success, False on failure.
 **/
function submitWindow(name){
   if(browserType() != "Safari")
   	return window.open('submit.php',name,"toolbar=no,directories=no,status=yes,location=no,scrollbars=no,outerWidth=100,outerHeight=100,width=100,height=100,alwaysLowered=yes,hotkeys=no,z-lock=yes");
   else
   	return window.open('submit.php',name,"toolbar=no,directories=no,status=yes,location=no,scrollbars=no,width=100,height=100,alwaysLowered=yes,hotkeys=no,z-lock=yes");
}

function submitWindowArgs(name, args){
	return window.open('submit.php'+args,name,"toolbar=no,directories=no,status=yes,location=no,scrollbars=no,outerWidth=100,outerHeight=100,alwaysRaised=yes,hotkeys=no,z-lock=yes");
}

function submitWindowOther(name){
	return parent.open('submit.php',name,"toolbar=no,directories=no,status=yes,location=no,scrollbars=no,outerWidth=100,outerHeight=100,alwaysRaised=yes,hotkeys=no,z-lock=yes");
}

function newWindowMini(page, name, width, height){
   if(browserType() != "Internet Explorer")
   	return window.open(page,name,"toolbar=no,directories=no,status=yes,location=no,scrollbars=no,outerWidth="+width+",outerHeight="+height+",dependent=yes");
   else
   	return window.open(page,name,"toolbar=no,directories=no,status=yes,location=no,scrollbars=no,width="+width+",height="+height+",dependent=yes");      
}

function newWindow(page, name, width, height){
	return window.open(page,name,"outerWidth="+width+",outerHeight="+height+",dependent=yes");
}

function onlyNumeric(event) {
   var strUserAgent = navigator.userAgent.toLowerCase(); 
   var isIE = strUserAgent.indexOf("msie") > -1;
   if(isIE)
      iKeyCode = event.keyCode;
   else
      iKeyCode = event.which;
   if(
         (iKeyCode >= 32 && iKeyCode <= 45) ||
         (iKeyCode == 47) ||
         iKeyCode >= 58
     )
     return false;
   return true;
}

function onlyPhone(event) {
   var strUserAgent = navigator.userAgent.toLowerCase(); 
   var isIE = strUserAgent.indexOf("msie") > -1;
   if(isIE)
      iKeyCode = event.keyCode;
   else
      iKeyCode = event.which;
   if(
         (iKeyCode >= 32 && iKeyCode <= 39) ||
         (iKeyCode >= 42 && iKeyCode <= 44) ||
         (iKeyCode == 47 || iKeyCode == 46) ||
         iKeyCode >= 58
     )
     return false;
   return true;
}


function getFormObject(form, object){
   return eval("document."+form+".elements['"+object+"']");
}

function php2array(phparr){
   var arr;
   if(phparr.indexOf(";") != -1){
      arr=phparr.split(";");
      for(i=0; i<arr.length; i++)
         arr[i]=arr[i].split(",");
   }
   else if(phparr.length > 0)
      arr=new Array(phparr.split(","));
   else
      arr=new Array();
   return arr;
}
function phparray(phparr){
   var arr;
   if(phparr.length > 0)
      arr=phparr.split(",");
   else
      arr=new Array();
   return arr;
}


function setOptions(sel, arr){
   var numopts=Math.max(sel.options.length, arr.length);
   for(var i=numopts-1; i>=0; i--)
      sel.options[i]=null;   
   for(var i=0; i<numopts; i++){
      if(arr[i])
         sel.options[i]=new Option(arr[i][1], arr[i][0]);
   }
   return true;
}

function browserType(){
var browser,total;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
}
else browser = "An unknown browser";

return browser;
}

function checkIt(string)
{
   var thestring, detect = navigator.userAgent.toLowerCase();
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

// This function determines how many days in the selected month and
// makes sure that is how many are showing.
function setDays(formname,prefix){
   days=getDaysInMonth(getFormObject(formname, prefix+"Month").value, getFormObject(formname, prefix+"Year").value);
   dayOptions = getFormObject(formname, prefix+"Day").options;
   if(days>dayOptions.length){ // Add more days
      for(i=dayOptions.length; i<days; ++i){
         dayOptions[i] = new Option(i+1, i+1);
      }
   }
   else{ // Remove days.
      for(i=dayOptions.length-1; i>=days; --i){
         dayOptions[i]=null;
      }
   }
}

// This function returns the number of days in the month.
function getDaysInMonth(month,year)  {
   var days;
   if (month=="January" || month=="March" || month=="May" || month=="July" || month=="August" || month=="October" || month=="December")  days=31;
   else if (month=="April" || month=="June" || month=="September" || month=="November") days=30;
   else if (month=="February")  {
      if (isLeapYear(year)) { days=29; }
      else { days=28; }
   }
   return (days);
}

// This function is true if it is a leap year.
function isLeapYear (Year) {
   if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
      return (true);
   } 
   else { 
      return (false); 
   }
}

function stars(num){
   var starstr="";
   for(var i=0; i<num; i++)
      starstr+="*";
   return starstr;
}

function isIE(){
	return (d.all && !op);
}

function toggleVisible(id) {
	return setVisible(id, !isVisible(id));
}

function isVisible(id) {
	return document.getElementById(id).style.display!='none';	
}

function setVisible(id, visible){
	var display='';
	if(!visible) display='none';
	document.getElementById(id).style.display=display;
	return false;
}


