//Adaptive Image - javascript form validation support functions - The flying marketer */
//###############all functions should use regular expressions####################

/* mailing list form handler */
function su_form_check(frm) {
		strError = "";
		if(!ValidEmail(trim(frm.ea.value))) {
			strError = "Please enter a valid email address.\n";
			alert(strError);
			frm.ea.focus;
			return false;
			}
		else {
			frm.ea.value = trim(frm.ea.value); //trim data
			return true;
		}
		return false;
	}

function clear_box(box) { //clear a text box (from onfocus event)
	 	 if(box.value==box.defaultValue) {
	 	 	 box.value = "";
		 } 	 
	}
	
//validate email
function ValidEmail(email) {
var invalidChars = ' /:,;';
	
	if (email == '') { return false; }
	if (email.length  > 50) { return false;	}
	for (i=0; i<invalidChars.length; i++) {	//scan through and reject matching invalid chars
		if (email.indexOf(invalidChars.charAt(i),0) > -1) {	return false; }	//bad char in email string
	}
	atPos = email.indexOf('@',1);	//check for @ from second char onward
	if (atPos == -1) { return false; } // no @ found
	if (email.indexOf('@',atPos+1) > -1) { return false; }	//check for @ from next char onward - another @ found
	periodPos = email.indexOf('.',atPos)	//check for . from next char onward
	if (periodPos == -1) { return false; }	//no . found
	if (periodPos+3 > email.length)	{ return false; }	//check for at least 2 chars after .
	
	return true;
}

//phone number check
function IsPhoneNum(num) {
var validChars = '0123456789()+ ';
	if (num == '') return false;
	for (i=0; i<num.length; i++) {	//scan through and reject nonmatching valid chars
		if (validChars.indexOf(num.charAt(i),0) < 0) return false;	//bad char in number string
	}
	return true;
}

//is number check
function IsNum(num) {
var validChars = '0123456789.';
var dpcount = 0;
	if (num == '') return false;
	for (i=0; i<num.length; i++) {	//scan through and reject nonmatching valid chars
		if (validChars.indexOf(num.charAt(i),0) < 0) return false;	//bad char in number string
		if (num.charAt(i)=='.') dpcount++; //tally decimal points
		if (dpcount > 1) return false; //invalid number
	}
	return true;
}

//is integer number check
function IsInt(num) {
var validChars = '0123456789';
var dpcount = 0;
	if (num == '') return false;
	for (i=0; i<num.length; i++) {	//scan through and reject nonmatching valid chars
		if (validChars.indexOf(num.charAt(i),0) < 0) return false;	//bad char in number string
	}
	return true;
}

//password check
function ValidPword(pword) {
//##
	var reg = new RegExp("^[a-zA-Z0-9]{8,16}$");
	return(reg.test(pword));
}

//password confirmation check
function ValidPConf(pconf,pword) {
//##
	if (pconf != pword) return false;
	return true;
}

//email confirmation check
function ValidEConf(econf,email) {
//##
	if (econf != email) return false;
	return true;
}

//validate weblink url
function ValidURL(url) {
//##
	if (url != '' || url.substr(0,7) == 'http://' || url.substr(0,7) == 'HTTP://') return true;
return false;
}

//form input block enter/return key submitting form
function block_enter(e) {
var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 13) { //if character code is equal to ascii 13 (if enter key)
		return false;
	}
	else return true; //return true to the event handler
return true;
}

//trim whitespace from beginning and end of string
function trim(str)
{
    while (str.substring(0, 1) == " "
            || str.substring(0, 1) == "\n"
            || str.substring(0, 1) == "\r")
    {
        str = str.substring(1, str.length);
    }

    while (str.substring(str.length - 1, str.length) == " "
            || str.substring(str.length - 1, str.length) == "\n"
            || str.substring(str.length - 1, str.length) == "\r")
    {
        str = str.substring(0, str.length - 1);
    }

    return str;
}
