/***********************************************************************************************************
*		VALIDATE CONTACT INFORMATION
***********************************************************************************************************/

var firstBadEl = null;
var editErrorMsgs = Array();

function checkForm() {
	firstBadEl = null;
	editErrorMsgs = Array();
	
	var fm = document.forms['wholesaleForm'];
	if (fm.name.value == '') badField(fm.name, 'Name is required');
	if (fm.email.value == '') badField(fm.email, 'Email address is required');
	if (fm.code.value == '') badField(fm.code, 'Anti-SPAM characters required');

	if (fm.email.value && !validMail(fm.email.value)) badField(fm.email, 'Email address does not appear to be valid');
	
	if (fm.phone.value && !validPhone(fm.phone.value)) badField(fm.phone, 'Phone number should be of form NNN-XXX-XXXX');

	//if (fm.zip.value && !validZIP(fm.zip.value)) badField(fm.zip, 'ZIP code does not appear to be valid');

	if (this.firstBadEl) {
		var errorMsg = 'Please correct the following:\n'
									+	this.editErrorMsgs.join('\n');
		alert(errorMsg);
		this.firstBadEl.focus();
		return false;
	}
	
	return true;
}

// Handle field validation failure
function badField(el, msg) {
	this.editErrorMsgs.push('- '+msg);
	if (!this.firstBadEl) this.firstBadEl = el;
}

/**
 * Check to see if a string is a valid email address
 *
 * @param		string	s						String to check
 *
 * @return	bool		valid				True if valid email address, false otherwise
 */
function validMail(s) {
	var a = false;
	var res = false;
	if ( typeof( RegExp ) == 'function') {
		var b = new RegExp( 'abc' );
		if ( b.test( 'abc' ) == true ) {
			a = true;
		}
	}
	if ( a == true ) {
		reg = new RegExp( '^([a-zA-Z0-9\\-\\.\\_]+)' +
						 '(\\@)([a-zA-Z0-9\\-\\.ÄäÜüÖö]{2,255})' +
						 '(\\.)([a-zA-Z]{2,6})$' );
		res = ( reg.test( s ) );
	}
	else {
		res = ( s.search( '@' ) >= 1 &&
		s.lastIndexOf( '.' ) > s.search( '@' ) &&
		s.lastIndexOf( '.' ) >= s.length - 5 )
	}
	return( res );
}

/**
 * Check to see if a string is a valid U.S. ZIP code
 *
 * @param		string	s						String to check
 *
 * @return	bool		valid				True if valid ZIP code, false otherwise
 */
function validZIP(s) {
	var valid = "0123456789-";
	var hyphencount = 0;
	
	if (s.length != 5 && s.length != 10) {
		return false;
	}
	for (var i=0; i < s.length; i++) {
		temp = "" + s.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			return false;
		}
		if ((hyphencount > 1) || ((s.length==10) && ""+s.charAt(5)!="-")) {
			return false;
		}
	}
	return true;
}

/**
 * Check to see if a string is a valid North American phone number
 *
 * @param		string	phonenumber			String to check
 *
 * @return	bool		valid						True if valid phone number, false otherwise
 */
function validPhone(phonenumber) {
	if (phonenumber.match(/^[ ]*[+]{0,1}[ ]*[1]{0,1}[-|.]{0,1}[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-|.]{0,1}[ ]*[0-9]{3,3}[ ]*[-|.]{0,1}[ ]*[0-9]{4,4}[ ]*$/)!=null)
		return true;
	return false;
}

