/*
 * jQuery 1.1.3.1 - New Wave Javascript
 *
 * Copyright (c) 2007 John Resig (jquery.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2007-07-05 00:43:24 -0400 (Thu, 05 Jul 2007) $
 * $Rev: 2243 $
 */



// opens new window
// -----------------------------------------------------------------
var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable=no,toolbar=no,location=no,scroll=yes,scrollbars=yes,menubar=no,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable=no,toolbar=no,location=no,directories=no,status=no,scroll=no,scrollbars=no,menubar=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}




function validate_form(frm)
{
	var msg = 'Please correct the following errors:\n';
   	var is_valid = true;
   	var fields = ['first_name','last_name','phone','email'];
   	$(fields).each(function(i){       	    
   		if (frm.elements[fields[i]].value == '') {
   		    var field_name = fields[i].replace(/_/g,' ');
   			msg = msg + '\n '+field_name+' is a required field';
   			$(frm.elements[fields[i]]).addClass('form_error');
   			is_valid = false;
   		} else {
   		    $(frm.elements[fields[i]]).removeClass('form_error');
   		}
   		if (fields[i] == 'email') {
   		    var value = frm.elements['email'].value;
           	var regex = /^((\"[^\"\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\-]+))$/;
           	if (value != '' && !regex.test(value)) {
           		is_valid = false;
           		msg = msg + '\n email must be a valid email address';
           	}
   		}

		if (fields[i] == 'phone') {
			var value = frm.elements['phone'].value;
			if(value == ''){
				msg = msg + "\n phone number must be 10 digits";
			} else if(value != '') {
				var digits = value.replace(/[^0-9]/ig, '');
		     	if (!digits) {
					msg = msg + "\n phone number must be 10 digits";
			     }
			}
		}
   	});	

	// disclaimer check	
	if (frm.elements['disclaimer_agree'] && frm.elements['disclaimer_agree'].checked == false) {
	  	msg = msg + "\n you must agree to the disclaimer";
		is_valid = false;		
	}


   	// check date
   	if($('input.date-picker')) {
   		$('input.date-picker').each(function(i){
   			if (this.value != "") {
   				var date_regex = /^([0-9]{1,2})[-\/]([0-9]{1,2})[-\/]([0-9]{4})$/;
   				if(!date_regex.test(this.value)) {       					
   					msg = msg + '\n '+this.name.replace(/_/g,' ')+' must match format mm/dd/yyyy';
   					$(this).addClass('form_error');
   					is_valid = false;
   				} else {
   				    $(this).removeClass('form_error');
   				}
   			}
   		});
   	}
   	if (!is_valid) {
   	    alert(msg);
   	    return is_valid;
   	};
   	return is_valid;
}

$(document).ready(function() {
	$('form#frm-contact').submit(function(){
		return validate_form(this);
	});
});
