BR_DOM  = (document.getElementById) ? true : false;
BR_NS4  = (document.layers) ? true : false;
BR_IE   = (document.all) ? true : false;
BR_IE4  =  BR_IE && !BR_DOM;
BR_Mac  = (navigator.appVersion.indexOf("Mac") != -1);
BR_IE4M =  BR_IE4 && BR_Mac;
BR_NS6  =  BR_DOM && !BR_IE;

// Change the background color of a row as an item is marked/unmarked
function Highlight(whichRow) {
	var r = null;
	if (whichRow.parentNode && whichRow.parentNode.parentNode) {
		r = whichRow.parentNode.parentNode;
	}
	else if (whichRow.parentElement && whichRow.parentElement.parentElement) {
		r = whichRow.parentElement.parentElement;
	}
	if (r) {
		if (r.className == "unselectedRow") {
			r.className = "selectedRow";
		}
	}
}

function Unhighlight(whichRow) {
	var r = null;
	if (whichRow.parentNode && whichRow.parentNode.parentNode) {
		r = whichRow.parentNode.parentNode;
	}
	else if (whichRow.parentElement && whichRow.parentElement.parentElement) {
		r = whichRow.parentElement.parentElement;
	}
	if (r) {
		if (r.className == "selectedRow") {
			r.className = "unselectedRow";
	    }
	}
	if (document.forms[0].markall) {
	  document.forms[0].markall.checked=false;
	}
	if (document.forms[0].assignall) {
	  document.forms[0].assignall.checked=false;
	}
}

function Toggle(whichRow) {
	if (whichRow.checked) {
		Highlight(whichRow);
		if (document.forms[0].markall) {
		  document.forms[0].markall.checked = AllChecked();
		}
		if (document.forms[0].assignall) {
		  document.forms[0].assignall.checked = AllChecked();
		}
	}
	else {
		Unhighlight(whichRow);
		if (document.forms[0].markall) {
		  document.forms[0].markall.checked = false;
		}
		if (document.forms[0].assignall) {
		  document.forms[0].assignall.checked = false;
		}
	}
}

// Toggle all checkboxes from marked to clear
function ToggleAll(whichRow) {
	if (whichRow.checked) {
		checkAll();
	}
	else {
		uncheckAll();
	}
}

//All Checked?
function AllChecked() {
	var checkThese = document.forms[0].select.length;

    if (document.forms[0].select.length == "undefined") {
	  if ((document.forms[0].select.disabled == false)
	     && (document.forms[0].select.checked == false)) {
	       return false;
	  }
    }

	for (var i = 0; i < checkThese; i++) {
	  if ((document.forms[0].select[i].disabled == false)
	     && (document.forms[0].select[i].checked == false)) {
	       return false;
	  }
	}
	return true;
}

function checkAll() {
	var checkThese = document.forms[0].select.length;

	if (checkThese > 200) {
	   alert("Please wait this may take a minute");
	}

    if (document.forms[0].select.length == "undefined") {
	    if (document.forms[0].select.disabled == false) {
	      document.forms[0].select.checked = true;
	      document.forms[0].modified.value = 2;
	      var thisRow = "row0";
	      thisRow=eval(thisRow);
	      thisRow.className = "selectedRow";
	   }
	   checkThese = 0;
    }

	for (var i = 0; i < checkThese; i++)
	{
	  if (document.forms[0].select[i].disabled == false) {
	    document.forms[0].select[i].checked = true;
	    document.forms[0].modified[i].value = 2;
	    var thisRow = "row" + i;
	    thisRow=eval(thisRow);
	    thisRow.className = "selectedRow";
	  }
	}

	if (document.forms[0].markall) {
	  document.forms[0].markall.checked = true;
	}

	if (document.forms[0].assignall) {
	  document.forms[0].assignall.checked = true;
	}
}

//goes with ToggleAll
function uncheckAll() {
	var checkThese = document.forms[0].select.length;

	if (checkThese > 200) {
	   alert("Please wait this may take a minute");
	}

    if (document.forms[0].select.length == "undefined") {
	  document.forms[0].select[i].checked = false;
	  document.forms[0].modified[i].value = 1;
	  var thisRow = "row" + (i);
	  thisRow=eval(thisRow);
	  thisRow.className = "unselectedRow";
	  checkThese = 0;
    }

	for (var i = 0; i < checkThese; i++)
	{
	  document.forms[0].select[i].checked = false;
	  document.forms[0].modified[i].value = 1;
	  var thisRow = "row" + (i);
	  thisRow=eval(thisRow);
	  thisRow.className = "unselectedRow";
	}

	if (document.forms[0].markall) {
	  document.forms[0].markall.checked = false;
	}
	if (document.forms[0].assignall) {
	  document.forms[0].assignall.checked = false;
	}
}

function img_act (tag) {
	document [tag].src = eval(tag + "h.src");
}

function img_inact (tag) {
    document [tag].src = eval(tag + ".src");
}

function submitlink( selectedtype )
{
  document.thisForm.linktype.value = selectedtype;
  document.thisForm.submit();
}

function submitlinkid(selectedtype,selectedid)
{
  document.thisForm.linktype.value = selectedtype;
  document.thisForm.linkid.value = selectedid;
  document.thisForm.submit();
}

function submitlink_after_confirmation(selectedtype, message)
{
  if (!confirm(message))
    return false;

  document.thisForm.linktype.value = selectedtype;
  document.thisForm.submit();

}

function Retry_onclick() {
  window.history.back(2);
}

function newWindow(url) {
  var loc = url;
  remote = window.open('','galeFooterWindow');
  remote.location.href = url;
  if (remote.opener == null) 
    remote.opener = window;
  else
    remote.focus();
}

function helpWindow(url) {
  var loc = url;
  remote = window.open('','helpWindow', 'alwaysRaised=yes,width=750,height=550,screenX=50,screenY=50,top=50,left=50,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,status=no');
  remote.location.href = url;
  if (remote.opener == null) 
    remote.opener = window;
  else
    remote.focus();
}

function makeRemote(url) {
  var loc = url;
  remote = window.open('','eBookSelectionWindow', 'alwaysRaised=yes,width=750,height=550,screenX=50,screenY=50,top=50,left=50,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,status=no');
  remote.location.href = url;
  if (remote.opener == null) 
    remote.opener = window;
  else
    remote.focus();
}
function checkPos(pos, max) 
{
	if (pos.search(/[^0-9]/) != -1)
	{
       alert("Enter a valid starting position");
       return false;
	}

    if ((Number(pos) < 1) || (Number(pos) > Number(max))) {
       alert("Enter a valid starting position");
       return false;
    }
    return true;
}

function selection_onchange(i)
{
	var f = document.forms[0];
    if (f.select.length > 0) {
	  if (f.select[i].checked == true) {
	    f.modified[i].value = 2;
	  } else {
	    f.modified[i].value = 1;
	  }
	} else {
	  if (f.select.checked == true) {
	    f.modified.value = 2;
	  } else {
	    f.modified.value = 1;
	  }
	}
	return true;
}

function checkDateString(errStr, dateStr, daysValid) 
// Function to check a string for MM/DD/YYYY format
// Note that this function does not check if any alternative
// date formats have been entered (e.g., September 1, 1999)
{
    var errText = usrInputMsg +errStr
    var errInit = usrInputMsg
    
    var inputDate = new String
    inputDate = dateStr
    
    var len = inputDate.length - 1

    if ((inputDate == "None") || (inputDate == "") || (inputDate.length != 10)) {
        errText += errorMsg[0];
    }
    else {
		for (i = len; i > 0; i--) { 
		//May need to change this for the cases where months and days are single digits;
		//this assumes 2-digit numbers are input.
			if ((i == 2) || (i == 5)) {
				if ((inputDate.charAt(i) != "/") && (inputDate.charAt(i) != "-")) {
					errText += errorMsg[0];
					break;  
				}
			}
			else {
				if (isNaN(inputDate.charAt(i))) {
					errText += errorMsg[1];
					break;
				}
			}
		}
		var mon = new Number
		mon = Number(inputDate.substring(0,2))
		var day = new Number
		day = Number(inputDate.substring(3,5))
		var year = new Number
		year = Number(inputDate.substring(6,10))
    
		if (mon > 12) {
			errText += errorMsg[3];
		}

		if ((year < 1999) || (year > 2999)) {
			errText += errorMsg[2];
		}

		if (day > 31) {
			errText += errorMsg[4];
		}
		if (mon == 2) {
			if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
			// if the year divides evenly by 4 and 400 but not 100, it is a leap year
				if (day > 29) {
					errText += errorMsg[5];
				}
			}
			else {
				if (day > 28) {
					errText += errorMsg[6];
				}
			}
		}
		if ((mon == 4) || (mon == 6) || (mon == 9) || (mon == 11)) {
			if (day > 30) {
				errText += errorMsg[7];
			}
		}
    
		var today = Date.parse(Date())
		var dtDays = Date.parse(inputDate)
	
		if (daysValid) {
		// Check that the date entered is less than the valid days entered (optional)
			var okInterval = (daysValid * 24 * 60 * 60 * 1000) // convert days to milliseconds
			 
			if (dtDays > (today + okInterval)) {
				errText += errorMsg[8] +daysValid +".\n";
			}
		}
		
		if (dtDays <= today){
		errText += errorMsg[9];
		}
	}
	if (errText != errInit) {
		alert(errText);
		return false;
	}
	else {
		return true;
	}
}

function checkPastDate(inputDate) {

    // Check date entered not earlier than today (?)
    // Need to figure out when today begins and yesterday ended

    var tNow = new Date()
    var tMon = new Number
    var tDay = new Number
    var tYear = new Number

    tDay = tNow.getDate() - 1   // Function returns 1 thru 31; we want yesterday
    tMon = tNow.getMonth() + 1	// Function returns 0 thru 11
    tYear = tNow.getFullYear()  // Function returns local 4-digit year

    // Apr, Jun, Sep, Nov
    if ((tMon == 4) || (tMon == 6) || (tMon == 9) || (tMon == 11)) {

        if (tDay == 0) {
            tDay = 31
            tMon -= 1
        }
    }
    // Jan, Feb, Mar, May, July, Aug, Oct, Dec
    else { 

        if (tDay == 0) {
            tMon -= 1
            if (tMon == 2) { // Now we are in February
                if (leapYrTest == 0) {
                    tDay = 29
                }
                else {
                    tDay = 28
                }
            }
            else {
                if (tMon == 0) { // Now we are in December
                    tDay = 31
                    tMon = 12
                    tYear = year-1
                }
                else {
                    tDay = 30
                }
            }
        }
    }

    var yesterday = new String
    yesterday = String(tMon) +"/" +String(tDay) +"/" +String(tYear)
    var dtYes = Date.parse(yesterday)
    var dtDays = Date.parse(inputDate)

    if (dtDays <= dtYes) {
	alert(usrInputMsg +errorMsg[9]);
	return true;
    }
}

// VALIDATION HELPER FUNCTION: loops through a string testing
// each character for correct date characters ( 1-9 and '/' );
// returns true if any char is 'Not a Date Character'
function NotDateChar(str)
{
	for (var i = 0; i < str.length; i++)
	{
        var ch = str.substring(i, i + 1);
        if ((ch < "0" || "9" < ch) && ch != '/')
        {       return true;    }
	}
	return false;        
} //end function

function checkIfNum(x) 
// Function to check a specific string for numeric value
{
    // if they’ve left it blank
    if (x == "") {
		alert(usrInputMsg +errorMsg[12]);
		return false;
    }
    // get the length of the string passed
    // check each letter to see if it will convert to an integer
    len = x.length
    for (i = 0; i < len; i++) {
        str = x.substring(i,i+1)
        if (str != "0") {
            if (!parseInt(str)) {
            // if not, alert the user and return 
				alert(usrInputMsg +errorMsg[13]);
				return false;
            }
		}
    }
    return true;
}

function checkPassword(txt, type)
// Function to check string as a valid password  
{
    var pw = String(txt)
    var pos = 0

	if (pw.length < 1) {
		alert(usrInputMsg +type +" Password:\n" +errorMsg[14]);
		return false;
	}
	if (pw.length > 22) {
		alert(usrInputMsg +type +" Password:\n" +errorMsg[14]);
		return false;
	}
	/* Comment this piece out unless initial number restriction is enforced on Passwords
	if ( (pw.charAt(0) == "0") || (parseInt(pw.charAt(0))) ) {
		alert(usrInputMsg +type +" Password:\n" +errorMsg[16]);
		return false;
	}
	*/
	for (i = 0; i < pw.length; i++) {
		if ((pw.charCodeAt(i) < 48) ||
			((pw.charCodeAt(i) > 57) && (pw.charCodeAt(i) < 65)) ||
			((pw.charCodeAt(i) > 90) && (pw.charCodeAt(i) < 97)) ||
			(pw.charCodeAt(i) > 122)) {
			if (pw.charCodeAt(i) != 95) {
				pos = i+1;
				alert(usrInputMsg +type +" Password:\n" +errorMsg[17] +pos +errorMsg[18]);
				return false;
			}
		}
	}
	return true;
}

function checkIdRules(errors, txt)
{
    var str = String(txt)
    var pos = 0
    var errText = usrInputMsg +errors
    var errInit = usrInputMsg

    if ((str != "") && (str.charCodeAt(0) == 95) ) {
		errText += errorMsg[10];
    } 
    //str = str.toLowerCase()
    for (i = 0; i < str.length; i++) {

		if ((str.charCodeAt(i) < 48) ||
			((str.charCodeAt(i) > 57) && (str.charCodeAt(i) < 97)) ||
			(str.charCodeAt(i) > 122)) {
			if (str.charCodeAt(i) != 95) {
				pos = i+1;
				errText += errorMsg[11];
				break;
			}
		}
	}
	if (errText != errInit) {
		alert(errText);
		return false;
	}
	else {
		return true;
	}
}

function checkEmailAddress(email)
{
    var str = String(email)
    var atOk = 0
    var errors = ""
    var warning = ""
    
    str = str.toLowerCase()
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) == "@") {
			atOk = 1;
			continue;
		}
		if ((str.charAt(i) == ".") || (str.charAt(i) == "_")) {
			continue;
		}
		if ((str.charCodeAt(i) < 48) ||
			((str.charCodeAt(i) > 57) && (str.charCodeAt(i) < 97)) ||
			(str.charCodeAt(i) > 122)) {
				errors = "- Email address may have illegal character(s).\n";
		}
    }
	if (!atOk) {
		errors += "- Email address must contain @ and domain name of addressee.\n";
	}
	if (errors != "") {
		warning = "WARNING: E-mail address looks a little odd.\n\n" +errors +"\nAre you sure you wish to continue?";
		if (!confirm(warning))
			return false;
	} 
	return true;
}

function submitThisForm(errors)
{
    if (errors == "") 
		return true;
	var errText = usrInputMsg +errors
	alert(errText);
	return false;
} 

function onError(focus, message)
{
    alert(usrInputMsg+message);
    focus.focus();
    thisPage.cancelEvent = true;
}

function askAreYouSure()
{
    if (!confirm("WARNING: These changes will be applied to ALL locations within this Institution. \nOnce submitted, these changes are not undo-able, unless you submit further changes, \neither on an Institution-wide or individual Location level.  \n\nAre you sure you wish to continue?"))
		return false;
    else
        return true;
}

function resetForm(docForm)
{
	docForm.reset();
	return;
}

// Window opening functions; not currently in use
function openLocWindow(strSrchLibId,scriptName)
{
    var url = "Location.asp?libid=" +strSrchLibId  
    window.open(url,"Location")
}

function editLocWindow(strSrchLibId,locid,act,scriptName)
{
    var url = "Location.asp?libid=" +strSrchLibId +"&locid=" +locid +"&act=" +act 
    window.open(url,"Location")
}

function openIPWindow(strSrchLibId,ipid,act)
{
    var url = "IPAddr.asp?libid=" +strSrchLibId +"&ipid=" +ipid +"&act=" +act
    window.open(url,"IPAddr")
}

function openIPAllWindow(strSrchLibId)
{
    var url = "IPAddrAll.asp?libid=" +strSrchLibId
    var features
    features = "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=850,height=550"
    window.open(url,"IPAddrAll",features,1)
}

function openDBWindow(strSrchLibId)
{
    var url = "CustDB.asp?libid=" +strSrchLibId
    window.open(url,"CustDB")
}

function openDBAllWindow(strSrchLibId)
{
    var url = "CustDBAll.asp?libid=" +strSrchLibId
    var features
    features = "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=850,height=550"
    window.open(url,"CustDBAll",features,1)
}

function openURWindow(strSrchLibId)
{
    var url = "UsgRep.asp?libid=" +strSrchLibId
    window.open(url,"UsgRep")
}

// Above functions to open separate windows will not be used in V1.1
// Below functions which utilizes a single window named "RLISWeb" for all pages will be implemented.
function doGoBack()
{
	var urlWidth //= window.parent.width
	var urlHeight //= window.parent.height
	var features
	var lastPage
		
	lastPage = document.thisForm.lastPage.value

	features = "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1"
	//,width=" +urlWidth +",height=" +urlHeight
	if (document.thisForm.doRefresh.value == 1) {
		window.open(lastPage,"RLISWeb",features);
	}
	else {
		//var brwser
		//brwser = navigator.appName
		//if (brwser == "Netscape") {
			window.history.go(lastPage);
		//} else {
			//window.history.back(2);
		//}
	}
}

function checkGoBack()
{
	if (document.thisForm.goBack.value == 1) {
		doGoBack();
	}
	return;
}
