var numimages = 20
var loginbtnoff = new Image()
var loginbtnon = new Image()
var report1off = new Image()
var report1on = new Image()
var report2off = new Image()
var report2on = new Image()
var report3off = new Image()
var report3on = new Image()
var report4off = new Image()
var report4on = new Image()
var report5off = new Image()
var report5on = new Image()
var backbtnoff = new Image()
var backbtnon = new Image()
var proceedbtnoff = new Image()
var proceedbtnon = new Image()
var searchbtnoff = new Image()
var searchbtnon = new Image()
var donebtnoff = new Image()
var donebtnon = new Image()
var delbtnoff = new Image()
var delbtnon = new Image()

loginbtnoff.src = "/vs/trichinae/images/login.gif"
loginbtnon.src = "/vs/trichinae/images/loginon.gif"
report1off.src = "/vs/trichinae/images/view.gif"
report1on.src = "/vs/trichinae/images/viewon.gif"
report2off.src = report1off.src
report2on.src = report1on.src
report3off.src = report1off.src
report3on.src = report1on.src
report4off.src = report1off.src
report4on.src = report1on.src
report5off.src = report1off.src
report5on.src = report1on.src
backbtnoff.src = "/vs/trichinae/images/back.gif"
backbtnon.src = "/vs/trichinae/images/backon.gif"
proceedbtnoff.src = "/vs/trichinae/images/proceed.gif"
proceedbtnon.src = "/vs/trichinae/images/proceedon.gif"
searchbtnoff.src = "/vs/trichinae/images/search.gif"
searchbtnon.src = "/vs/trichinae/images/searchon.gif"
donebtnoff.src = "/vs/trichinae/images/done.gif"
donebtnon.src = "/vs/trichinae/images/doneon.gif"
delbtnoff.src = "/vs/trichinae/images/del.gif"
delbtnon.src = "/vs/trichinae/images/delon.gif"



re = new RegExp("\\W+")	

function rollOn(imgName) {
	   if (document.images) {
    	   document[imgName].src = eval(imgName + "on.src");
        }
}

function rollOff(imgName) {
        if (document.images) {
            document[imgName].src = eval(imgName + "off.src");
        }
}

function formDoIt(formName) {
		if (document.forms) {
			if (genericValidateForm(document[formName])) {document[formName].submit()}
		}
}

function toggleAll(formName) {
	var toggleMode = -1;
    for (var i = 0; i<document[formName].elements.length; i++) {
        if ((document[formName].elements[i].type == 'checkbox')) {
            if (toggleMode == -1) {
				if (document[formName].elements[i].checked) {toggleMode = 0;}
				else {toggleMode = 1;}
			}
			if (toggleMode == 1) {document[formName].elements[i].checked = -1;}
			else {document[formName].elements[i].checked = 0;}
        }
    }
}

function delete4popUp(formName, by) {
	by = "\'" + by + "\'";
	var temp = new String(getCookie('userid'));
	if (temp==''||temp.toUpperCase()=='NULL') {
		temp=="UNKNOWN";
	}
	if (by.length > 0) {
		if (window.opener && window.opener.document[formName] && !window.opener.closed){
		    for (var i = 0; i<window.opener.document[formName].elements.length; i++) {
		        if ((window.opener.document[formName].elements[i].type == 'checkbox')) {
					window.opener.document[formName].elements[i].checked = 0;
				}
	        }
			if (confirm("The following supplier TIN will be removed from any future listings and reports for your USER ID of "+temp+":\n\n     * "+by+"\n\n\nAre you certain you want to proceed with deletion??\n     (Choose OK to PROCEED, or CANCEL to ABORT)\n")) {
				window.opener.document[formName].by.value = by;
				window.opener.document[formName].mode.value = "DELETE";
				window.opener.document[formName].submit();
				window.close();
			}
		}
    }
}

		
function deleteSel(formName) {
	var by = "";
	var niceby = "";
	var any = 0;
	var temp = new String(getCookie('userid'));
	if (temp==''||temp.toUpperCase()=='NULL') {
		temp=="UNKNOWN";
	}
    for (var i = 0; i<document[formName].elements.length; i++) {
        if ((document[formName].elements[i].type == 'checkbox')) {
			if (document[formName].elements[i].checked) {
				any = 1;
				if (by != "") {by = by + ","; niceby = niceby + "\n";}
				by = by + "\'" + document[formName].elements[i].value + "\'";
				niceby = niceby + "     * " + document[formName].elements[i].value;
			}
        }
    }
	if (any) {
		if (confirm("The following supplier TINs will be removed from any future listings and reports for your USER ID of "+temp+":\n\n"+niceby+"\n\nAre you certain you want to proceed with deletion??\n     (Choose OK to PROCEED, or CANCEL to ABORT)\n")) {
			document[formName].mode.value = "DELETE";
			document[formName].by.value = by;
			document[formName].submit();
		}
	}
	else {
		alert ('You must check at least one TIN number to delete!');
	}
}
			
function addSel(formName) {
	var temp = new String(getCookie('userid'));
	if (temp==''||temp.toUpperCase()=='NULL') {
		temp=="UNKNOWN";
	}
	var result = prompt("Please a valid supplier Tricinae Identification Number (TIN) to add to all future listings and reports for your USER ID of "+temp+":\n", "");
	if (result.length>0 && result.length<17){
		document[formName].mode.value = "ADD";
		document[formName].by.value = result;
		document[formName].submit();
	}
	else {
		alert ('Invalid Entry, try again.');
	}
}

function refreshSel(formName) {
	document[formName].mode.value = "VIEW";
	document[formName].by.value = "";
	document[formName].submit();
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function genericValidateForm(form) {
	var tempid = new String()
	var temppwd	= new String()

	for (i=0;i<form.length;i++) {
		var tempobj=form.elements[i];
		
		if ((tempobj.type=="text"||tempobj.type=="textarea"||tempobj.type=="password") && tempobj.value==''){
				alert('You cannot have a blank value for ' + tempobj.name + '.');
				return false;
		}
		if ((tempobj.type=="text"||tempobj.type=="password")&&tempobj.value.search(re) != -1) {
		 		alert("Your " + tempobj.name + " must only contain alphanumeric characters. (A-Z, 0-9)");
				return false;
		}

	}

	return true;

}

function printUserID() {
		var temp = new String(getCookie('userid'));
		if (temp==''||temp.toUpperCase()=='NULL') {
			document.write('UNKNOWN');
		}
		else {
			document.write(temp.toUpperCase());
		}

		return null;
}

function popUp(by) {
	by = replace(by, '*', '&');
	mywindow = window.open(by, "popUp", "width=620,height=460,resizable=1,dependent=1");
}

function getCookieVal (offset) {  
        var endstr = document.cookie.indexOf (";", offset);  
        if (endstr == -1)    
                endstr = document.cookie.length;  
                return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {  
        var arg = name + "=";  
        var alen = arg.length;  
        var clen = document.cookie.length;  
        var i = 0;  
        while (i < clen) {    
        var j = i + alen;    
        if (document.cookie.substring(i, j) == arg)      
                return getCookieVal (j);    
                i = document.cookie.indexOf(" ", i) + 1;    
                if (i == 0) break;   
        }  
        return null;
}

function setCookie (name, value) {  
        var argv = SetCookie.arguments;  
        var argc = SetCookie.arguments.length;  
        var expires = (argc > 2) ? argv[2] : null;  
        var path = (argc > 3) ? argv[3] : null;  
        var domain = (argc > 4) ? argv[4] : null;  
        var secure = (argc > 5) ? argv[5] : false;  
        document.cookie = name + "=" + escape (value) + 
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
        ((path == null) ? "" : ("; path=" + path)) +  
        ((domain == null) ? "" : ("; domain=" + domain)) +    
        ((secure == true) ? "; secure" : "");
}

function onTop() {
	if (top.location != location)
		top.location.href = location.href;
}

function isNN4() {
	if (navigator.appVersion.charAt(0) >= "4" && navigator.appName == "Netscape") return true;
	else return false;
	return false;
}

function isIE4() {
	if (navigator.appVersion.charAt(0) >= "4" && navigator.appName == "Microsoft Internet Explorer") return true;
	else return false;
	return false;
}

function goodBrowser() {
	if (isNN4()) {
//		alert("Netscape OK"); 
		return true;
	}
	else if (isIE4()) {
//		alert("IE OK");
		return true;
	}
	else {
		alert("The Trichinae Herd Certification Page requires Internet Explorer 4.0 or Netscape 4.0 or higher.  Please upgrade your browser before proceeding.  Returning you to the VS Home Page...");
		location = "http://www.aphis.usda.gov/vs";
		return false;
	}
	return false;
}
