var RE_FLOAT=/(^\-?\d+$)|(^\-?\d+\.\d+$)/;
var RE_INTEGER=/^\-?\d+$/;

String.prototype.trim = function () {return this.replace(/^\s*/, "").replace(/\s*$/, ""); }

function validateFloat(val,minVal, maxVal)
{
	if (val == null || val == "")
	{
		throw ("Empty");
	}
	if (!RE_FLOAT.test(val))
	{
		throw ("Value is not a number");
	}
	if ((minVal !=null) && (minVal != NaN) && (val < minVal))
	{
		throw ("Value is less than minimum allowed ("+minVal+")");
	}
	if ((maxVal !=null) && (maxVal != NaN) && (val > maxVal))
	{
		throw ("Value is greater than maximum allowed ("+maxVal+")");
	}
	return val;
}

function validateInt(val, minVal , maxVal )
{
    if (val == null || val == "")
    {
	    throw ("Empty");
    }

    if (!RE_INTEGER.test(val))
    {
        throw ("Value contains non-numeric characters");
    }
    var intVal = parseInt(val, 10);
    if (intVal == NaN)
    {
	throw ("Value is not a number");
    }
    if (minVal != null)
    {
	    var intMinVal = parseInt(minVal,10);
	    if ((intMinVal != NaN) && (intVal < intMinVal))
	    {
	        throw ("Value is less than minimum allowed("+intMinVal+")");
	    }
    }
    if (maxVal != null)
    {
	var intMaxVal = parseInt(maxVal,10);
	if ((intMaxVal != NaN) && (intVal > intMaxVal))
	{
	    throw ("Value is greater than maximum allowed("+intMaxVal+")");
	}
    }
    return intVal;
}



function parseDate(str, isUTC)
{

    if (str == "")
    {
	    throw ("Empty");
    }
    if (isUTC == null)
    {
        isUTC = false;
    }
    var re = new RegExp("(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d) (\\d\\d):(\\d\\d)(:(\\d\\d))?$");
    var result = re.exec(str);
    if (result == null)
    {
	    throw ("Invalid date format:"+str+".\nFormat accepted is mm/dd/yyyy hh:mm.");
    }

    var month = parseInt(result[1],10);
    if (month > 12 || month < 1)
    {
	    throw("Invalid month value:"+month+".\nAllowed range is 01-12");

    }
    var year = parseInt(result[3],10);
    if (year < 1970 || year > 2200)
    {
	    throw ("Invalid year value:"+year+".\nAllowed range is 1970-2200");
    }
    var day = parseInt(result[2],10);
    if (day < 1)
    {
	    throw("Invalid day of month value:"+day);
    }

    if (day>31)
    {
	    throw("Invalid day of month value:"+day);
    }
    if (day > 28)
    {
	    if (month == 2)
	    {
	        if ( ((year%4) != 0) || (day > 29))
	        {
	           throw("Invalid day of month value:"+day);
	        }
	    }
	    if (day > 30)
	    {
	        switch (month)
	        {
		        case 4:
		        case 6:
		        case 9:
		        case 11:
		            throw("Invalid day of month value:"+day);

		        default:
		        break;
	        }
	    }
	}


    var hour = parseInt(result[4],10);
    if (hour <0 || hour > 23)
    {
	    throw("Invalid hours value:"+hour+".\nAllowed range is 00-23");
    }
    var min = parseInt(result[5],10);
    if (min <0 || min >59)
    {
	    throw("Invalid minutes value:"+min+".\nAllowed range is 00-59");
    }

    var sec = 0;
    if (result[7] != "")
    {
        sec = parseInt(result[7],10);
        if (sec <0 || sec >59)
        {
	        throw("Invalid seconds value:"+sec+".\nAllowed range is 00-59");
        }

    }
    var dateTime  = new Date();
    if (!isUTC)
    {
        dateTime.setFullYear(year,month-1,day);
        dateTime.setHours(hour,min,0,0);
        dateTime.setSeconds(sec,0);
    }
    else
    {
        dateTime.setUTCFullYear(year,month-1,day);
        dateTime.setUTCHours(hour,min,0,0);
        dateTime.setUTCSeconds(sec,0);
    }
    return dateTime;
}



function parseDate2(str, isUTC)
{

    if (str == "")
    {
	    throw ("Empty");
    }
    if (isUTC == null)
    {
        isUTC = false;
    }
    //var re = new RegExp("(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d) (\\d\\d):(\\d\\d)(:(\\d\\d))?$");
    var re = new RegExp("(\\d\\d\\d\\d)-(\\d{1,2})-(\\d{1,2}) ?(\\d{1,2})?(?::(\\d{1,2}))?(?::(\\d{1,2}))?$");

    var result = re.exec(str);

    if (result == null)
    {
	    throw ("Invalid date format:"+str+".\nFormat accepted is yyyy-mm-dd hh:mm.");
    }

    var month = parseInt(result[2],10);
    if (month > 12 || month < 1)
    {
	    throw("Invalid month value:"+month+".\nAllowed range is 01-12");

    }
    var year = parseInt(result[1],10);
    if (year < 1970 || year > 2200)
    {
	    throw ("Invalid year value:"+year+".\nAllowed range is 1970-2200");
    }
    var day = parseInt(result[3],10);
    if (day < 1)
    {
	    throw("Invalid day of month value:"+day);
    }

    if (day>31)
    {
	    throw("Invalid day of month value:"+day);
    }
    if (day > 28)
    {
	    if (month == 2)
	    {
	        if ( ((year%4) != 0) || (day > 29))
	        {
	           throw("Invalid day of month value:"+day);
	        }
	    }
	    if (day > 30)
	    {
	        switch (month)
	        {
		        case 4:
		        case 6:
		        case 9:
		        case 11:
		            throw("Invalid day of month value:"+day);

		        default:
		        break;
	        }
	    }
	}


    var hour = 0;
    if (typeof(result[4]) != "undefined" && "" != result[4])
    {
	    hour = parseInt(result[4],10);

	    if (hour <0 || hour > 23)
	    {
		    throw("Invalid hours value:"+hour+".\nAllowed range is 00-23");
	    }
    }
    var min = 0;
    if (typeof(result[5]) != "undefined" && "" != result[5])
    {
    	min = parseInt(result[5],10);
    	if (min <0 || min >59)
    	{
	    throw("Invalid minutes value:"+min+".\nAllowed range is 00-59");
    	}
    }
    var sec = 0;
    if (typeof(result[6]) != "undefined" && result[6] != "")
    {
        sec = parseInt(result[6],10);
        if (sec <0 || sec >59)
        {
	        throw("Invalid seconds value:"+sec+".\nAllowed range is 00-59");
        }

    }
    var dateTime  = new Date();

    if (!isUTC)
    {
        dateTime.setFullYear(year,month-1,day);
        dateTime.setHours(hour,min,0,0);
        dateTime.setSeconds(sec,0);
    }
    else
    {
        dateTime.setUTCFullYear(year,month-1,day);
        dateTime.setUTCHours(hour,min,0,0);
        dateTime.setUTCSeconds(sec,0);
    }

    return dateTime;
}




function convertUTCTimeStringToLocalTimeString(utcString, ignoreSeconds )
{
    try
    {
        if (ignoreSeconds == null)
        {
            ignoreSeconds = true;
        }
        var utcDate = parseDate(utcString,true);
        var month = utcDate.getMonth()+1;
        if (month <10)
        {
            month = "0"+month;
        }
        var hours = utcDate.getHours();
        if (hours <10)
        {
            hours = "0"+hours;
        }

        var minutes = utcDate.getMinutes();
        if (minutes <10)
        {
            minutes = "0"+minutes;
        }

        var day = utcDate.getDate();
        if (day <10)
        {
            day = "0"+day;
        }
        if (ignoreSeconds)
        {
            return month+"/"+day+"/"+(utcDate.getFullYear())+" "+
                hours +":"+minutes;
        }
        var sec = utcDate.getSeconds();
        if (sec < 10)
        {
            sec = "0"+sec;
        }
        return month+"/"+day+"/"+(utcDate.getFullYear())+" "+
                hours +":"+minutes+":"+sec;

    }
    catch (error)
    {
        if (error != "Empty")
        {
            alert ("String "+utcString+" doesn't represent valid date/time value\n"+error);
        }
        return "";
    }
}


function convertLocalTimeToUTCTimeStringString(localString, ignoreSeconds)
{
    try
    {
        if (ignoreSeconds == null)
        {
            ignoreSeconds = true;
        }
        var localDate = parseDate(localString);
        var month = localDate.getUTCMonth()+1;
        if (month <10)
        {
            month = "0"+month;
        }
        var hours = localDate.getUTCHours();
        if (hours <10)
        {
            hours = "0"+hours;
        }

        var minutes = localDate.getUTCMinutes();
        if (minutes <10)
        {
            minutes = "0"+minutes;
        }

        var day = localDate.getUTCDate();
        if (day <10)
        {
            day = "0"+day;
        }
        if (ignoreSeconds)
        {
            return month+"/"+day+"/"+(localDate.getUTCFullYear())+" "+
                hours +":"+minutes;
        }

        var sec = localDate.getUTCSeconds();
        if (sec < 10)
        {
            sec = "0"+sec;
        }
        return month+"/"+day+"/"+(localDate.getUTCFullYear())+" "+
                hours +":"+minutes+":"+sec;
    }
    catch (error)
    {
        if (error != "Empty")
        {
            alert ("String "+localString+" doesn't represent valid date/time value\n"+error);
        }
        return "";
    }
}

function trimLeft(str)
{
    var re = /^\s+/;
    str = str.replace(re,"");
    return str;
}

function trimRight(str)
{
    var re = /\s+$/;
    str = str.replace(re,"");
    return str;
}
function trim(str)
{

    return trimRight(trimLeft(str));
}


function replaceSpecialCharacters(inStr)
{
    if (inStr == null)
    {
        return null;
    }
    var str = new String();
    str = inStr;

    str = str.replace(/\+/g ,"%2B");
    //str = str.replace(/\//g ,"%2F");
    str = str.replace(/\?/g ,"%3F");
    str = str.replace(/\%/g ,"%25");
    str = str.replace(/#/g ,"%23");
    str = str.replace(/&/g ,"%26");
    str = str.replace(/\'/g ,"''");

    return str;
}


function replaceSpecialCharacters2(inStr)
{
    if (inStr == null)
    {
        return null;
    }
    var str = new String();
    str = inStr;

//    str = str.replace(/\+/g ,"%2B");

//    str = str.replace(/\?/g ,"%3F");
//    str = str.replace(/\%/g ,"%25");
//    str = str.replace(/#/g ,"%23");
//    str = str.replace(/&/g ,"%26");
    str = str.replace(/\'/g ,"''");

    return str;
}


function handleExpiredPage(selectedMenu)
{
//    document.cookie = "prevSelection="+escape(selectedMenu)+"; path=/;";
    top.document.location = top.document.location;

}


function getCookie(name)
{
    var retval = "";
    var myCookie = " " +document.cookie+";";
    var searchName = " "+name+"=";
    var startOfCookie = myCookie.indexOf(searchName);
    var endOfCookie;

    if (startOfCookie != -1)
    {
        startOfCookie += searchName.length;
        endOfCookie = myCookie.indexOf(";", startOfCookie);
        retval = unescape(myCookie.substring(startOfCookie, endOfCookie));

    }
    return retval;
}

function clearCookie(name)
{
    var week = 7*24*60*60*1000;
    var expDate = new Date();
    expDate.setTime(expDate.getTime() - week);
    document.cookie = name+"=expired; expires ="+expDate.toUTCString();

}


// remove nodes from xml represents dataset if [elemIdxToTest] element is an empty string.
function removeEmptyNodesFromXML(xmlDtSrc, elemIdxToTest)
{

// expected xml format:
// <root>

//    <record>
//        <column0></column0>       // idx = 0  // if elemIdxToTest == 0 and column0's value is an empty string, than this <record> element is deleted
//        <column1></column1>       // idx = 1
//        ...................
//        <columnN></columnN>       // idx = n
//    </record>

//    <record>
//        <column0></column0>
//        <column1></column1>
//        ...................
//        <columnN></columnN>
//    </record>
//</root>

    if (elemIdxToTest == null)
    {
        elemIdxToTest = 0;
    }
    var workDtSrc = xmlDtSrc;
    if (workDtSrc != null)
    {
        var emptyNodes = new Array();
        var j=-1;
        if (workDtSrc.documentElement != null)
        {
            for (var i = 0; i< workDtSrc.documentElement.childNodes.length; i++)
            {
                if (workDtSrc.documentElement.childNodes[i].childNodes[elemIdxToTest] != null)
                {
                    if (workDtSrc.documentElement.childNodes[i].childNodes[elemIdxToTest].text == "")
                    {
                        emptyNodes[++j] = workDtSrc.documentElement.childNodes[i];
                    }
                }
                else
                {
                    throw ("wrong dataset format");
                }
            }
            for (var i = 0; i <emptyNodes.length; i ++)
            {
                workDtSrc.documentElement.removeChild(emptyNodes[i]);
            }
        }
    }
    return workDtSrc;
}

function createComboBoxItemsFromXMLData(comboBox, xmlNode,idField,valueField)
{

    if (comboBox == null || xmlNode == null)
    {
        return;
    }
    if ( (idField == null) || (idField == "") )
    {
        idField = "id";
    }
    if ( (valueField == null) || (valueField == "") )
    {
        valueField = "name";
    }
    for (i = 0; i <xmlNode.childNodes.length; i++)
    {
        var opt=document.createElement('option');
        opt.value =xmlNode.childNodes[i].getAttribute(idField);
        opt.text  =xmlNode.childNodes[i].getAttribute(valueField);
        if ((xmlNode.childNodes[i].getAttribute(idField) != null) && (xmlNode.childNodes[i].getAttribute(valueField) != null))
        {
            try
            {
                comboBox.add(opt,null); // standards compliant
            }
            catch(ex)
            {
                comboBox.add(opt); // IE only
            }
        }
    }
}


function disableElement (obj, val)
{
    if (obj == null)
    {
	return;
    }



    if (obj.disabled != null)
    {
        obj.disabled= val?"disabled":"";
    }
    else if (obj.readOnly != null)
    {
	obj.readOnly = val;

	//if ((obj.type != null && obj.type.toUpperCase() == "TEXT") || (obj.tagName.toUpperCase() == "TEXTAREA")
       // ||obj.type.toUpperCase() == "PASSWORD")
       // {
	   // if (val)
	   // {
	   //     obj.className="disabled_input";
	   // }
	   // else
	   // {
	   //     obj.className="";
	   // }
	//}
    }


    for (var i = 0; i < obj.childNodes.length; i++)
    {
	disableElement(obj.childNodes[i], val);
    }
}




function clearText (obj)
{
    if (obj == null)
    {
	return;
    }



    if ( (obj.tagName != null) &&  (obj.tagName.toUpperCase() == "SELECT") )
    {
	obj.selectedIndex = 0;
    }
    else if  ( (obj.type != null) && (obj.type.toUpperCase() == "CHECKBOX"))
    {
    	obj.checked = false;
    }
    else  if ((obj.type != null && obj.type.toUpperCase() == "TEXT") || ((obj.tagName!= null) && (obj.tagName.toUpperCase() == "TEXTAREA")) ||
	((obj.tagName!= null) && (obj.tagName.toUpperCase() == "HIDDEN"))
    )
    {
	obj.value="";
    }






    for (var i = 0; i < obj.childNodes.length; i++)
    {
	clearText(obj.childNodes[i]);
    }
}


function getPageOffsetLeft (el)
{
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}


function getPageOffsetTop (el)
{
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}




function GetXmlHttpObject()
 {
 var objXMLHttp=null;
 if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
 else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
 return objXMLHttp;
 }


function addHandler(object, event, handler, useCapture)
{
	if (object.addEventListener)
	{
		object.addEventListener(event, handler, useCapture);
	}
	else if (object.attachEvent)
	{
		object.attachEvent('on' + event, handler);
	}
	else
	{
		object['on' + event] = handler;
	}
}

function isChanged(name)
{
        var el = document.getElementById(name);
        var opt, hasDefault, i = 0, j;
        switch (el.type) {
                case 'text' :
                case 'textarea' :
                case 'hidden' :
                        if (!/^\s*$/.test(el.value) && el.value != el.defaultValue) return true;
                        break;
                case 'checkbox' :
                case 'radio' :
                        if (el.checked != el.defaultChecked) return true;
                        break;
                case 'select-one' :
                case 'select-multiple' :
                        j = 0, hasDefault = false;
                        while (opt = el.options[j++])
                                if (opt.defaultSelected) hasDefault = true;
                        j = hasDefault ? 0 : 1;
                        while (opt = el.options[j++])
                                if (opt.selected != opt.defaultSelected) return true;
                        break;
        }
        return false;
}
;
