var isOpera = (navigator.userAgent).toUpperCase().indexOf("OPERA") > -1;
var isIE = (navigator.appVersion).toUpperCase().indexOf("MSIE") > -1 && !isOpera;
var isGecko = (navigator.userAgent).toUpperCase().indexOf("GECKO") > -1;

var popupWindow;
var win = window;    // window to search.
var n   = 0;

function findInPage(str) {
    var txt, i, found;

    if (str == "")
        return false;

    if (isIE) {
        txt = win.document.body.createTextRange();

        // Find the nth match from the top of the page.
        for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
            txt.moveStart("character", 1);
            txt.moveEnd("textedit");
        }

        if (found) {      // If found, mark it and scroll it into view.
            txt.moveStart("character", -1);
            txt.findText(str);
            txt.select();
            txt.scrollIntoView();
            n++;
        } else { // Otherwise, start over at the top of the page and find first match.
            if (n > 0) {
                n = 0;
                findInPage(str);
            } else  // Not found anywhere, give message.
                alert("'" + str + "' not found");
        }
    }  else {
        alert("Your browser doesn't support this");
    }

    return false;
}

function do_resize() {
    if (isIE)
        document.body.scroll="no";

    if (typeof document.getElementById('contentdiv') == 'undefined')
        return;

    if (isGecko) {
        document.getElementById('contentdiv').style.height = window.innerHeight - 130;
        document.getElementById('contentdiv').style.width = window.innerWidth - 205;
    } else {
        document.getElementById('contentdiv').style.height = document.body.clientHeight - 130;
        document.getElementById('contentdiv').style.width = document.body.clientWidth - 205;
    }
}

function maxLength(obj) {
    var MAX_LENGTH = 255;

    var inputStr = obj.value;
    var strlength = inputStr.length;
    if (strlength > MAX_LENGTH) {
      obj.value = inputStr.substring(0, MAX_LENGTH);
    }

    var val = MAX_LENGTH - obj.value.length;
    if (val < 0){
      val = 1;
      obj.value = inputStr.substring(0,MAX_LENGTH-1);
    }
}

function checkEmail(email) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
        return true ;
    }
    return false;
}

function unload_orphan_popup(name, popup) {
    if (popup != null)
        popup.close();
    return null;
}

function do_onunload() {
    popup = unload_orphan_popup("__calendar", popupWindow);
}

function numberCheck(e) {
	var keyCode;
    var e = !e ? window.event : e;

	if (e.keyCode)
	    keyCode = e.keyCode;
	else if (e.which)
	    keyCode = e.which;

	if (keyCode == 8 || keyCode == 0){ 	//Ignore the Netscape value for backspace,left,right,delete. IE has no value
	  	return true;
	}

	if (keyCode<48 || keyCode >58){
		return false;
	}

	return true;
}

// Supported date format is yyyy-mm-dd.
function checkDateFormat(data) {
    var separator = "-";

    if ((data.charAt(4) != separator) || (data.charAt(7) != separator) || (data.length != 10))
        return false;

    if (isNotNumber(data.charAt(0)) || isNotNumber(data.charAt(1)) ||
            isNotNumber(data.charAt(2)) || isNotNumber(data.charAt(3)) ||
            isNotNumber(data.charAt(5)) || isNotNumber(data.charAt(6)) ||
            isNotNumber(data.charAt(8)) || isNotNumber(data.charAt(9))) {
		return false;
    }

    day = eval(data.charAt(8)+data.charAt(9));
    day = parseInt(day);
    mnth = eval(data.charAt(5)+data.charAt(6));
    mnth = parseInt(mnth);
    year = eval(data.charAt(0)+data.charAt(1)+data.charAt(2)+data.charAt(3));
    year = parseInt(year);

    if (mnth>12 || mnth<1)
        return false;

    // mnthArray[0] is january, mnthArray[11] is december
    mnthArray = new Array(31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31 ,31 ,30 ,31 ,30 ,31);

    // IE number months starting with january = 0
    mnth = mnth - 1;
    if (day > parseInt(mnthArray[parseInt(mnth)]))
        return false;

	return true;
}

function isNotNumber(num) {
    return  isNaN(num) || (num == ' ');
}

function isLeapYear(intYear) {
	return ( (0 == (intYear % 4)) && ( (0 != (intYear % 100)) || (0 == (intYear % 400))));
}

// Supported date format is yyyy-mm-dd.
function parseDate(dateStr) {
    if (!checkDateFormat(dateStr))
        return null;

    var day = eval(dateStr.charAt(8) + dateStr.charAt(9));
    day = parseInt(day);
    var month = eval(dateStr.charAt(5) + dateStr.charAt(6));
    month = parseInt(month);
    var year = eval(dateStr.charAt(0) + dateStr.charAt(1) + dateStr.charAt(2) + dateStr.charAt(3));
    year = parseInt(year);

    return new Date(year, month, day, 0, 0, 0)
}

function getFormatedNum(num) {
    return ((num <= 9) ? '0' : '') + num;
}

function openBrWindow(theURL, winName, width, height) {
   var left = (window.screen.width - width)/2;
   var top = (window.screen.height - height)/2;
   var features = 'resizable=no, scrollbars=no, width=' + width + ', height=' + height + ', left=' + left + ', top=' + top;
   newWindow = window.open(theURL, winName, features);
   newWindow.focus();
}

//
// Calendar
//

function writeHTMLCalendar(oWhere, jsOnLoad) {
	var strHTML;
	strHTML = "<HTML>\n";
	strHTML = strHTML + "   <HEAD>\n";
	strHTML = strHTML + "      <script language=\"javascript\" src=\"js/common.js\"></script>\n";
	strHTML = strHTML + "      <script language=\"javascript\" src=\"js/calendar.js\"></script>\n";
	strHTML = strHTML + "      <link rel=\"stylesheet\" href=\"css/styles.css\" type=\"text/css\"/>\n";
	strHTML = strHTML + "\n";
	strHTML = strHTML + "      <Title>Calendar</Title>\n";
	strHTML = strHTML + "   </HEAD>\n";
	strHTML = strHTML + "       <BODY>\n";
	strHTML = strHTML + "           <TABLE>\n";
	strHTML = strHTML + "              <TR>\n";
	strHTML = strHTML + "                <TD>&nbsp;</TD>\n";
	strHTML = strHTML + "                <TD>&nbsp;</TD>\n";
	strHTML = strHTML + "                <TD valign=\"top\">\n";
	strHTML = strHTML + "                    <DIV id=\"calendar\"></DIV>\n";
	strHTML = strHTML + "                </TD>\n";
	strHTML = strHTML + "              </TR>\n";
	strHTML = strHTML + "           </TABLE>\n";
	strHTML = strHTML + "\n";
	strHTML = strHTML + "           <SCRIPT language=\"javascript\">\n";
	strHTML = strHTML + "           function waitJSLoad()\n";
	strHTML = strHTML + "           {\n";
	strHTML = strHTML + "             if ( typeof(newCalendars) == \"undefined\" )\n";
	strHTML = strHTML + "           	 {\n";
	strHTML = strHTML + "           		setTimeout(\"waitJSLoad()\",10);\n";
	strHTML = strHTML + "           	 }\n";
	strHTML = strHTML + "             else\n";
	strHTML = strHTML + "           	 {\n";
	strHTML = strHTML + "           		" + jsOnLoad + "\n";
	strHTML = strHTML + "           	 }\n";
	strHTML = strHTML + "           }\n";
	strHTML = strHTML + "\n";
	strHTML = strHTML + "           waitJSLoad();\n";
	strHTML = strHTML + "\n";
	strHTML = strHTML + "           </SCRIPT>\n";
	strHTML = strHTML + "      </BODY>\n";
	strHTML = strHTML + "</HTML>\n";

	oWhere.write( strHTML );
}

function openCalendar(dateid) {
   	var width = 245;
   	var height = 210;
   	var top = (screen.height - height) / 2;
   	var left = (screen.width - width) / 2;
	var jsOnLoad = "newCalendars('calendar','" + dateid + "');";

   	popupWindow = window.open ("", '__calendar', config='height='+height+', width='+width+', left='+left+', top='+top+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
	writeHTMLCalendar( popupWindow.document, jsOnLoad );

   	if(isIE)
		setTimeout('popupWindow.focus()', 250);
   	else
   		popupWindow.focus();
}