﻿/// <reference path="Main.js" />
// Common Javascript Functions

//Trim spaces from text
function Trim(str)
{
	str = str.replace(/^\s*/, "") //trimming from the left
	str = str.replace(/\s*$/, "") //trimming from the right
	return str;
}

//format date
function format_date(date)
{
    var month, day, year, output
    
    if (date.getMonth() < 10) { month = '0' + (date.getMonth() + 1); } else { month = date.getMonth() + 1; }
    if (date.getDate() < 10){ day = '0' + date.getDate(); } else { day = date.getDate(); }
    year = date.getFullYear();
    
    return (month + "/" + day + "/" + year);
}

//Set the visibility of an item
function vis(id, val)
{
    var obj = document.getElementById(id);
	obj.style.visibility = val;
}

// Set the Default Button for the textbox you are on.
function clickButton(e, buttonid)
{ 
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt)
      { 
          if (evt.keyCode == 13)
          { 
                bt.click(); 
                return false; 
          } 
      } 
}


//Open window
function openWindow(page, height, width)
{
    if (window.showModalDialog) 
    {
	    window.showModalDialog(page, window, "dialogheight:" + height + "px; dialogwidth:" + 
	                           width + "px; resizable:no; help:no; status:no; scroll:no; edge: raised;");
	}
	else
	{
	    window.open(page, window, "height=" + height + ", width=" + width + 
	                ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,help=no,modal=yes");
	}
}

//Selects an item in a dropdown list box to the value passed as an argument.
function setListItem(oDropDown, val)
{
    val = Trim(val);
    oDropDown.options[0].selected = true;   //default to first item.
    
    for( var k=0; k < oDropDown.options.length; k++)
    {
        if (Trim(oDropDown.options[k].value).toUpperCase() == val.toUpperCase())
        {
            oDropDown.options[k].selected = true;
            break;
        }
    }
}

// Set the value of a textbox field
function setTextBoxValue(elemId, sValue)
{
    var oElem = document.getElementById(elemId);
    oElem.value = nullCheck(sValue);
    this.resetBorderColor(oElem);
}

// Sheck if field is null
function nullCheck(sValue)
{
    if (sValue == null) sValue = "";
    return sValue;
}

// Set the value of a label
function setLabelValue(elemId, sValue)
{
    document.getElementById(elemId).innerHTML = nullCheck(sValue);
}

//--------------------------------------------------------------------------
//  WARP Event handlers
//-------------------------------------------------------------------------- 
function warpMenu_RefreshRequest(oPanel,oEvent,id)
{
        sm('box',275,85);
}

function warpMenu_RefreshComplete(oPanel,oEvent,id)
{
    hm('box');
}

//--------------------------------------------------------------------------
// Count length of textbox
//--------------------------------------------------------------------------
function textCounter(field, countfield, maxlimit) 
{
    if (field.value.length > maxlimit)
    {
        field.value = field.value.substring(0, maxlimit);
    }
    else
    {
        countfield.value = maxlimit - field.value.length;
    }    
}

//--------------------------------------------------------------------------
// Modal Please Wait
//--------------------------------------------------------------------------
function pageWidth() 
{
    return window.innerWidth != null? 
        window.innerWidth: document.documentElement && document.documentElement.clientWidth 
        ? document.documentElement.clientWidth:document.body != null
        ? document.body.clientWidth:null;
}

function pageHeight() 
{
    return window.innerHeight != null
        ? window.innerHeight: document.documentElement && document.documentElement.clientHeight 
        ? document.documentElement.clientHeight:document.body != null
        ? document.body.clientHeight:null;
}

function posLeft() 
{
    return typeof window.pageXOffset != 'undefined' 
        ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft
        ? document.documentElement.scrollLeft:document.body.scrollLeft
        ? document.body.scrollLeft:0;
}

function posTop() 
{
    return typeof window.pageYOffset != 'undefined' 
        ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop
        ? document.documentElement.scrollTop: document.body.scrollTop
        ?document.body.scrollTop:0;
}

function $(x)
{
    return document.getElementById(x);
}

function scrollFix()
{
    var obol=$('ol');
    obol.style.top=posTop()+'px';
    obol.style.left=posLeft()+'px';
}

function sizeFix()
{
    var obol=$('ol');
    obol.style.height=pageHeight()+'px';
    obol.style.width=pageWidth()+'px';
}

function kp(e)
{
    ky=e?e.which:event.keyCode;
    if(ky==88||ky==120)hm();
    return false
}

function inf(h)
{
    tag=document.getElementsByTagName('select');
    for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;
    tag=document.getElementsByTagName('iframe');
    for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;
    tag=document.getElementsByTagName('object');
    for(i=tag.length-1;i>=0;i--)tag[i].style.visibility=h;
}

function sm(obl, wd, ht)
{
    var h='hidden';
    var b='block';
    var p='px';
    var obol=$('ol'); 
    var obbxd = $('mbd');
    obbxd.innerHTML = $(obl).innerHTML;
    obol.style.height=pageHeight()+p;
    obol.style.width=pageWidth()+p;
    obol.style.top=posTop()+p;
    obol.style.left=posLeft()+p;
    obol.style.display=b;
    var tp=posTop()+((pageHeight()-ht)/2)-12;
    var lt=posLeft()+((pageWidth()-wd)/2)-12;
    var obbx=$('mbox');
    obbx.style.top=(tp<0?0:tp)+p;
    obbx.style.left=(lt<0?0:lt)+p;
    obbx.style.width=wd+p;
    obbx.style.height=ht+p;
    inf(h);
    obbx.style.display=b;
    return false;
}

function hm()
{
    var v='visible';
    var n='none';
    $('ol').style.display=n;
    $('mbox').style.display=n;
    inf(v);
    document.onkeypress=''
}

function initmb()
{
    var ab='absolute';
    var n='none';
    var obody=document.getElementsByTagName('body')[0];
    var frag=document.createDocumentFragment();
    var obol=document.createElement('div');
    obol.setAttribute('id','ol');
    obol.style.display=n;
    obol.style.position=ab;
    obol.style.top=0;
    obol.style.left=0;
    obol.style.zIndex=998;
    obol.style.width='100%';
    frag.appendChild(obol);
    var obbx=document.createElement('div');
    obbx.setAttribute('id','mbox');
    obbx.style.display=n;
    obbx.style.position=ab;
    obbx.style.zIndex=999;
    var obl=document.createElement('span');
    obbx.appendChild(obl);
    var obbxd=document.createElement('div');
    obbxd.setAttribute('id','mbd');
    obl.appendChild(obbxd);
    frag.insertBefore(obbx,obol.nextSibling);
    obody.insertBefore(frag,obody.firstChild);
    window.onscroll = scrollFix; 
    window.onresize = sizeFix;
}

window.onload = initmb;