// Text Area Limitation
var submitcount=0;
function checkSubmit() {

      if (submitcount == 0)
      {
      submitcount++;
      document.Surv.submit();
      }
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
}



//Popup Function
function popup(x){
	if (x=="markers")
		window.open("products/markers.jsp", "markers", "width=600, height=330, status=no, resizable=no");
	else if (x=="magnets")
		window.open("products/magnets.jsp", "roles", "width=600, height=260, status=no, resizable=no");
	else if (x=="stickies")
		window.open("products/stickies.jsp", "stickies", "width=600, height=230, status=no, resizable=no");
	else if (x=="threehole")
		window.open("products/threehole.jsp", "threehole", "width=600, height=180, status=no, resizable=no");
	else if (x=="note")
		window.open("notesonnote.jsp", "notes", "width=300, height=150, status=no, resizable=no");
	else
		alert("Error: Cannot find the product, " + x);
}


function validateContactUs(oForm)
{
  	if (oForm.SenderName.value == ''){
  		alert("Please type in your name.");
  		return false;
  	}
  	if (oForm.SenderEmail.value == ''){
  		alert("Please type in your email.");
  		return false;
  	}
  	if (oForm.SenderQuestion.value == ''){
  		alert("Please type in your question.");
  		return false;
  	}
  	if (!validateEmail(oForm.SenderEmail.value)){
  		return false;
  	}
  	return true;
	
}

function validateEmail(emailStr) {
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	
	var checkTLD=1;
	
	//check ending	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	// checking the format user@domain format.
	var emailPat=/^(.+)@(.+)$/;
	
	/* Checking special char: ( ) < > @ , ; : \ " . [ ] */
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";	
	var validChars="\[^\\s" + specialChars + "\]";
	
	// Checing quotes."
	var quotedUser="(\"[^\"]*\")";
	
	/* Check if IP is domain IP addresses, */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
	/* Too many/few @'s or something;*/
	
	alert("Please check the email field.");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
	
	// See if "user" is valid
	if (user.match(userPat)==null) {
		alert("The username doesn't seem to be valid.");
		return false;
	}
	
	/* Verify IP address. */
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {	
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			   }
		}
		return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid.");
			return false;
		}
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}



function validateNameForm(oForm)
{
	//Check whitespaces
	remove_XS_whitespace(oForm.FirstName);
	remove_XS_whitespace(oForm.LastName);
	remove_XS_whitespace(oForm.Email);

  	if (oForm.FirstName.value == ''){
  		alert("Please type in your first name.");
  		return false;
  	}
  	if (oForm.LastName.value == ''){
  		alert("Please type in your last name.");
  		return false;
  	}
  	if (oForm.Email.value == ''){
  		alert("Please type in your email.");
  		return false;
  	}  
  	if (!validateEmail(oForm.Email.value)){
  		return false;
  	}
  	return true;
}


function validateOrderForm(oForm)
{
	
	//Check whitespaces
	remove_XS_whitespace(oForm.ProductQty);
	remove_XS_whitespace(oForm.MarkerQty);
	remove_XS_whitespace(oForm.MagnetQty);
	remove_XS_whitespace(oForm.ThreeHoleQty);
	remove_XS_whitespace(oForm.StickyQty);
	
	//Check blank. if "" then put 0.
	validateBlanktoZero(oForm.ProductQty);
	validateBlanktoZero(oForm.MarkerQty);
	validateBlanktoZero(oForm.MagnetQty);
	validateBlanktoZero(oForm.ThreeHoleQty);
	validateBlanktoZero(oForm.StickyQty);
	
	//Check Numeric
	if (	validateNumeric(oForm.ProductQty.value) &&
		validateNumeric(oForm.MarkerQty.value) &&
		validateNumeric(oForm.MagnetQty.value) &&
		validateNumeric(oForm.ThreeHoleQty.value)&&
		validateNumeric(oForm.StickyQty.value)){
		
		//Maximum Eraseboards
		if ((oForm.ProductLine.value == "Eraseboards") && (51 <= parseInt(oForm.ProductQty.value))){
			alert("You cannot purchase more than 50 Eraseboards using this form.");
			return false;
		
		}
		//Maximum Erasesheets
		if ((oForm.ProductLine.value == "Erasesheets") && (101 <= parseInt(oForm.ProductQty.value))){
			alert("You cannot purchase more than 100 Erasesheets using this form.");
			return false;
		
		
		}
		//Maximum Markers
		if (41 <= parseInt(oForm.MarkerQty.value)){
			alert("You cannot purchase more than 40 marker packs using this form.");
			return false;
		}
		
		//Maximum Magnets
		if (101 <= parseInt(oForm.MagnetQty.value)){
			alert("You cannot purchase more than 100 magnet packs using this form.");
			return false;
		}
		
		//Maximum Stickies
		if (101 <= parseInt(oForm.StickyQty.value)){
			alert("You cannot purchase more than 100 sticky packs using this form.");
			return false;
		}
		
		//Maximum ThreeHoles
		if (parseInt(oForm.ProductQty.value) < parseInt(oForm.ThreeHoleQty.value)){
			alert("You cannot order more 3-hole punches than the boards you ordered.");
			return false;
		}
		
		
		//var iTotal;
		//iTotal = parseInt(oForm.ProductQty.value) + parseInt(oForm.MarkerQty.value) + parseInt(oForm.MagnetQty.value) + parseInt(oForm.ThreeHoleQty.value) + parseInt(oForm.StickyQty.value);

		if ((parseInt(oForm.ProductQty.value) < 1)
			||(parseInt(oForm.MarkerQty.value) < 0)
			||(parseInt(oForm.MagnetQty.value) < 0)
			||(parseInt(oForm.ThreeHoleQty.value) < 0)
			||(parseInt(oForm.StickyQty.value) < 0)){
			
			alert("Please enter a valid quantity.  You must at least buy one board.");
			return false;
		}
	}else{
		return false;
	}

	return true;
}

function validateBlanktoZero(item)
{	
  if (item.value == ''){item.value = '0'};

}

function remove_XS_whitespace(item)
{
  item.value = item.value.replace(/\r/g, " ");

  //There are regex switchs for printing /nonprinting characters 
  //but they stripped some characters either needed or left some not wanted,
  //thats why the following replacement exists.

  item.value = item.value.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");
  item.value = item.value.replace(/'/g, "");
 
  item.value = item.value.replace(/ +/g, " ");  
  item.value = item.value.replace(/^\s/g, "");
  item.value = item.value.replace(/\s$/g, "");	

  if (item.value == ' '){item.value = ''};

}

function validateNumeric(field) {
	var valid = "0123456789-";
	
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			alert("Invalid characters in the quantities.  Please try again.");
			return false;
		}
		
	}
	return true;
}
