function isValidPhoneNumber(s) {
	var temp = s.replace(/\D/g, "")
	return temp.length > 8 && temp.length < 26
}

function isValidSelectBox(o) {
	return (o.options[o.selectedIndex].value != "_none_" &&
			o.options[o.selectedIndex].value.trim() != "")
}

function isValidDate(s) {
	var temp = s.replace( /\s/g, "");
	re = /^((3[01]|0[1-9]|[12]\d)|([1-9]))\/((0[1-9]|1[012])|([1-9]))\/(\d{4}|\d{2})$/;
	return ( re.test(temp) );
	return false;
}

function isValidEmailAddress(s) {
	var temp = s.replace(/\s/g, "")
//	return (temp.match(/^[\w\.\-]+\x40[\w\.\-]+\.\w{3}$/)) &&

return (temp.match(/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/)) &&
			temp.charAt(0) != "." && !(temp.match(/\.\./))
}
function textFieldBlurHandler() {
	this.value = this.value.trim()
}
function numFieldBlurHandler() {
	if( isNaN( this.value ) ) {
		this.value = '';
	} else {
		this.value = this.value.replace(/\D/g, "")
	}
}

function ageFieldBlurHandler() {
	if( isNaN( this.value ) ) {
		this.value = '';
	} else {
		this.value = this.value.replace(/\D/g, "")
		if( this.value < 0 || this.value > 120 ) {
			alert( 'Invalid age entered');
			this.focus();
		}

	}
}

function yearFieldBlurHandler() {
	if( isNaN( this.value ) || this.value == '0' || this.value == '') {
			this.value = '';
	} else {
		if (this.value < 1900 || this.value > 2100) {
			this.value = '';
			alert( 'Please enter a valid year or clear the field to skip' );
			this.focus();
		}
	}


}

function phoneFieldBlurHandler() {
	var temp = this.value.replace(/\D/g, "")

	//if (temp.length > 9 && temp.length < 26)
	//	if (temp.length == 10) {
	//		this.value = "(" + temp.substring(0,3) + ") "
	//		this.value += temp.substring(3,6) + "-" + temp.substring(6,10)
	//	}
}

// convenience function.
// attaches textFieldBlurHandler to the onblur event
// of every text or textarea element in f
// var myForm = document.myForm
// attachAllTextHandlers(myForm)
//		-> every text element in myForm now will call
//		   textFieldBlurHandler (above) onblur
function attachAllTextHandlers(f) {
	var el
	for (var i = 0; (el = f.elements[i]); i++) {
		if (el.type == "text" || el.type == "textarea")
			el.onblur = textFieldBlurHandler
	}
}

// as convenient a place as any to store this...
var STD_ERROR_PREFIX = "There were one or more problems with the form values "
STD_ERROR_PREFIX += "you entered.\nPlease check the following and try submitting "
STD_ERROR_PREFIX += "the form again:\n\n"



String.prototype.trim = function() {return this.replace(/^\s*|\s*$/g, "")}


String.prototype.endsWith = function() {
	var bOk = false
	for (var i = 0; i < arguments.length; i++) {
		if (this.indexOf(arguments[i]) == this.length - arguments[i].length) {
			bOk = true
			break
		}
	}
	return bOk
}


function popup( s ) {
	window.open( s, 'popwin', 'width=530,height=500,left=150,top=100,menubar');
}
function confirmpopup(s) {
	var t = confirm("Are you sure you want to delete this?");
	if( t ) {
		//alert('yes');
		window.open( s, 'popwin', 'width=250,height=100,left=150,top=150,menubar');
	}
}

function init() {
	if( document.myform ) {
		var f = document.myform;
		attachAllTextHandlers(f);
		attachCheckbox(f);		
//		gensql();
	}
}
onload=init;

if( !Array.prototype.push() ) {
	Array.prototype.push = function(s) { 
		this[this.length] = s;
	}
}


function gensql() {
	
	var f = document.myform;
	
	var div = document.getElementById("temp");

	var el;
	//var sql = "var sql = \"INSERT INTO [table] (bApproved, "

	var sql = '';

	for (var i = 0; (el = f.elements[i]); i++) {
		if( el.type != "submit" && el.type != "reset" ) {
			sql+= 'var ' + el.name + ' = (String(Request("' + el.name + '"))).Escape(0).int();<br>'
		}
	}
	sql += 'var id = (String(Request("id"))).Escape(0).int();<br>'
	sql += 'var bApproved = (String(Request("bApproved"))).Escape(0).int();<br>'

	sql += "<br><br>";
	
	sql += "var sql = \"INSERT INTO [content_] (bApproved, "

	for (var i = 0; (el = f.elements[i]); i++) {
		if( el.type != "submit" && el.type != "reset" && el.type != "hidden") {
			sql+= el.name;
			if( (f.elements[i+1] && f.elements[i+1].type != "submit" && f.elements[i+1].type != "reset") ) {
				sql += ', ';
			}
		}
	}
	sql += ') values (0, ';
	
	for (var i = 0; (el = f.elements[i]); i++) {
			if( el.type != "submit" && el.type != "reset" && el.type != "hidden") {
				sql+= "'\"+" + el.name + "+\"'";
				if( (f.elements[i+1] && f.elements[i+1].type != "submit" && f.elements[i+1].type != "reset" && f.elements[i+1].type != "hidden") ) {
					sql += ', ';
				}
			}
	}
	sql += ')\";';
	
	//div.innerHTML = sql;
}
function attachCheckbox(f) {
	var el
	for (var i = 0; (el = f.elements[i]); i++) {
		if (el.type == "checkbox")
		{
			if( el.checked ) {
				el.value=1;
			} else {
				el.value=0;
			}
//			alert( el.value );
			el.onblur = checkboxblurhandler;
		}

	}
}

function checkboxblurhandler() {
	//alert( this.value );
	if( this.checked ) {
		this.value=1;
	} else {
		this.value=0;
	}
}
