var images;


if (images == null)
    images = new Array();
var lastHelp;
if (lastHelp == null)
    lastHelp = '';
/*
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 refreshWindow(currWindow)
{
if (currWindow == null)
    currWindow = window;
    var sURL = unescape(currWindow.location.pathname);
    currWindow.location.href = sURL;
}



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.Day = day;
    this.Month = month;
    this.Year = year;
    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.compareTo = function compareTo(dt)
    {
        if (this.Year > dt.Year)
        {
            return 1;
        }
        else if (this.Year < dt.Year)
        {
            return -1;
        }
        else
        {
            if (this.Month > dt.Month)
            {
                return 1;
            }
            else if (this.Month < dt.Month)
            {
                return -1;
            }
            else
            {
                if (this.Day > dt.Day)
                {
                    return 1;
                }
                else if (this.Day < dt.Day)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            
        }
        
    }
    
    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;
    }

}

//name of image
function preloadimg(imgname,up,over)
{	
    images[imgname] = new imagePreload(up,over);
	var img = getElem(imgname);
	img.src = up;

	
	
}
function getImagePreload(id)
{
return images[id];
    
    
}

function changeimg(image,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
  */
    var id = null;
    try
    {
        if (image.id != null) 
            id = image.id;
    }
    catch (ex)
    {}
    if (id == null)
         id = image;
    id = resolveID(id);
    var o = getImagePreload(id);
    var img = getElem(id);
	if (type.toLowerCase() == 'over')
	{
		img.src = o.img.src;
	}
	else
		img.src = o.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;
}

function resolveIDCheckInList(ID,list)
{
    var j;
    for (j = 0; j < list.length; j++)
    {
        var e = list[j];
        var elemID = e.id;
        
        if ((elemID != null) && (elemID.length >= ID.length) && (elemID.substr(elemID.length - ID.length,ID.length) == ID) &&
        (elemID.length == ID.length || (elemID.substr(elemID.length-ID.length-1,1) == '_')))
        {
            return elemID;
        }
    }
    for (j = 0; j < list.length; j++)
    {
        var e = list[j];
        if (e.childNodes != null)
        {
            var elemID = resolveIDCheckInList(ID,e.childNodes);
            if (elemID != null)
                return elemID;
        }
    }
    return null;

}
function resolveIDFromDocument(ID)
{

    if (ID == null)
        return "";
    var doc = document;
    var i,j;
    var id = null;
    for (i = 0; i < doc.forms.length; i++)
    {
        var f = doc.forms[i];
        id = resolveIDCheckInList(ID,f.childNodes);
        if (id != null)
           break;
    }
    if (id == null)
        id = ID;
    return id;
}

//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)
{
    var elem = document.getElementById(id);
    if (elem != null)
        return id;
    var err = false;
    try
    {
        var tmp = resolvedIDs[id];
        if (tmp == null || tmp == "")
            err = true;
        else
            return tmp;
    }
    catch (ex)
    {
        err = true;
    }
    if (err)
        return resolveIDFromDocument(id);
    else
        return null;
}

function getElem2(id)
{
    return document.getElementById(id);
}

//gets an element, but first checks if id can be resolved
function getElem(id)
{
    var currid;
	var elem = null;
    if (typeof(id) != "string")
        id = id.id;
     elem = document.getElementById(id);
     if (elem == null)
     {
        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)
{
    if (cmb.id == null)
        cmb = getElem(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 setValGroupAndClick(valGroup,btnID)
{
    var btn = document.getElementById(btnID);
    com.cs.ui.form.setValidationGroup(valGroup);
    btn.click();
}

function noValidation() {
    _global.form_doValidation = "no";
}

function validationGroup(group) { 

   _global.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);
   var uiElem = ctrl.getField();
   if (uiElem != null)
        uiElem.setEnabled(state);
    else
    {
        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);
    var uiElem = ctrl.getField();
    uiElem.setError(state);
    //ctrl.focus();
    /*
    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);
   var uiElem = ctrl.getField();
   if (uiElem != null)
       uiElem.setEnabled(false);
   else
        ctrl.disabled = true;
    
    /*    
   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');
   var uiElem = ctrl.getField();
   if (uiElem != null)
        uiElem.setEnabled(true);
   else
        ctrl.disabled = false;
    /*
   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(decimalNum);

	// 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);
}
function hideElem(elem)
{
    var element = null;
    try
    {
        if (elem.id != '' && elem.id != null)
        {
            element = elem;
        }
    }
    catch (ex) {}
    if (element == null)
    {
        element = getElem(elem);
    }
    element.style.display = 'none';
    
}

function showElem(elem)
{
    var element = null;
    try
    {
        if (elem.id != '' && elem.id != null)
        {
            element = elem;
        }
    }
    catch (ex) {}
    if (element == null)
    {
        element = getElem(elem);
    }
    element.style.display = '';
    
}

function openPopup(url)
{
    window.open(url);
}
//menubar - whether to show the menubar or not
//toolbar - whether to show the toolbar or not
//scrollbar - whether to show scrollbar
function openPopupCenter(url,width,height,menubar,toolbar,scrollbar)
{
    
var left,top;
    left = (screen.availWidth - width) / 2;
    top = (screen.availHeight - height) / 2;
    openPopupSettings(url,width,height,left,top,menubar,toolbar,scrollbar);

}
//menubar - whether to show the menubar or not
//toolbar - whether to show the toolbar or not
function openPopupSettings(url,width,height,left,top,menubar,toolbar,scrollbar)
{
    var sMenuBar,sToolBar,sScrollbar;
    if (menubar) sMenuBar = 'yes'; else sMenuBar = 'no';
    if (toolbar) sToolBar= 'yes'; else sToolBar= 'no';
    if (scrollbar) sScrollbar= 'yes'; else sScrollbar= 'no';
    window.open(url,'_blank','menubar='+sMenuBar+',toolbar='+sToolBar+',width=' + width + 
        ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=' + sScrollbar);

}
function getInnerWidth(element)
{
	return element.clientWidth;
}
function getInnerHeight(element)
{
    return element.clientHeight;
}
function getOuterWidth(element)
{
    return element.offsetWidth;
}
function getOuterHeight(element)
{
    return element.offsetHeight;
}

function getWindowWidth()
{
	if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth; // IE6 with doctype
	
	if (window.innerWidth) return window.innerWidth; //All but IE
	if (document.body.clientWidth) return document.body.clientWidth; //	IE other
	return 0;

}
function getWindowHeight()
{
	if (window.innerHeight) return window.innerHeight; //All but IE
	if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight; // IE6 with doctype
	if (document.body.clientHeight) return document.body.clientHeight; // IE other
	return 0;

}
function gotoAnchor_callback(name)
{

    var url = window.location.href;
    var hashPos = url.lastIndexOf('#');
    if (hashPos > -1)
    {
        url = url.substr(0,hashPos);
    }
    url += '#' + name;
    window.location = url;
    clearInterval(gotoanchor_timerid);
}

function gotoAnchor(name)
{
    var s = "gotoAnchor_callback('" + name + "')";
    gotoanchor_timerid = setInterval(s,100);
    
    
    
}

function centerInScreen(w)
{

    var x,y,width,height;
    width = w.outerWidth;
    height = w.outerHeight;
    width += 30;
    height += 60;
    x = (screen.availWidth - width) / 2;
    y = (screen.availHeight - height) / 2;
    w.moveTo(x,y);
}

function refreshParent()
{
    refreshWindow(opener);
}

