var validator_counter=0;

var verify_failed_counter=0;
function verify_alphanum(x, type){
	if (!x.value.search(/^\w{2,}$/)){
		highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		return false;
	}
}

function verify_complete(x, type){
	if (!x.value.search(/^.{2,}$/)){
		highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		return false;
	}
}

function verify_email(x, type){
	if (!x.value.search(/^([a-zA-Z0-9\_\'\+\*\$\%\^\&\!\.\-])+[\@\+](([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$/)){
		highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		return false;
	}
}

function verify_number(x, type){
	if (!x.value.search(/^\d+$/)){
	    highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		return false;
	}

}

function verify_zipcode(x, type){
	if (!x.value.search(/^\d{5}$/)){
	    highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		return false;
	}

}

function verify_phone(x, type){
	//strip out special characters
	var temp = x.value.replace(/\(|\)|\-|\./g,'');
	if (!temp.search(/^\d{10}$/)){
		x.value = temp.substring(0,3)+'-'+temp.substring(3,6)+'-'+temp.substring(6); 
		
		highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		
		return false;
	}
}

function verify_date(x, type){
	x.value = normalize_date(x.value);
	if (is_date(x.value)){
		highlight_ok(x);
		return true;
	}else{
		if (type =='warning'){
			highlight_warning(x);
		}else{
			highlight_error(x);
		}
		
		return false;
	}
}

function normalize_date(thedate){
	thedate = thedate.replace(/-/g,'/');
	//given a date string return a formated sting
	var date;
	thedate == '' ? date = new Date() : date = new Date(thedate); 
	var m = date.getMonth()+1;
	m < 10 ? m= '0'+m : 0;
	var d = date.getDate();
	d < 10 ? d= '0'+d : 0;
	var y = date.getYear();
	y > 1900 ? y+0 : y=1900+y;
	if (m && d && y){
		return m +'/'+ d +'/'+ y;
	}else{
		return '';
	}
	
}

function is_date(thedate){
	var date = new Date(thedate);
	try{
		var s = date.getYear()+'0';
		if ( !s.search(/^\d+$/) ){
			return true;
		}else{
			return false;	
		}
	}catch(e){
		alert(e);
		return false;	
	}
}

function highlight_ok(x){
	$('#'+x.id).addClass('ok');
	$('#'+x.id).removeClass('error');
	$('#'+x.id).removeClass('warning');
}
function highlight_warning(x){
	$('#'+x.id).addClass('warning');
	$('#'+x.id).removeClass('error');
	$('#'+x.id).removeClass('ok');
}
function highlight_error(x){
	$('#'+x.id).addClass('error');
	$('#'+x.id).removeClass('ok');
	$('#'+x.id).removeClass('warning');
}
