var f_year, f_month, f_day,
    t_year, t_month, t_day,
    dplay   = 1,
    dlabel  = "undefined",
    slabel  = "undefined",
    dformat = 1,
    p_year, p_month, p_day,
	timInact,
    timExpi,quit,
    IsStanding = false;
var thetimer1 = 0,
    thetimer2 = 0,	
	FTimerInterval = 0, 
	tempInterval = 0,
	STimerInterval = 0;
var	TheDays  = "";
var	DEFAULTSELROWBGCOLOR ="#CCCCCC",//Default Background color value for a selected table row 
	DEFAULTSELROWFONTCOLOR="#003399"; //Default font color value for a selected table row 

function UpdDate()
{
  var myInput = document.getElementById("sdate");
  if (myInput !=null) myInput.innerHTML = document.getElementById("cal1").value;
  var myInput = document.getElementById("edate");
  if (myInput !=null)	myInput.innerHTML = document.getElementById("cal2").value;
}

function ParseDateToMilisec(StrDay,date_format)
{
  var y,m,d,t;
  var npos;

  if (parseInt(date_format) == 1)
  {
	d = StrDay.substr(0,2);
	m = StrDay.substr(3,2);
	y = StrDay.substr(6,4);
	var newd =  m+"/"+d+"/"+y;
	t= Date.parse(newd);
  }
  else t= Date.parse(StrDay);
  
  return(t!=null);	
}	

function ParseDateToInt(StrDay,date_format)
{
  var y,m,d,t;
  var npos;

  if (parseInt(date_format) == 1)
  {
	d = StrDay.substr(0,2);
	m = StrDay.substr(3,2);
	y = StrDay.substr(6,4);
	var newd =  m+"/"+d+"/"+y;
	t= Date.parse(newd);
  }
  else t= Date.parse(StrDay);
  // For Comparison reasons..
  return(t);	
}	
		
//Returns true if an oblect is not null
function CheckAnObj(CkObj)
{
  return(CkObj!=null); 
}
//Change the background and the font color a row

function ChangeRowColors(SelRowObj,SelBgColor,SelFontColor)
{
  if(!CheckAnObj(SelRowObj))return;
  
  SelRowObj.bgColor = SelBgColor;
  SelRowObj.style.color = SelFontColor;  
}
//assigns Table background and font color to all rows that the table contains
function TblRestoreColors(TblObj)
{
  if(!CheckAnObj(TblObj))return;

  var i;
  for(i=0;i<TblObj.rows.length;i++)
    ChangeRowColors(TblObj.rows[i],TblObj.bgColor,TblObj.style.color);	  
}
//HightLight a specific Row Table
function TblSelectRow(TblObj,SelRowObj,SelBgColor,SelFontColor)
{
  TblRestoreColors(TblObj);
  if((SelBgColor=="")||(SelBgColor==null))SelBgColor=DEFAULTSELROWBGCOLOR;
  if((SelFontColor=="")||(SelFontColor==null))SelFontColor=DEFAULTSELROWFONTCOLOR;
  ChangeRowColors(SelRowObj,SelBgColor,SelFontColor);
}
//Enables/Disables all the document buttons
function EnableDisableDocButtons(Disabled)
{
  var i;
  var DocItems = document.forms[0];

  for(i=0;i<DocItems.length;i++)
  {
	if ( (DocItems[i].type=="button") && (DocItems[i].id.search("nav") == -1) )
	{
		if(Disabled){
			DocItems[i].style.tagUrn=DocItems[i].style.color;
			DocItems[i].style.color="#666666";
			DocItems[i].style.cursor="text";
		}else{
			DocItems[i].style.color=DocItems[i].style.tagUrn;
			DocItems[i].style.cursor="pointer";
		}
		DocItems[i].disabled=Disabled;
	}
  }
}

//Enables/Disables a given element.
function EnableDisableSpecificButton(id,Disabled)
{
  var DocItem = document.getElementById(id);
  if(DocItem != null){
	if(Disabled){
		DocItem.style.tagUrn=DocItem.style.color;
		DocItem.style.color="#666666";
		DocItem.style.cursor="text";
	}else{
		DocItem.style.color= DocItem.style.tagUrn;
		DocItem.style.cursor="pointer";
	}
	DocItem.disabled=Disabled;
  }
}

//Deletes all spcaces at the begining of a string
function LeftTrim(Str){
  var LeftTrimStr="";
  if(!CheckAnObj(Str))return(LeftTrimStr);
  
  var i; 
  for(i=0;i<Str.length;i++)
	if(Str.charAt(i)!=" "){
		LeftTrimStr=Str.substr(i);
		break;
	};

  return(LeftTrimStr);
}

//Deletes all spcaces at the end of a string
function RightTrim(Str){
  var RightTrimStr="";
  if(!CheckAnObj(Str))return(RightTrimStr);

  var i;
  for(i=Str.length-1;i>=0;i--)
     if(Str.charAt(i)!=" "){
	   RightTrimStr=Str.substr(0,i+1);
       break;
     };

  return(RightTrimStr);
}

//Deletes all spcaces at the begining&end of a string
function Trim(Str){
  return(RightTrim(LeftTrim(Str)));
}

function addevent(obj, evtype, func)// Compatible for all platforms
{ // Add an event in every valid Object
  // e.g. Window.onbeforeprint event can now be added to Mozilla.
  if(obj.addEventListener) { obj.addEventListener(evtype,func,false); } 
  else if(obj.attachEvent) { obj.attachEvent('on'+evtype, func); }
}	

function getmouse_x_y(ev)
{
 mouse1 = ev.screenX;
 mouse2 = ev.screenY;
 mousecoordinates(mouse1,mouse2); 
}
	
function GetDaysCnt(cYear, cMonth)
{
  var mt = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var ym = cYear.valueOf();

  // In case of a leap year Feb will have 29 days.
  if((ym % 4 == 0) && ((ym % 100 != 0) || (ym % 400 == 0))) mt[1] = 29;

  return mt[cMonth-1];
}

function GetDayLabel(cYear, cMonth, cDay)
{
//  var TheDays = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

  var dt = new Date(cYear.valueOf(),cMonth.valueOf()-1,cDay.valueOf());
  var dw = dt.getDay();

  return TheDays[dw];
}

function MarkCurrentDate(cYear, cMonth, cDay, TheDaysLabels)
{ 
  var itms = document.all,
      dn   = GetDaysCnt(cYear,cMonth),
      ss;
	  
  TheGlobDays = TheDaysLabels;
  TheDays = TheDaysLabels.split(","); 
  
  itms._head1.innerText = '';
  itms._head1.innerText = GetDayLabel(cYear,cMonth,"1");
  
  itms._head2.innerText = '';
  itms._head2.innerText = GetDayLabel(cYear,cMonth,"2");
  
  itms._head3.innerText = '';
  itms._head3.innerText = GetDayLabel(cYear,cMonth,"3");
  
  itms._head4.innerText = '';
  itms._head4.innerText = GetDayLabel(cYear,cMonth,"4");
  
  itms._head5.innerText = '';
  itms._head5.innerText = GetDayLabel(cYear,cMonth,"5");
  
  itms._head6.innerText = '';
  itms._head6.innerText = GetDayLabel(cYear,cMonth,"6");
  
  itms._head7.innerText = '';
  itms._head7.innerText = GetDayLabel(cYear,cMonth,"7");
  
  for(i=0;i<itms.length;i++)
  {
    if(itms[i].type == "button")
    {
	  if(itms[i].name.search("btn") != -1) continue;
	
      if(itms[i].name == ("Year"+cYear)) itms[i].style.color = "red";
      else if(itms[i].name == ("Month"+cMonth)) itms[i].style.color = "red";
           else if(itms[i].name == ("Day"+cDay)) itms[i].style.color = "red";
                else if(itms[i].name != "") itms[i].style.color = "blue";

      if(itms[i].name.search("Day") != -1)
      {
        ss = itms[i].name.split("Day");

        if(ss[1].valueOf() > dn)
        {
          itms[i].style.visibility = "hidden";
          if(ss[1] != cDay) continue;

          for(k=i-1;k>0;k--)
            if(itms[k].type               == "button" &&
               itms[k].name.search("Day") != -1       &&
               itms[k].style.visibility   == "visible")
            {
              itms[k].style.color = "red";

              ss   = itms[k].name.split("Day");
              cDay = ss[1];

              break;
            }
        }
        else itms[i].style.visibility = "visible";
      }
    }
  } // of for i...

  if(dplay == 1)
  {
    f_year  = cYear;
    f_month = cMonth;
    f_day   = cDay;
  }
  else
  {
    t_year  = cYear;
    t_month = cMonth;
    t_day   = cDay;
  }
  
  if(dlabel != "undefined")
    if(dformat == 1) dlabel.innerText = f_day + "/" + f_month + "/" + f_year;
    else dlabel.innerText = f_month + "/" + f_day + "/" + f_year;

  if(slabel != "undefined")
  {
    if(IsStanding) return;
    if(dformat == 1) slabel.innerText = t_day + "/" + t_month + "/" + t_year;
    else slabel.innerText = t_month + "/" + t_day + "/" + t_year;
  }
}

function YearClicked(btnObj)
{
  var ss = btnObj.name.split("Year");

  if(dplay == 1) MarkCurrentDate(ss[1],f_month,f_day,TheGlobDays);
  else MarkCurrentDate(ss[1],t_month,t_day,TheGlobDays);
}

function MonthClicked(btnObj)
{
  var ss = btnObj.name.split("Month");

  if(dplay == 1) MarkCurrentDate(f_year,ss[1],f_day,TheGlobDays);
  else MarkCurrentDate(t_year,ss[1],t_day,TheGlobDays);
}

function DayClicked(btnObj)
{
  var ss = btnObj.name.split("Day");
 
  if(dplay == 1) MarkCurrentDate(f_year,f_month,ss[1],TheGlobDays);
  else MarkCurrentDate(t_year,t_month,ss[1],TheGlobDays);
}

function FillSelection(fyear, fmonth, fday)
{
var   lopt  = document.all.dlist,
      lsize = document.all.dlist.length,
      lv    = fday,
      dn    = 0,
      totD  = 0;

  dn   = GetDaysCnt(fyear,fmonth);

  if( ( fmonth!=p_month ) || ( fyear !=p_year ) ) totD = 30;
  else totD = (dn-lv);

       // ----- clear list
         for(i=0;i<lsize;i++) lopt.remove(0);

         for(i=0;i<totD+1;i++)
         {
        var oOption = document.createElement("OPTION");
            oOption.text=i+1;
            oOption.value=i+1;
            lopt.add(oOption);
         }
           lopt.selectedIndex=i-1;
}

function AssignPubDate( pYear, pMonth, pDay)
{
   p_year  = pYear;
   p_month = pMonth;
   p_day   = pDay;
}

function ShowActDate(_where)
{
  dplay = parseInt(_where);

  if(dplay == 1) MarkCurrentDate(f_year,f_month,f_day,TheGlobDays);
  else MarkCurrentDate(t_year,t_month,t_day,TheGlobDays);
}

function JustApplyDate(_where, cYear, cMonth, cDay)
{
  if(_where == "1")
  {
    f_year  = cYear;
    f_month = cMonth;
    f_day   = cDay;
  }
  else
  {
    t_year  = cYear;
    t_month = cMonth;
    t_day   = cDay;
  }
}

function Set_Bit(bptr, bi, onoff)
{
  var barr = new Array(1,2,4,8,16,32,64,128,256,512,
                       1024,2048,4096,8192,16384,
                       32768,65536); // Bits array

  if(onoff) return (bptr | barr[bi-1]);
  else return (bptr & (~barr[bi-1]));
}

function Ck_Bit(bptr, bi)
{
  var barr = new Array(1,2,4,8,16,32,64,128,256,512,
                       1024,2048,4096,8192,16384,
                       32768,65536); // Bits array

  return (((bptr & barr[bi-1]) != 0) ? true : false);
}

function CallWin(urlPage,_scroll,resi,top,left,wide,deep,dArgs)
{
  var wnd,
      tmp = "toolbar=no,location=no,scrollbars=";

  if(_scroll) tmp += "yes,resizable=";
  else tmp += "no,resizable=";

  if(resi) tmp += "yes,width=";
  else tmp += "no,width=";

  tmp += (wide + ",height=" + 
          deep + ",top="    +
          top  + ",left="   +
          left);

  if(parseInt(wide) == 1024 &&
     parseInt(deep) == 768)

       tmp += ",fullscreen=no,status=yes";
  else tmp += ",menubar=no,status=no";

  wnd = window.open(urlPage,"_blank",tmp);

  if (wnd != null)
  {
     wnd.focus();
	 if(wnd.opener == null) wnd.opener = self;
	 wnd.dialogArguments = dArgs;
	 return wnd;
  }
}

function setTimerCookie(T,P){
	var timerExp = new Date();
  	timerExp.setTime(timerExp.getTime()+(T*1000));
  	Delete_Cookie("msField",P);
  	Set_Cookie("msField",timerExp.getTime(),"",P);  
}

function SetTimerOpt(tim)
{ 
  window.status="Page Completed";
  if ( parseInt(tim) == 0 ) return;
  if ( parseInt(tim) == -1 ){ // if -1 hold old value for reset function to set a new timer.
	  tempInterval = FTimerInterval;
	  window.clearTimeout(thetimer1); 
	  return;
  }
  FTimerInterval = tim;
  window.clearTimeout(thetimer1);
  thetimer1 = window.setTimeout("CountDown()",(parseInt(FTimerInterval*1000)));
}

function SetTimerOps(tim)
{ 
  window.status="Page Completed";
  if  ( ( parseInt(tim) == 0 ) || ( parseInt(tim) == -1 ) ) return;
  var timerExp = new Date();
  var timeNow = timerExp.getTime();
  thetimer2 = window.setTimeout("SetTimerOps("+tim+")",2000);
  
  if( (timeNow >= parseInt(Get_Cookie("msField",0)))){
	  window.clearTimeout(thetimer2);
	  CountDown();   
  }
}

function CountDown()
{ 
   window.clearTimeout(thetimer2);
   window.clearTimeout(thetimer1);
   var mc = parseInt(Get_Cookie("MACOS",1)); // Is from CrewRoom?
   if(mc == 0) alert('We take privacy and information protection seriously.\nWe limit the time that secure connections are open on our website.\nPlease sign-on again.\nNOTE: Your browser may also limit secure\nconnection time and automatically log you out independent of our timeout procedure.');
   logmeout();
}

function resetcount()
{ 
  if ( parseInt(FTimerInterval) == 0 ) return;
  if ( parseInt(FTimerInterval) == -1 ){
	  window.clearTimeout(thetimer1);
	  thetimer1 = window.setInterval("CountDown()",parseInt((tempInterval*1000)));	
	  return;
  }
  window.clearTimeout(thetimer1);
  thetimer1 = window.setInterval("CountDown()",parseInt((FTimerInterval*1000)));	
}

function fullscreen()
{
  top.window.moveTo(0,0);
  top.window.resizeTo(screen.availWidth,screen.availHeight);
}

function DisableBtns()
{ 
  var btc = document.all.item("btns");
  for(i=0;i<btc.length;i++) btc[i].disabled = true;
}

function Get_Cookie(name, pos)
{
  var start = document.cookie.indexOf(name+"="),
      len   = start + name.length + 1;

  if((!start) && (name != document.cookie.substring(0,name.length))) return null;
  if(start == -1) return null;

  var end = document.cookie.indexOf(";",len);
  if(end == -1) end = document.cookie.length;
 
  var clst = document.cookie.substring(len,end);
  clst = clst.split(escape(",")); 

  if (pos > clst.length) 
  {
    return null;
  }
  else
  {
   var clst1=unescape(clst[pos]);
   
   if(clst1.charCodeAt(0) == 34) return unescape(clst1.substring(1,clst1.length-1));
   else return clst1;
  }	
}

function Set_Cookie(name, value, expires, path, domain)
{
		
  if(name == "") name = "wtvar";

  if(expires == "")
  {
    expires = new Date();
    expires.setDate(expires.getDate()+2);
  }
  
  document.cookie = name + "=" + escape(value) +
    ((expires) ? ";expires=" + expires.toGMTString() : "") +
    ((path)    ? ";path="    + path                  : "") +
    ((domain)  ? ";domain="  + domain                : "");
}

function Set_CookieA(name, pos , value, expires, path, domain)
{
  var start = document.cookie.indexOf(name+"="),
  len   = start + name.length + 1;

  if((!start) && (name != document.cookie.substring(0,name.length))) return null;
  if(start == -1) return null;

  var end = document.cookie.indexOf(";",len);
  if(end == -1) end = document.cookie.length;

  var clst = document.cookie.substring(len,end);
  clst = clst.split(escape(","));

  if (pos > clst.length) return null
  else clst[pos]=unescape(value);

  if(name == "") name = "wtvar";

  if(expires == "")
  {
    expires = new Date();
    expires.setDate(expires.getDate()+2);
  }

  document.cookie = name + "=" + escape(clst) +
    ((expires) ? ";expires=" + expires.toGMTString() : "") +
    ((path)    ? ";path="    + path                  : "") +
    ((domain)  ? ";domain="  + domain                : "");
}

function Delete_Cookie(name, path, domain)
{
   if(Get_Cookie(name,0) != null)
   {
      document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}

function WrapText(txt, bWidth, fSize)
{
  fSize = parseInt(fSize);
  if(isNaN(fSize)) fSize = 10;
	  
  bWidth = parseInt(bWidth);
  if(isNaN(bWidth)) bWidth = 120;
	  
  var mc  = Math.round((bWidth * fSize) / 72),
      tmp = "",
      fx,ls=0,rs=0;
		  
  while(txt.length > 0)
  {
    fx = mc - 1;

    if(fx >= txt.length)
    {
       tmp += txt;
       break;
    }

    while(txt.charCodeAt(fx) != 32)
    {
      if(fx > 0) { fx--; ls++; }
      else break;
    }

    fx = mc - 1;

    while(txt.charCodeAt(fx) != 32)
    {
      if(fx < txt.length) { fx++; rs++; }
      else break;
    }

    if(ls <= rs) fx = (mc - 1) - ls;
    else fx = (mc - 1) + rs;

    tmp += txt.substring(0,fx) + '\n';
    txt  = txt.substring(fx+1,txt.length);
  }

  return tmp;
}

function DetectPlatform()
{
  if(navigator.appVersion.indexOf("Win") != -1)
  {
    return(1);
  }
  else if(navigator.appVersion.indexOf("Mac") != -1)
  {
    return(2);
  }
  else  return(2);
}

function Create_new(item)
{	 
  obj = document.getElementById(item);
  visible = (obj.style.visibility!="hidden")
  var key = document.getElementById("x" + item);
 
  if (visible) 
  {
    obj.style.visibility = "hidden";
    key.innerHTML = "<img src='wtouch/textfolder.gif' width='32' height='27' hspace='0' vspace='0' border='0'>";
  }
  else
  {
    obj.style.visibility = "visible";
    key.innerHTML = "<img src='wtouch/emptytext.gif' width='32' height='27' hspace='0' vspace='0' border='0'>";
  }
}

function HotSpot(button_name)
{
  var itms = document.forms[0];
  
  for(i=0;i<itms.length;i++) 
  {
	if ( (itms[i].type != "button") || (itms[i].name.search("btn") != -1) ) continue;
    if(itms[i].name == (button_name) && !itms[i].disabled ) 
		itms[i].style.color = "red";
    else if(!itms[i].disabled){ //MAX if mouse out and btn not disabled then change color.
		itms[i].style.color = "blue";
	}
  }
}

function rollover(objs)
{
   if (objs.value == 'Logout') return;
   else objs.style.color  = "#FF1450";
}

function rollout(objs)
{
   objs.style.color = "white";
}

function ChangeNavStyle(tblid)
{	
	//FIX FOR OPERA...
	if(!tblid.disabled){
  		tblid.border = "1";
  		tblid.style.borderColor = "#000000";
  		tblid.style.borderTopColor="transparent";
  		tblid.style.borderLeftColor="transparent";
	}
}

function DefaultNavStyle(tblid)
{
  tblid.border = "0";
  tblid.style.borderColor = "transparent";
}

function disablenavbtns(btnobj)
{
  var itms = document.all.tags("BUTTON");
  for(i=0;i<itms.length;i++) 
   itms[i].disabled = true;
}

function CallHelpWin(helpurl,helpanchor)
{
  if(screen.height == 600 && screen.width == 800)
   CallWin(helpurl+helpanchor,true,true,0,0,505,520);
  else
   CallWin(helpurl+helpanchor,true,true,0,0,760,685);	
}

function ShowPad(obj)
{
 switch(parseInt(obj))
 {
   case 1:
		document.forms[0].Doff.value = "";
		document.all.offdays.style.visibility = "visible";
		document.all.priordays.style.visibility = "hidden";
	 break;

   case 2:
		document.forms[0].Prior.value = "";
		if (document.all.offdays != null) document.all.offdays.style.visibility = "hidden";
		document.all.priordays.style.visibility = "visible";
	 break;		 
 }
}   

function HideCal()
{
  if ( (document.all.offdays != null) && (document.all.offdays.style.visibility == "visible") ) document.all.offdays.style.visibility = "hidden";
  if ( (document.all.priordays != null) && (document.all.priordays.style.visibility == "visible") ) document.all.priordays.style.visibility = "hidden";
  if (document.forms[0].Doff !=null) label1.innerHTML = document.forms[0].Doff.value;
} 

function DisappearCalendar2(_oper)
{
  var calend2 = document.getElementById('cal2');
  var calimg2 = document.getElementById('cal_gif2');	   	   
  if ( (calend2 == null) || (calimg2 == null) ) return;
  if (_oper)
  {
  calend2.style.visibility = "hidden";
  calimg2.style.visibility = "hidden";
  }
  else
  {
  calend2.style.visibility = "visible";
  calimg2.style.visibility = "visible";
  }
  return _oper;
}

function Detect_Browser() 
{ 
  var brows;
  var newURL='', verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
  
  if (app.indexOf('Netscape') != -1) 
  {brows = 1;}
  else
  if (app.indexOf('Microsoft') != -1) 
  {brows = 2 ;}	
  
  return(brows);		
}


var scr_loaded = true;

/*
	Is used as a global function to detect if browser should open
	dialog windows.
	Returns: true if modal windows are allowed. False if not. 
*/
function allowDialog(){
	var allow = true;
														  /* We DON'T want to allow */
	if( ( navigator.appName.indexOf('Opera') != -1 )    || 		/* Opera */
        ( navigator.appName.indexOf('iPhone') != -1 )   ||		/* Iphone */
		( navigator.appVersion.indexOf('iPad') != -1 )  ||		/* Ipad */
	    ( navigator.appName.indexOf('Android') != -1 )  || 		/* Android in general */
	    ( navigator.appVersion.indexOf('GT-P1000')!=-1 )||	    /* Galaxy tab */
		( navigator.appVersion.indexOf('BlackBerry')!=-1 )||		/* RIM Device */
    	( navigator.appVersion.indexOf('Android')!=-1 ) ) 	    /* Android emulator */
			allow = false;
	
	return allow;
}

/*
	Find if the browser belongs to a mobile device.
	If yes return true.
	Else return false.
*/
function isMobile(){
  var isMobile = false;
  														  /* Return true only if detect one of: */
  if( ( navigator.platform.indexOf("iPhone") != -1 )  ||		/* Iphone */
      ( navigator.appVersion.indexOf('iPad') != -1 )  ||		/* Ipad */
  	  ( navigator.appName.indexOf('Android') != -1 )  ||		/* Android in general */
	  ( navigator.appVersion.indexOf('GT-P1000')!=-1 )||		/* Galaxy tab */
	  ( navigator.appVersion.indexOf('BlackBerry')!=-1 )||		/* RIM Device */
	  ( navigator.appVersion.indexOf('Android')!=-1 ) )  		/* Android emulator */
	 		isMobile = true;
			
   return isMobile;
}
