function ConfirmValues(strValuePrimary, strValueSecondary){
    return (strValuePrimary == strValueSecondary);
}

// Validator Used on ChooseUser.asp
function Username_Validator(theForm) {
  if (theForm.username.value == "") {
    alert("Please enter a value for the \"User Name\" field.");
    theForm.username.focus();
    return (false);
  }
  if (theForm.username.value.length < 4) {
    alert("Please enter at least 4 characters in the \"User Name\" field.");
    theForm.username.focus();
    return (false);
  }
  if (theForm.username.value.length > 30) {
    alert("Please enter at most 30 characters in the \"User Name\" field.");
    theForm.username.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
  var checkStr = theForm.username.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) if (ch == checkOK.charAt(j)) break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  if (!allValid) {
    alert("Please enter only letter and digit characters in the \"User Name\" field.");
    theForm.username.focus();
    return (false);
  }
  if (theForm.password.value == "") {
    alert("Please enter a value for the \"Password\" field.");
    theForm.password.focus();
    return (false);
  }
  if (theForm.password.value.length < 6) {
    alert("Please enter at least 6 characters in the \"Password\" field.");
    theForm.password.focus();
    return (false);
  }
  if (theForm.password.value.length > 30) {
    alert("Please enter at most 30 characters in the \"Password\" field.");
    theForm.password.focus();
    return (false);
  }
  
  var checkStr = theForm.password.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) if (ch == checkOK.charAt(j)) break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  if (!allValid) {
    alert("Please enter only letter and digit characters in the \"Password\" field.");
    theForm.password.focus();
    return (false);
  }
  if (theForm.confirmpass.value == "") {
    alert("Please enter a value for the \"Confirm Password\" field.");
    theForm.confirmpass.focus();
    return (false);
  }
  if (theForm.confirmpass.value.length < 4) {
    alert("Please enter at least 4 characters in the \"Confirm Password\" field.");
    theForm.confirmpass.focus();
    return (false);
  }
  if (theForm.confirmpass.value.length > 30) {
    alert("Please enter at most 30 characters in the \"Confirm Password\" field.");
    theForm.confirmpass.focus();
    return (false);
  }
  if (theForm.confirmpass.value != theForm.password.value ) {
      alert("Please re-enter your password correctly.");
      theForm.confirmpass.focus();
      return (false);
  }
  
  var checkStr = theForm.confirmpass.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) if (ch == checkOK.charAt(j)) break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
  }
  if (!allValid) {
    alert("Please enter only letter and digit characters in the \"Confirm Password\" field.");
    theForm.confirmpass.focus();
    return (false);
  }
  return (true);
}

// Email Validator
function Validate_Email(theForm) {    
  if (theForm.email.value == "") {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }
  if (theForm.email.value.length < 6) {
    alert("Please enter at least 6 characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
  var checkStr = theForm.email.value;
  var allValid = true;
  var bDotPresent = false;
  var bRatePresent = false;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) if (ch == checkOK.charAt(j))	break;
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
    if (j == checkOK.length - 1  ) {
    	bDotPresent = true;
    }
    if (j == checkOK.length -2 ) {
    	bRatePresent = true;
    }
  }
  if (!allValid) {
    alert("Please enter only letter, digit and \"-_@.\" characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }
  if ((!bDotPresent) || (!bRatePresent) ) {
   	alert ("Please enter a valid email address");
   	theForm.email.focus();
   	return(false);
  }  
  
    if(!ConfirmValues(theForm.email.value, theForm.confirmEmail.value)){
        alert ("The email addresses you entered do not match. Please re-enter.");
        theForm.confirmEmail.focus();
        return (false);
    }
  
  return (true);
}

// Open PopUp Window
function openPopUp (winURL, winName, winFeatures) {
	newWindow = window.open(winURL,winName,winFeatures);
	newWindow.focus();
}

// New Gallery Window
function newGalleryWindow(galleryURL, galleryWinName, galleryFeatures) {
	window.open(galleryURL, galleryWinName, galleryFeatures);
}

// Display WIndow
function displayWindow (url, width, height) {
	var Win = window.open (url,"displayWindow",'width=' + width + ',height=' +
	height + ',resizable=0,scrollbars=no,menubar=no,status=yes');
	Win.focus();
}

// Form Validator
function validateIt(theForm) {
	if(theForm.categoryid.value != '') {
		theForm.submit();
	} else {
     	return false
   	}
}

// Change Password (I think)
function password() {
	self.name = "Parent_Window"; 
	window.open('/portal/user/send_pass.asp','EmailLoginInfo','toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=350,height=260');
}

// Button Over and Active states for IE

//startList = function() {
	//if (document.all&&document.getElementById) {
		// alert("function triggered");
		//inputArray = document.getElementsByTagName("Input");
		//for (i=0; i<inputArray.length; i++) {
			// alert("inside loop");
			//node = inputArray[i];
			//if (node.className=="button") {
				//node.onmouseover=function() {
					//this.className+=" over";
					// alert("if triggered");
				//}
				//node.onmousedown=function() {
					//this.className=this.className.replace(" over", " active");
				//}
				//node.onmouseout=function() {
					//this.className=this.className.replace(" over", "");
					//this.className=this.className.replace(" active", "");
				//}
			//}
		//}
	//}
//}
//window.onload=startList;

// Table row Over state for IE

function confirmCancelMembership() {
	var conf = confirm("Warning! You are about to cancel your auto renewal for your membership! Are you sure you want to do this?")
	if (conf)
		return true;
	else
		return false;
}

startList = function() {
	if (document.all&&document.getElementById) {
		//alert("function triggered");
		inputArray = document.getElementsByTagName("TR");
		for (i=0; i<inputArray.length; i++) {
			// alert("inside loop");
			node = inputArray[i];
			if (node.className=="Row") {
				node.onmouseover=function() {
					this.className+=" over";
					// alert("if triggered");
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
			if (node.className=="altRow") {
				node.onmouseover=function() {
					this.className+=" altOver";
					// alert("if triggered");
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" altOver", "");
				}
			}
		}
	}
}
window.onload=startList;
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element, className) {
	if (!hasClass(element, className)) {
		if (element.className) element.className += " " + className;
		else element.className = className;
	}
}

function removeClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	element.className = element.className.replace(regexp, "$2");
};

function hasClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	return regexp.test(element.className);
};
function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      if (!document.getElementById(id)) return false;
      var element = document.getElementById(id);
      element.focus();
    }
  }
}

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (element.type == "button") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.name+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        alert("The "+element.name+" field must be a valid email address.");
        return false;
      }
    }
  }
  return true;
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
    return false;
  } else {
    return true;
  }
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    if(thisform.id != "frmSignup"){
        resetFields(thisform);
        thisform.onsubmit = function() {
          return validateForm(this);
        }
    }
  }
}

addLoadEvent(focusLabels);
addLoadEvent(prepareForms);