var strBrowser; 
if (navigator.appName == 'Microsoft Internet Explorer') {
    strBrowser = 'Explorer';
}
else {
    if (navigator.userAgent.indexOf('Chrome') > 1) {
        strBrowser = 'Chrome';
    }
    else if (navigator.userAgent.indexOf('Firefox') > 1) {
        strBrowser = 'Firefox';
    }
    else if (navigator.userAgent.indexOf('Safari') > 1) {
        strBrowser = 'Safari';
    }
    else if (navigator.userAgent.indexOf('Opera') == 0) {
        strBrowser = 'Opera';
    }
}

function populateDD(obj, co) {
    obj.length = 0;
    row = co.split(getrd());
    for (var i = 0; i < row.length - 1; i++) {
        fields = row[i].split(getcd());
        obj.options[i] = new Option(fields[0], fields[1]);
    }
}

function populateDDBlank(obj, co) {
    obj.length = 0;
    row = co.split(getrd());
    obj.options[0] = new Option('', 0);
    for (var i = 0; i < row.length - 1; i++) {
        fields = row[i].split(getcd());
        obj.options[i+1] = new Option(fields[0], fields[1]);
    }
}

function replaceString(x, y, z) {
	var pos = 0;
	var len = y.length;
	pos = x.indexOf(y)
	while ( pos != -1 )
	{
		var pre = x.substring(0,pos);
		var post = x.substring(pos+len,x.length);
		x = pre + z + post;
		pos = x.indexOf(y);
	}
	return x;
}

if (navigator.appName == 'Microsoft Internet Explorer') {
    strBrowser = 'IE';
    tblTop = 0;
}
else {
    if (navigator.userAgent.indexOf('Chrome') > 1) {
        strBrowser = 'Chrome';
        tblTop = 1;
    }
    else if (navigator.userAgent.indexOf('Firefox') > 1) {
        strBrowser = 'Firefox';
        tblTop = 2;
    }
    else if (navigator.userAgent.indexOf('Safari') > 1) {
        strBrowser = 'Safari';
        tblTop = 2;
    }
    else if (navigator.userAgent.indexOf('Opera') == 0) {
        strBrowser = 'Opera';
        tblTop = 3;
    }
}

function isEmpty(s){   
	return ((s == null) || (s.length == 0));
}

function isInteger(s){   	
	var i;
    if (isEmpty(s)) { return false;}
	for (i = 0; i < s.length; i++){   
       	var c = s.charAt(i);
       	if ((c == "-" || c == "+" ) && i == 0 ) continue;
       	if (!isDigit(c)) { return false;}
    }
    return true;
}

function isNumeric(s) { 
	var i,j=0;
    if (isEmpty(s)) { return false;}
    for (i = 0; i < s.length; i++) {
	    var c = s.charAt(i);
       	if ((c == "-" || c == "+") && i == 0) { continue; }
       	if (!isDigit(c) && c != ".") { return false; }
       	if (c == ".") { j++; } 
       	if ( j > 1) {return false;}
    }
    return true;
}

function addDays(pdate,pdays){
	var newdate=new Date(pdate);
	var newtimems=newdate.getTime()+(pdays*24*60*60*1000); //Calculate the new date by converting it to the millsecond
	newdate.setTime(newtimems); //Set the new computed date
	var actualday = newdate.getDate(); // Get the Actual Day (1-31) from the computed date
	var actualmonth = newdate.getMonth(); // Get the Actual Month (1-12) from the computed date
	actualmonth += 1;
	var actualyear = newdate.getYear(); // Get the Actual Year from the computed date
	if (actualyear < 2000 ) {actualyear += 1900;}
	return actualmonth + '/' + actualday + '/' + actualyear;
}

function getToday(){
	var now = new Date();
	return addDays(now,0);
}

function compareDates(x,y) {
	var xdate = new Date(x);
	var ydate = new Date(y);
	var xtime = xdate.getTime();
	var ytime = ydate.getTime();
	if ( xtime > ytime ){ return true ;}
	return false ;
}

function isDate(strDate) {
	var validcharacters = "0123456789/"
	var j;
	
	for ( var i=0; i< strDate.length; i++){
		j = "" + strDate.substring(i,i+1);
		if ( validcharacters.indexOf(j) == "-1" ){return false;}
}
	if (strDate.length > 10 || strDate.length < 8){return false;}
	var fs = strDate.indexOf("/")
	var ls = strDate.lastIndexOf("/")
	if (fs == -1 || ls == -1 || fs == ls){return false;} 
	var m = strDate.substring(0,fs)
	var d = strDate.substring(fs + 1,ls)
	var y = strDate.substring(ls + 1, strDate.length)
	if ( m < 1 || m > 12 ){return false;}
	if ( d < 1 || d > 31 ){return false;}
	if ( y < 1900 || y > 9999 ){return false;}
	if ((m == 2 || m == 4 || m == 6 || m == 9 || m == 11) && (d == 31)){return false;}
	if ( m == 2 ){
		var g=parseInt(y/4)
		if (isNaN(g)) { return false;}
		if (d > 29) { return false;}
		if (d == 29 && ((y/4)!=parseInt(y/4))) { return false;}
	}
	return true;
}
function isInteger(s){   	
	var i;
    if (isEmpty(s)) { return false;}
	for (i = 0; i < s.length; i++){   
       	var c = s.charAt(i);
       	if ((c == "-" || c == "+" ) && i == 0 ) continue;
       	if (!isDigit(c)) { return false;}
    }
    return true;
}

function isDigit(c){
	return ((c >= "0") && (c <= "9"));
}
function selectrow(obj,co)	{
	for (var i=0; i < (obj.options.length);i++)	{
	if (obj.options[i].value == co){obj.options[i].selected = true; break;}}
}

function concatenate(obj, type) {
	var list="";
	for (i = 0; i < obj.length; i++) {
	    if (obj[i].selected) {
			if (list == "")
				list = obj[i].value;
			else
				list = list + type + obj[i].value;
		}
	}
	return list;
}

function ltrim(x){
	if ( replaceString(x," ","") == "" ) return "";
	while ( x.indexOf(" ") == 0 ) { x = x.substring(1,x.length); }
	return x;
}

function rtrim(x){
	if ( replaceString(x," ","") == "" ) return "";
	while ( x.lastIndexOf(" ") == (x.length - 1)) {x = x.substring(0,x.length - 1);}
	return x;
}

function trim(x){
	return ltrim(rtrim(x));
}

function formatPhone(str) {
	str = trim(str);
	str = replaceString(str,"(","");
	str = replaceString(str,")","");
	str = replaceString(str," ","");
	str = replaceString(str, "-", "");
	str = replaceString(str, ".", "");
	return str;
}
function isEMail(str){
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}
	if (str.indexOf(" ")!=-1){return false;}
	return true;
}

var columndelimiter = "//*///*//";
var rowdelimiter = "//**//**//";
var tabledelimiter = "/***/***/";

function getcd() { return columndelimiter; }
function getrd() { return rowdelimiter; }
function gettd() { return tabledelimiter; }

function initAJAX() {
    try { xmlHttp = new XMLHttpRequest(); }
    catch (e) {
        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
            try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e) { alert("Your browser does not support AJAX!"); return false; }
        }
    }
}

function isTime(str) {
    // str must be in this format 23:59:59
    var aryTime = str.split(':');
    var strHour = parseInt(aryTime[0]);
    var strMinute = parseInt(aryTime[1]);
    var strSecond = parseInt(aryTime[2]);
    if (strHour < 24 && strMinute < 60 && strSecond < 60) return true;
    else return false;
}

function hasRole() {
    var url = 'ajax.asp?func=hasRole';
    xmlHttp.open('get', url, true);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.responseText == 0) {
                window.location.href = 'error.asp';
            }
        }
    }
}

function chkFocus(src, target) {
    if (isEmpty(src.value)) {
        var tgt = document.getElementById(target);
        if (tgt.selectedIndex == -1) {
            tgt.selectedIndex = 0;
        }
    }
}

function left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
function right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function chkPassword(pwd, strength) {
    var strong = new RegExp("^(?=.{10,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
    var medium = new RegExp("^(?=.{8,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    var weak = new RegExp("(?=.{6,}).*", "g");
    switch (strength) {
        case 'weak':
            if (!weak.test(pwd)) {
                alert('Your password must be at least 6 characters in length.');
                return false;
            }
            else { return true; }
            break;
        case 'medium':
            if (!medium.test(pwd)) {
                alert('Your password must be at least 8 characters in length and contain at least one number and one letter.');
                return false;
            }
            else { return true; }
            break;
        case 'strong':
            if (!strong.test(pwd)) {
                alert('Your password must be at least 10 characters in length and contain at least one number, one letter, and one other character.');
                return false;
            }
            else { return true; }
            break;
    }
}

function chgType(obj, objType) {
    var newObj = document.createElement('input');
    newObj.type = objType;
    if (obj.size) newObj.size = obj.size;
    if (obj.style.width) newObj.style.width = obj.style.width;
    if (obj.value) newObj.value = obj.value;
    if (obj.name) newObj.name = obj.name;
    if (obj.id) newObj.id = obj.id;
    if (obj.className) newObj.className = obj.className;
    obj.parentNode.replaceChild(newObj, obj);
    newObj.focus();
}


