/**
 *
 *
 *
 *
 */

// regular expressions

function validate(f) {
	// validate input fields
	success = true;
	inp = f.getElementsByTagName('input');
	pmsg = '';
	emsg = '';
	pcnt = 0;
	ecnt = 0;
	emails = new Array();
	phones = new Array();
	if (inp.length > 0) {	
		for (i=0; i < inp.length; i++) {
			if (document.getElementById('req_'+inp[i].id)) {
				val = trim_str(inp[i].value);
				nme = inp[i].id;
				if (nme.indexOf('phone') != -1 || nme.indexOf('mobile') != -1 || nme.indexOf('fax') != -1) {
					if (!checkPhone(val) && val != '') {
						pmsg = 'All phone numbers MUST be 10 digits long' + "\n\n";
						success = false;
						inp[i].style.border = "1px solid red";
					}
					phones[pcnt] = nme;
					pcnt++;
				} else if (nme.indexOf('email') != -1 && inp[i].type != 'checkbox' && inp[i].type != 'radio') {
					if (!checkMail(val)) {
						emsg += 'All email addesses must be in similar format to something@somewhere.com' + "\n\n";
						success = false;
						inp[i].style.border = "1px solid red";
					}
					emails[ecnt] = nme;
					ecnt++;
				} else {
					if (val == "") {
						success = false;
						inp[i].style.border = "1px solid red";
					}
				}
			}
		}
	}

	// validate phones and email addresses
	if (phones.length > 0){
		empty = true;
		for(i=0; i < phones.length; i++) {
			if (document.getElementById(phones[i]).value != '') {
				empty = false;
				break;
			}
		}
		if (empty === true){
			pmsg = 'You must enter at least one phone number' + "\n\n";
		} else {
			success = true;
		}
	}
	if (emails.length > 0){
		empty = true;
		for(i=0; i < emails.length; i++) {
			if (document.getElementById(emails[i]).value != '') {
				empty = false;
				break;
			}
		}
		if (empty){
			emsg = 'You must enter at least one email address' + "\n\n";
		} else {
			success = true;
		}
	}

	inp = '';
	// disable textarea
	inp = f.getElementsByTagName('textarea');
	if (inp.length > 0) {	
		for (i=0; i < inp.length; i++) {
			if (document.getElementById('req_'+inp[i].id)) {
				if (document.getElementById('req_'+inp[i].id).innerHTML == "*" && trim_str(inp[i].value) == "") {
					success = false;
					inp[i].style.border = "1px solid red";
				}
			}
		}
	}
	inp = '';
	// disable select
	inp = f.getElementsByTagName('select');
	if (inp.length > 0) {	
		for (i=0; i < inp.length; i++) {
			if (document.getElementById('req_'+inp[i].id)) {
				if (document.getElementById('req_'+inp[i].id).innerHTML == "*" && trim_str(inp[i].value) == "") {
					success = false;
					inp[i].style.border = "1px solid red";
				}
			}
		}
	}
	if (!success) {
		alert(pmsg + emsg + "Please enter all required fields");
	}
	return success;
}

function checkMail(eaddr){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(eaddr)) {
		return true;
	} else {
		return false;
	}
}

function checkPhone(num){
	var numfilter  = /^([0-9]{10})$/;
	if (numfilter.test(num)) {
		return true;
	} else {
		return false;
	}
}
	
	

/**
 * trim()
 * js function
 * trims leading and trailing whitespace
 */
function trim_str(strText) { 
	// this will get rid of leading spaces 
	while (strText.substring(0,1) == ' ' || strText.substring(0,1) == "\n" || strText.substring(0,1) == "\r") {
		strText = strText.substring(1, strText.length);
	}

	// this will get rid of trailing spaces 
	while (strText.substring(strText.length-1,strText.length) == ' ' || strText.substring(strText.length-1,strText.length) == "\n" || strText.substring(strText.length-1,strText.length) == "\r") {
		strText = strText.substring(0, strText.length-1);
	}

	return strText;
} 