var winWidth;
var winHeight;
var IsIe = false;
var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
var dsocleft=document.all? iebody.scrollLeft : window.pageXOffset;
var dsoctop=document.all? iebody.scrollTop : window.pageYOffset;
var isIndexPage;
    
function getDocumentScroll()
{
    dsoctop=document.all? iebody.scrollTop : window.pageYOffset;
    dsocleft=document.all? iebody.scrollLeft : window.pageXOffset;
}

function setDocumentScroll()
{
    // force browser to redraw scrollbars
    top.resizeBy(1,1);
    top.resizeBy(-1,-1);
    // re-apply document scroll
    top.scrollTo(dsocleft, dsoctop);
}

function GetWindowSize() {
 	var myWidth = 0, myHeight = 0;
 	
 	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
 	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		IsIe = true;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		IsIe = true;
	}
	//window.alert( 'Width = ' + myWidth );
	//window.alert( 'Height = ' + myHeight );
	winWidth = myWidth;
	winHeight = myHeight;
}

function IsNumeric(obj)
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var sText = obj.value;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            obj.value = sText.substring(0, sText.length - 1);
            IsNumber = false;
        }
    }
    return IsNumber;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
 }
 
 function FormatCurrency(obj) {
    return formatCurrency(obj.value);
 }
 
function SetDefaultLeftContent(IsIndex) { 
    if (IsIndex == undefined)
        IsIndex = false;
    oHeader = document.getElementById("PageHeader");
	oContent = document.getElementById("PageContent");
	oContentL = document.getElementById("LeftContent");
	oContentR = document.getElementById("RightContent");
	oContentRH = document.getElementById("RightContentHeader");
	oContentFooter = document.getElementById("PageFooter");
	
	if (oContentL != null & IsIndex == true)
	{
	    WidthAdjusted = oContent.offsetWidth - 290;
	    oContentL.style.width = parseInt(WidthAdjusted) + "px";   
	    
	    
	}  else
	{
	    if (oContentL.offsetWidth > 5)
	        oContentR.style.width = (oContent.offsetWidth - 188) + "px";
	}
	
	oContent.style.top = "1px";
    
    if (document.getElementById("pnlMembers"))
	    oContent.style.top = "-24px";

	if ((oContent.offsetHeight > oContentL.offsetHeight) && (oContent.offsetHeight > oContentR.offsetHeight))
	{
	    oContentFooter.style.top = ((oContent.offsetHeight + oHeader.offsetHeight) + 100) + "px";
	} else {
	    if (oContentL.offsetHeight >> oContentR.offsetHeight)
	    {
            oContentFooter.style.top = (oContentL.offsetHeight) +  "px";
        }
        else if (oContentR.offsetHeight >> oContentL.offsetHeight) {
            oContentFooter.style.top = (oContentR.offsetHeight) +  "px";}
        else {
            oContentFooter.style.top = "173px";
        }
    }
    
    if (oContentRH != null)
    {
        oContentRH.style.width = oContentR.style.width;
    }
    oContentFooter.style.width = oHeader.offsetWidth + "px";
    oContentSubNav = document.getElementById("PageSubMenu1_mnuSubNav");
    if (oContentSubNav == null && IsIndex == false) {
        document.getElementById("PageContentCookies").style.width = (oContentRH.offsetWidth - 40) + "px";
    } else if (oContentSubNav != null) {
        document.getElementById("PageContentCookies").style.width = "570px";
    }
    
}

function Init(IsIndex) {
	//PreloadImages();
	isIndexPage = IsIndex;
	GetWindowSize();
	//SetFooterPos();
	SetDefaultLeftContent(IsIndex);
	
	//Set handlers for window/mouse events
	window.onblur = register;
	window.oncontextmenu = register;
	window.onerror = register;
	window.onfocus = register;
	window.onscroll = register;
	window.onresize = register;
	window.onclick = register;
	window.onmousedown = register;
	window.onmouseup = register;
}

function Resized() {
	GetWindowSize();
	SetFooterPos();
	SetDefaultLeftContent(isIndexPage);
}

function register(e) {
	if(!e) e = window.event;
	if(e.type=='resize') {
		Resized();
	}
}

function SearchFocus(obj, searchDefault) {
    if (obj.value == searchDefault)
    {
        obj.value='';
        obj.style.fontStyle='normal';
        obj.style.color='#000';
    }
    else
    {
        obj.select();
    }
}

function SearchBlur(obj, searchDefault) {
    if (obj.value == '')
    {
        obj.value=searchDefault;
        obj.style.fontStyle='italic';
        obj.style.color = 'gray';
    }
}

function onLoginItemSelect() {
    
    document.forms['frmLogin'].txtMemberUser.focus();
    return true;
}

function SetCookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );
  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    //alert( exp_y + "," + exp_m + "," + exp_d + " = " + expires.toGMTString());
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
  //alert(value);

}

/* Dreamweaver Functions */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function Show(strID) {
	document.getElementById(strID).style.display='block';
}

function Hide(strID) {
	document.getElementById(strID).style.display='none';
}

function setTabAble(id, index, setfocus)
{
    var obj = document.getElementById(id);
    obj.tabIndex = index;
    obj.tabEnabled = true;
    if (setfocus)
    {
      obj.focus();
    }
}

function IsRequiredNumeric(obj, maxlength)
{
    var sText = obj.value;

    if (sText.length > maxlength)
        return false;
    
    var ev = window.event ? window.event : e;
    var k = ev.which ? ev.which : ev.keyCode;
    
    if (k != 8 || k != 46)
    {
        return IsNumeric(obj);
    }
    
    return true;
}

function IsNumeric(obj)
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var sText = obj.value;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
          Char = sText.charAt(i); 
          if (ValidChars.indexOf(Char) == -1) 
          {
             obj.value = sText.substring(0, sText.length - 1);
             IsNumber = false;
          }
    }
      
    return IsNumber;
}

function ToggleObjectEnabled(obj, isenabled, focusfirst)
{
    obj.disabled = (isenabled == "true" || isenabled == true ? false : true);
    
    ToggleObjectChildren(obj, isenabled, focusfirst);
}

function ToggleObjectChildren(obj, isenabled, focusfirst)
{
    var o = obj.getElementsByTagName("input");
    objFocus = null;
    
    for (var i = 0; i < o.length; i++)
    {       
        o[i].disabled = (isenabled == "true" || isenabled == true ? false : true);
        if (focusfirst == true && i == 0) 
            objFocus = o[i];
    }
    
    var s = obj.getElementsByTagName("span");
    
    for (var i = 0; i < s.length; i++)
    {
        s[i].disabled = (isenabled == "true" || isenabled == true ? false : true);
    }
    
    var t = obj.getElementsByTagName("textarea");
    
    for (var i = 0; i < t.length; i++)
    {
        t[i].disabled = (isenabled == "true" || isenabled == true ? false : true);
    }

    if (objFocus != null)
        objFocus.focus();
}

//**************************************************************************************************
function ToggleMemberLogin(param)
{
    if(MemberLoginDialog.get_isShowing())
    {
	    MemberLoginDialog.Close();
	    
	    setDocumentScroll();
    }
    else
    {
        getDocumentScroll();
        
        MemberLoginDialog.set_x(0); 
        MemberLoginDialog.set_y(0); 

        if (param == undefined)
        {
            MemberLoginDialog.Show();
        } else {
            MemberLoginDialog.Show();
        }
        window.frames['frmLogin'].document.getElementById('txtMemberUser').focus();
    }
}

function ToggleStaffDetails(param)
{
    if(StaffDetailsDialog.get_isShowing())
    {
	    StaffDetailsDialog.Close();
	    
	    setDocumentScroll();
    }
    else
    {
        getDocumentScroll();
        
        StaffDetailsDialog.set_x(0); 
        StaffDetailsDialog.set_y(0); 

        if (param == undefined)
        {
            StaffDetailsDialog.Show();
        } else {
            StaffDetailsDialog.Show();
            CallbackStaffMember.Callback(param);
        }
    }
}

function ToggleCCDetails()
{
    if(CCDetailsDialog.get_isShowing())
    {
	    CCDetailsDialog.Close();
	    
	    setDocumentScroll();
    }
    else
    {
        getDocumentScroll();
        
        CCDetailsDialog.set_x(0); 
        CCDetailsDialog.set_y(0); 

        CCDetailsDialog.Show();       
    }
}

function ToggleStaffMembers(param)
{
    if(StaffMembersDialog.get_isShowing())
    {
        Hide('ctl05$cboStaffType');
        Hide('ctl05$cboStaffState');
        
        //Show('BannerHeader');
	    StaffMembersDialog.Close();
	    
	    setDocumentScroll();
    }
    else
    {
        getDocumentScroll();
        
        StaffMembersDialog.set_x(0); 
        StaffMembersDialog.set_y(0); 

        Show('ctl05$cboStaffType');
        Show('ctl05$cboStaffState');

        setTabAble("ctl05_cboStaffType", 1, true);
        setTabAble("ctl05_txtStaffName", 2, false);
        setTabAble("ctl05_txtStaffAddress", 3, false);
        //Hide('BannerHeader');

        if (param == undefined)
        {
            StaffMembersDialog.Show();
            CallbackStaffMembers.Callback(0);            
        } else {
            StaffMembersDialog.Show();
            CallbackStaffMembers.Callback(param);
        }
    }
}

function OnStaffMemberSubmit(gdStaff, rowValues) {
    
    gdStaff.Table.AddEmptyRow();
    
    gdStaff.Edit(gdStaff.Levels[0].Table.GetRow(gdStaff.Levels[0].Table.GetRowCount()-1));
    
    var aryValues = rowValues.split(',');
    var curDate = new Date();
    
    var theRow = gdStaff.Levels[0].Table.GetRow(gdStaff.Levels[0].Table.GetRowCount()-1); 
    theRow.SetValue(0, aryValues[0], true);
    theRow.SetValue(1, aryValues[1], true);
    theRow.SetValue(2, aryValues[2], true);
    theRow.SetValue(3, aryValues[3], true);
    theRow.SetValue(4, aryValues[4], true);
    theRow.SetValue(5, aryValues[5], true);
    theRow.SetValue(6, aryValues[6], true);
    theRow.SetValue(7, aryValues[7], true);
    theRow.SetValue(8, aryValues[8], true);
    theRow.SetValue(9, 0, true);
                
    gdStaff.Edit(theRow); 
    gdStaff.EditingDirty = true;
    gdStaff.editComplete();
    
    ToggleStaffMembers();
}

function ToggleEmployeeMembers(param)
{
    if(EmployeeMembersDialog.get_isShowing())
    {
        Hide('ctl03_cboGender');
        Hide('ctl03_cboCoverageType');
        Hide('ctl03_cboDEPLife');
        Hide('ctl03_cboEarningType');
	    EmployeeMembersDialog.Close();
	    
	    setDocumentScroll();
    }
    else
    {
        getDocumentScroll();
        
        EmployeeMembersDialog.set_x(0); 
        EmployeeMembersDialog.set_y(0); 

        Show('ctl03_cboGender');
        Show('ctl03_cboCoverageType');
        Show('ctl03_cboDEPLife');
        Show('ctl03_cboEarningType');

        if (param == undefined)
        {
            EmployeeMembersDialog.Show();
        } else {
            EmployeeMembersDialog.Show();
            CallbackEmployeeMembers.Callback(param);
        }
    }
}

function OnEmployeeSubmit(gdEmployee, rowValues) {
    
    gdEmployee.Table.AddEmptyRow();
    
    gdEmployee.Edit(gdEmployee.Levels[0].Table.GetRow(gdEmployee.Levels[0].Table.GetRowCount()-1));
    
    var aryValues = rowValues.split(',');
    var curDate = new Date();
    
    var theRow = gdEmployee.Levels[0].Table.GetRow(gdEmployee.Levels[0].Table.GetRowCount()-1); 
    theRow.SetValue(0, aryValues[0], true);
    theRow.SetValue(1, aryValues[1], true);
    theRow.SetValue(2, aryValues[2], true);
    theRow.SetValue(3, aryValues[3], true);
    theRow.SetValue(4, aryValues[4], true);
    theRow.SetValue(5, aryValues[5], true);
    theRow.SetValue(6, aryValues[6], true);
    theRow.SetValue(7, aryValues[7], true);
    theRow.SetValue(8, aryValues[8], true);
    theRow.SetValue(9, 0, true);
                
    gdEmployee.Edit(theRow); 
    gdEmployee.EditingDirty = true;
    gdEmployee.editComplete();
    
    ToggleEmployeeMembers();
}

function ToggleSeminarDetails(action, param)
{
    if(SeminarDetailsDialog.get_isShowing())
    {
	    SeminarDetailsDialog.Close();
	    
	    setDocumentScroll();
    }
    else
    {
        getDocumentScroll();
        
        SeminarDetailsDialog.set_x(0); 
        SeminarDetailsDialog.set_y(0); 

        if (param == undefined)
        {
            SeminarDetailsDialog.Show();
        } else {
            SeminarDetailsDialog.Show();
            CallbackSeminarDetails.Callback(action,param);
        }
    }
}

// ****************************************[ Page Callbacks ]************************************************

function PerformMemberLogin() {
    CallbackLogin.Callback();
}
