/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function validateEmail() {
if(!isGoodEmail(document.frmMail.Email.value, false)){
      alert("Please make sure that you\ninput a valid e-mail address");
      document.frmMail.Email.focus();
      return false
    }
    return true;
}

function isGoodEmail(emailStr, thankyouflag) {
if (thankyouflag == null) thankyouflag=true;
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\!\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var firstChars=validChars
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom="(" + firstChars + validChars + "*" + ")"
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid
if (user.match(userPat)==null) {
   return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
  for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
     return false
    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   return false
}
if (domArr[domArr.length-1].length==3 && len<2) {
   return false
}

// default behavior - keep existing uses continue to work
if(thankyouflag) {
// Everything's valid:
   //window.open('Thank You', 'newFormWindow', 'width=340,height=260');
}
return true;
}


function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '' || formElement.value == '-1') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function isEmpty_Add(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value.length < 6) {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function checkWholesaleInfo()
{
	with (window.document.frmMail) {
				if(isEmpty(FirstName, 'Enter First Name')) {
					return false;
				} else if(isEmpty(LastName, 'Enter Last Name')) {
					return false;
				}  else if(isEmpty(CompanyName, 'Enter Company Name')) {
					return false;
				} else if (isEmpty(StoreName, 'Enter Store Name')) {
					return false;
				} else if (isEmpty(Phone, 'Enter Phone')) {
					return false;
				} else if (!validateEmail()) {
					return false;
				} else if (isEmpty(City, 'Enter City')) {
					return false;
				} else if (isEmpty(State, 'Select State')) {
					return false;
				} else if (isEmpty(HowTeezHer, 'Select How did you hear about Teez-Her')) {
					return false;
				} else if (isEmpty_Add(Message, 'Enter Message/Comments')) {
					return false;
				} else {
					return true;
				}
				
	}
}

