
function selectLocale(loc) {
	current_url = location.href;
	//send the url to the page that will change the locale in order to return to the same page
}

function divDisplay(divStr, OnOff) {
    var div = document.getElementById(divStr);
	if (div)
	{
		if (OnOff == 1) {
			div.style.display = 'block';
			}
	    else if (OnOff == 0) {
	        div.style.display = 'none';
	        }
	    else {
	        if (div.style.display != 'block')
			    div.style.display = 'block';
	        else
	            div.style.display = 'none';
	    }
	    return true;
	}
}

function highlight(divStr, OnOff, colorStr) {
    var div = document.getElementById(divStr);
	if (div)
	{
	    if (OnOff) 
		    div.style.backgroundColor = colorStr;
		else
		    div.style.backgroundColor = '';
    }
}

function highlightall(divStr, OnOff, colorStr, maxCount)
{
    for (var i=0;i<maxCount;i++)
    {
        highlight(divStr+i, OnOff, colorStr);
    }
}

function updatepage(str, targetDiv){
    document.getElementById(targetDiv).innerHTML = str;
}
 
function dropit(e, whichone, offsetX, offsetY){
//	curmenuID = eval(whichone).id;

//	var zindex=100
//	if (window.themenu&&themenu.id!=curmenuID)
//		themenuStyle.visibility = "hidden";
    themenu = eval(whichone);	
    themenuStyle = themenu.style;
	
	if (themenuStyle.visibility == "hidden"){
		themenuStyle.visibility = "visible"
    //	themenuStyle.zIndex=zindex++;
        themenuStyle.zIndex=100;
	    themenuoffsetX = 0;
	    themenuoffsetY = 0;
    //	alert(themenuoffsetX + ' , ' + event.clientX + ' , ' + event.offsetX);
	    themenuStyle.left = themenuoffsetX + event.clientX - event.offsetX + offsetX;
	    themenuStyle.top = themenuoffsetY + event.clientY - event.offsetY + offsetY;
	}
	else
		themenuStyle.visibility = "hidden";
	return false
}

function helpBubble(whichone, e, NoOffsetX) {

    var winW = document.documentElement.clientWidth;

    var posx = 0;
    var posy = 0;

    if (!e) var e = window.event;
    if (e) {
        if (e.pageX || e.pageY) { // FireFox, Mozilla, Opera7, IE7
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) { // IE6 + above mentioned browsers
            posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }
    }

    themenu = document.getElementById(whichone);
    themenuStyle = themenu.style;

    if ((winW >= 800) && (NoOffsetX != 1)) {
        posx = posx - ((winW - 800) / 2);
    }

    themenuStyle.left = posx + "px";
    themenuStyle.top = posy + "px";
    divDisplay(whichone);

}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function isDateUK(dtStr) {
    var dtCh = "/";
    var minYear = 1900;
    var maxYear = 2100;

    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strDay = dtStr.substring(0, pos1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        return false
    }
    return true
}

function IsNumeric(sText)
{
//checks for a number and returns true or false
   var ValidChars = "0123456789.";
   var Char;
   var i;

   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         return false;
         }
   }
   return true;
}

function IsFile(sText)
{
//checks for the characters : \ .
    var searchChars = ":\.";
    var Char;
    var i;
    
    for (i = 0; i < searchChars.length; i++)
    {
        Char = searchChars.charAt(i);
        if (sText.indexOf(Char) == -1)
        {
            return false;
        }
    }
    return true;
}

function IsDollar(sText)
{
//checks for a dollar format and returns true or false
   var ValidChars = "0123456789.,$";
   var Char;
   var i;

   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         return false;
         }
   }
   return true;
}

function formatFloat(floatStr, dec)
{
//formats floats to specified decimal places
    var multVal;
    var tempVal;
    var addVal;
    
    multVal = Math.pow(10,dec);
    tempVal = Math.round(floatStr * multVal) / multVal;
    
    if (String(tempVal).indexOf(".") == -1)
    {
        addVal = String(multVal).replace("1", ".");
        
        return tempVal + addVal;
    }
    else if (String(tempVal).indexOf(".") != String(tempVal).length - (dec + 1))
    {
        var i = (String(tempVal).indexOf(".")) - (String(tempVal).length - (dec + 1));
        multVal = Math.pow(10, i);
        addVal = String(multVal).replace("1", "");
        
        return tempVal + addVal;
    }
    else
    {
        return tempVal;
    }

}

function timeToDecimal(timeVal)
{
//converts time to decimal value
    if (timeVal != "")
    {
        var tempVal = String(timeVal).toLowerCase();
        var colPos;
        var hourVal;
        var minVal;
        
        if ((tempVal.indexOf(":") != -1) && ((tempVal.indexOf("am") != -1) || (tempVal.indexOf("pm") != -1)))
        {
            //12:00 PM        
            colPos = tempVal.indexOf(":");
            hourVal = Number(tempVal.substr(0, colPos));
            minVal = Number(tempVal.substr(colPos+1, 2));
            minVal = minVal / 60;
            
            if ((tempVal.indexOf("pm") != -1) && (hourVal != 12))
            {
                hourVal += 12;
            }
            else if ((tempVal.indexOf("am") != -1) && (hourVal == 12))
            {
                hourVal -= 12;
            }
            
            //alert(1);
            tempVal = hourVal + minVal;
        }
        else if (tempVal.indexOf(":") != -1)
        {
            //12:00
            colPos = tempVal.indexOf(":");
            hourVal = Number(tempVal.substr(0, colPos));
            minVal = Number(tempVal.substr(colPos+1, 2));
            minVal = minVal / 60;
            
            //alert(2);
            tempVal = hourVal + minVal;
        }
        else if ((tempVal.indexOf("am") != -1) || (tempVal.indexOf("pm") != -1))
        {
            //12 PM
            hourVal = tempVal.substr(0, tempVal.indexOf("m")-1);
            hourVal = Number(hourVal.replace(" ", ""));
            
            if ((tempVal.indexOf("pm") != -1) && (hourVal != 12))
            {
                hourVal += 12;
            }
            else if ((tempVal.indexOf("am") != -1) && (hourVal == 12))
            {
                hourVal -= 12;
            }
            
            //alert(3);
            tempVal = hourVal;
        }

        return tempVal; 
    }
    
    return timeVal
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function getDatePart(dateStr, part)
{
    var dtCh = "/";
	var pos1=dateStr.indexOf(dtCh);
	var pos2=dateStr.indexOf(dtCh,pos1+1);
	var strMonth=dateStr.substring(0,pos1);
	var strDay=dateStr.substring(pos1+1,pos2);
	var strYear=dateStr.substring(pos2+1);
	
	if (part == "m")
	{
	    return strMonth;
	}
	else if (part == "d")
	{
	    return strDay;
	}
	else if (part == "y")
	{
	    return strYear;
	}
}

function scrollPage()
{
    window.scroll(0,1000000);
}

function formatNumber(numberval, totalchars)
{
    var diff = Number(totalchars) - String(numberval).length;
    var zerostr = "";
    
    if (diff > 0)
    {
        for (var i = 0; i < diff; i++) 
        {   
             zerostr += "0";
        }  
    }
    
    return zerostr + String(numberval);
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isEmail(address) {
	var atPos, dotPos, wsPos, z;

	atPos = address.indexOf("@");
	dotPos = address.lastIndexOf(".");
	wsPos = address.indexOf(" ");
	z = address.length;
	
	// no whitespace
	if (wsPos > 0) return false;
	// '@' must appear in the second spot or later
	if (atPos < 1) return false;
	// Domain name after the '@' must be at least three characters
	if (atPos > (z - 4)) return false;
	// Last dot must appear after the '@' and before the top-level domain (at least two characters)
	if ((dotPos < atPos) || (dotPos > z - 3)) return false;
	
	// Final character must be a letter (not a semicolon, comma, period, or other character)
	if (!isAlpha(address.charAt(z-1))) return false;
	
	return true;
}

function isAlpha(txt) {
	var i, c;

  for (i = 0; i < txt.length; i++) {
		c = txt.charAt(i);
    if (!((c >= 'a') && (c <= 'z')) && !((c >= 'A') && (c <= 'ZZ'))) return false;
  }
  return true;	
}
