var images = new Array();
var lastHelp = '';
var form_enterPressed = false;
/*
preload the image first, by using the function PRELOADIMG

parameters are:  the imgname, which is the NAME attribute of the image
				the relevant location, of the Up and Over image
				
After that, on the mouseover and mouseout event, the function CHANGEIMG must be called

	img must be the IMAGE object, (passing it thourgh GetElementById) of the Image to be changed
	type is either over or up, according to situation
	
	the ASP function rollover can automatically set the image for rollover, once the image is preloaded



*/

function MyDate(dt)
{
    var day,month,year;
    var tmp = dt;
    if (dt == null || dt == "")
    {
        var tmpDate = new Date();
        tmp = tmpDate.getDate() + "/" + tmpDate.getMonth() + "/" + tmpDate.getFullYear();
    }
    var pos1,pos2;
    tmp = tmp.replace("-","/");
    pos1 = tmp.indexOf("/");
    pos2 = tmp.indexOf("/",pos1+1);
    day = Number(tmp.substr(0,pos1));
    month = Number(tmp.substr(pos1+1,(pos2-pos1-1)));
    year = Number(tmp.substr(pos2+1));
    if ((year + "").length < 4) 
    {
        if (year < 70)
            year += 2000;
        else
            year += 1900;
        
    }
    
    var months = new Array();
    months[1] = 31;
    months[2] = 28;
    months[3] = 31;
    months[4] = 30;
    months[5] = 31;
    months[6] = 30;
    months[7] = 31;
    months[8] = 31;
    months[9] = 30;
    months[10] = 31;
    months[11] = 30;
    months[12] = 31;
    
    this.Date = tmp;
    
    this.isInvalid = false;
    var tmpMaxMonth = 0;
    if (month < 1 || month > 13)
        this.isInvalid = true;
    else
    {
        tmpMaxMonth = months[month];
        if ((year % 4) == 0 && month == 2)
            tmpMaxMonth++;
        if (day < 1 || day > tmpMaxMonth)
            this.isInvalid = true;
    }
    
    
    this.addDays = function addDays(num)
    {
        if (!this.isInvalid)
        {
            var left = num;
            var isLeapYear = ((year % 4) == 0)
            var maxMonth;
            while (left > 0)
            {
                maxMonth = months[month];
                if (month == 2 && isLeapYear)
                    maxMonth++;
                day++;
                if (day > maxMonth)
                {
                    day = 1;
                    month++;
                }
                if (month > 12)
                {
                    year++;
                    month = 1;
                }
                left--;
            }
        }
    }
    
    this.getDate = function getDate()
    {
        if (!this.isInvalid)
        {
            var tmpstr = padNumber(day,2) + "/" + padNumber(month,2) + "/" + year;
            return tmpstr;
        }
        else
            return null;
    }
}

function visibilityElem(elemID,state)
{
    this.elementID = elemID;
    this.state = state;
    
}

function imagePreload(up,over)
{
    this.img = new Image();
    this.imgUp = up; //Up Image URL
    this.imgOver = over; // Over Image URL
    this.img.src = this.imgOver; // Changes source to the Over image,for preloading
    
    this.changeImage = function changeImage(up,over)
    {
        this.imgUp = up;
        this.imgOver = over;
        this.img.src = this.imgOver;
    }

}

function preloadimg(imgname,up,over)
{	
	images[imgname] = new imagePreload(up,over);
}


function changeimg(img,type)
{
/*img is the object of type image, that will have its image changed.
  it must have had images preloaded using preload images, and specified its correct ID
  type is either 'over' or 'up', according to what it is now
  */

	if (type.toLowerCase() == 'over')
	{
		img.src = images[img.id].img.src;
	}
	else
		img.src = images[img.id].imgUp;;
}
function txtforhtml(txt)
{
	tmp = txt
	tmp = replaceall(tmp,'<','&lt;');
	tmp = replaceall(tmp,'>','&gt;');
	tmp = replaceall(tmp,' ','&nbsp;');
	tmp = replaceall(tmp,'\n','<br>');
	return tmp;
}

function replaceall(txt,from,to)
{
	tmp = txt;
	while (tmp.indexOf(from) != -1)
	{
		tmp = tmp.replace(from,to);
	}
	return tmp;

}

function resizeimage(img,maxwidth,maxheight)
{
	if (maxwidth > 0)
	{
		if (img.width > maxwidth)
			img.width = 440;
	}
	if (maxheight > 0)
	{
		if (img.height > maxheight)
			img.height = maxheight;
	}
}

function doForQueryStr(str)
{
	var tmp = str;
	tmp = tmp.replace('%','%25');
	tmp = tmp.replace('&','%26');
	tmp = tmp.replace('=','%3D');
	tmp = tmp.replace('?','%3F');
	return tmp;
	
}


function revCharAt(str,tofind)
{
	for (var i = str.length - tofind.length; i--; i >= 0)
		if (str.substr(i,tofind.length) == tofind)
			return i;
	return -1;	
	
}

function addInPlaceInCmb(cmb,txt,value,noduplicates)
{
	if (noduplicates)
	{
		for (var i = 0; i < cmb.options.length; i++)
		{
			if (cmb.options[i].text.toLowerCase() == txt.toLowerCase() && cmb.options[i].value.toLowerCase() == value.toLowerCase())
				return;
		}
	}
	var newprod = document.createElement('option');
	newprod.text = txt;
	newprod.value = value;
	ffplace = null;
	ieplace = 0;
	for (var i = 0; i < cmb.options.length; i++)
		if (cmb.options[i].text > txt)
		{
			ffplace = cmb.options[i];
			ieplace = i;
			break;
		}
	
	try {
		cmb.add(newprod,ffplace);
	}
	catch(ex)
	{
		cmb.add(newprod,ieplace);
	}
	return;
}


//returns the resolved id, through the resolvedIDs array.  If it resolves,
//returns the resolved id, else returns the same id as passed.
function resolveID(id)
{
    try
    {
        var tmp = resolvedIDs[id];
        if (tmp == null || tmp == "")
            return id;
        else
            return tmp;
    }
    catch (ex)
    {
        return id;
    }
}

//gets an element, but first checks if id can be resolved
function getElem(id)
{
    
	var currid;
	var elem;
    var tmp = id.id;
    if (tmp != null)
    {
	    elem = id;
	}
	else
	{
	    currid = resolveID(id);
	    elem = document.getElementById(currid) ;
	    if (elem == null)
		    elem = document.getElementsByName(currid)[0];
	
	}
	return elem;
}
function getElemName(name)
{	return document.getElementsByName(name); }


function padNumber(num,digits)
{
	var tmp,snum,sret
	snum = "" + num;
	tmp = digits - snum.length;
	sret = num
	for (k = 1; k <= tmp; k++)
		sret = "0" + sret
	return sret
}

function addToCmb(cmb,txt,val)
{
    var opt = document.createElement("OPTION");
	opt.text = txt;
	opt.value = val;
    try 
        {cmb.add(opt);}
    catch (ex)
       {cmb.add(opt,null);}
}

//adds an element, to a certain location in the option
function addToCmbByIndex(cmb,txt,val,index)
{
    var opt = document.createElement("OPTION");
	opt.text = txt;
	opt.value = val;
	var nodeIndex;
	
    try 
        {cmb.add(opt,index);}
    catch (ex)
       {
         cmb.add(opt,cmb.options[index]);
       }

}

function addToCmbInOrder(cmb,txt,val,addifexists,noitemsfromend,noitemsfromtop)
//noitemsfromend is the number of items NOT to count from the end
{
    if (noitemsfromend == "" || noitemsfromend == null) noitemsfromend = 0;
    if (noitemsfromtop == "" || noitemsfromtop == null) noitemsfromtop = 0;
    var opt = document.createElement("OPTION");
    var nodeIE,nodeMoz;
    var index;
	opt.text = txt;
	opt.value = val;
	var i,found;
	found=false;
	if (!addifexists)
	{
	    for (i = 0; i < cmb.options.length-noitemsfromend; i++)
	    {
	        if (cmb.options[i].value == val)
	        {
	            found = true;
	            break;
	        }
	    }
	}
	if (!found)
	{
	    if (noitemsfromend > 0)
	    {
	        nodeMoz = cmb.options[cmb.options.length-noitemsfromend];
	        nodeIE = cmb.options.length-noitemsfromend;
	    }
	    else
	    {
	        nodeMoz = null;
	        nodeIE = -1;
	    }
        index = cmb.options.length-noitemsfromend;
	    for (i = noitemsfromtop; i < cmb.options.length-noitemsfromend; i++)
	    {
	        if (cmb.options[i].text.toLowerCase() > txt.toLowerCase())
	        {
	            nodeIE = i;
	            nodeMoz = cmb.options[i];
	            index = i;
	            break;
	        }
	    }
        try 
            {cmb.add(opt,nodeIE);}
        catch (ex)
           {cmb.add(opt,nodeMoz);}
        cmb.selectedIndex = index;
    }
    return !found;

    
}


function forMySql(txt)
{
    var tmp = txt;
    tmp = tmp.replace("\\","\\\\");
    tmp = tmp.replace("'","\\'");
    return tmp;
    

}

function clearCmb(cmb)
{
    while (cmb.options.length > 0)
    {
        cmb.remove(0);
    }
}

function getCmbText(cmb)
{
    var txt = "";
    if (cmb.selectedIndex > -1)
        txt = cmb.options[cmb.selectedIndex].text;
    return txt;
}


function checkItemExistsInCmb(cmb,val)
{
    var i;
    for (i = 0; i < cmb.options.length; i++)
    {
        if (cmb.options[i].value.toLowerCase() == val.toLowerCase())
        {
            return true;
        }
    }
    return false;

}

function getKey(e)
{
    if (window.event)
       return window.event.keyCode;
    else if (e)
       return e.which;
    else
       return null;
}

function hideRow(row)
{
    row.style.display = 'none';
}

function showRow(row)
{
    try
    { //firefox
        row.style.display = "table-row";
    }
    catch (ex)
    { // ie
        row.style.display = "";
    }

}

function noValidation() {
    form_doValidation = "no";
}

function validationGroup(group) { 
   form_validationGroup = group;
}

function isRequired(id)
{
   var ctrl = getElem(id);
   ctrl = getElem(ctrl.id + '_required');
   if (ctrl == null) {
       alert('Control [' + id + '_required] does not exist for function ISREQUIRED'); }
   ctrl.value = 'yes';
}

function notRequired(id)
{
   var ctrl = getElem(id);
   ctrl = getElem(ctrl.id + '_required');
   if (ctrl == null) {
       alert('Control [' + id + '_required] does not exist for function NOTREQUIRED'); }
   ctrl.value = 'no';
}

//changes enabled state
function changeState(id,state)
{
   var ctrl = getElem(id);
   ctrl.disabled = !state;
   if (ctrl.type != 'checkbox' && ctrl.type != 'radio') 
   {
      if (state) 
      {
          ctrl.className = class_enabled[ctrl.id];   
      } else 
      { 
          ctrl.className = class_disabled[ctrl.id];   
      }
   }
}


function markError(state,id)
{
    var errtxt,ctrl;
    ctrl = getElem(id);
    errtxt = getElem(ctrl.id + '_err');
    if (state)
    {
        ctrl.className = class_error[ctrl.id];
        errtxt.value = 'true';
    }
    else
    {
        errtxt.value = 'false';
        ctrl.className = class_enabled[ctrl.id];
    }
    ctrl.focus();
}
            
function disable(id)
{
   var ctrl = getElem(id);
   ctrl.disabled = true;
   if (ctrl.type != 'checkbox' && ctrl.type != 'radio') 
       { ctrl.className = class_disabled[ctrl.id]; }   
}

function enable(id)
{
   var ctrl = getElem(id);
   var ctrlerr = getElem(ctrl.id + '_err');

   ctrl.disabled = false;
   if (ctrl.type != 'checkbox' && ctrl.type != 'radio') 
       { ctrl.className = class_enabled[ctrl.id]; }   
   if (ctrlerr != null)
   {
      if (ctrlerr.value == 'true') 
      {
         if (ctrl.type != 'checkbox' && ctrl.type != 'radio') 
             { ctrl.className = class_error[ctrl.id]; }
      }
   }
}

            
function expandRow(img,rowIDArray,rowStateArray,imgPlusUp,imgPlusOver,imgMinusUp,imgMinusOver)
{
    var rowID = rowIDArray[img.id];
    var row = getElem(rowID);
    var image = images[img.id];
    var state = rowStateArray[img.id];
    if (state) //row is showing, change to NOT showing
    {
        image.changeImage(imgPlusUp,imgPlusOver);
        hideRow(row);
    }
    else //row is NOT showing, change to show
    {
        image.changeImage(imgMinusUp,imgMinusOver);
        showRow(row);
    
    }
    rowStateArray[img.id] = !state;
}            


//offset is the minimum offset from the top until the object starts moving and
//aligning to top.  If offset is not 0, positioning of object must be relative
function setElemToTop(elementID,offset,win,doc) {
	var w = win || window;
	var d = doc || document;
	var b = d.body;
	var x = getElem(elementID);
	
	var newY = 0;
	var yOffset = w.pageYOffset || d.documentElement.scrollTop || d.body.scrollTop;
	yOffset -= offset;
	if (yOffset > 0)
	    newY = yOffset;
	x.style.top = newY + "px";
	
}

function getDocumentHeight() {
	var h;
	h = document.documentElement.clientHeight || window.height;
	return h;
}
function getDocumentWidth() {
	var w;
	w = document.documentElement.clientWidth || window.width;
	return w;
}

function setElemToBottom(elementID,elemHeight,win,doc) {
    if (elemHeight == null) return;
	var w = win || window;
	var d = doc || document;
	var b = d.body;
	var x = getElem(elementID);
	var docHeight = getDocumentHeight();
	var yOffset = w.pageYOffset || d.documentElement.scrollTop || d.body.scrollTop;
	var newY = docHeight - elemHeight + yOffset;
	
	x.style.top = newY + "px";
	
	
}

function getElemHeight(elem)
{
    var x = elem.clientHeight || elem.offsetHeight || elem.scrollHeight || elem.height ||
            elem.Height;
    return x;
}
function getElemWidth(elem)
{
    var x = elem.clientWidth || elem.offsetWidth  || elem.scrollWidth || elem.width ||
            elem.Width ;
    return x;
}
function formatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);
	tmpNumStr = tmpNum.toFixed(2);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function getAbsPos( oId, tl ) 
{
    var o = ( typeof oId == 'String' ) ? getElem( oId ) : oId;
    var val = 0;
    while ( o.nodeName != "BODY" ) 
    {
        val += parseInt( ( tl == 'top' ) ? o.offsetTop : o.offsetLeft );
        o = o.parentNode;
    }
    return val;
}

///txtArea - to hold the text, any text area - set as invisible
///txt - the text to copy
/// IE ONLY!
function CopyToClipBoard(txtArea,txt)
{
    txtArea.innerText = txt;
    var Copied = txtArea.createTextRange();
    Copied.execCommand("Copy");

}

function openPopup(url)
{
    window.open(url);
}
