$(document).ready(function() {
	$('#formSubmitBtnOff').hide();
	$('#formSubmitBtn').show();
});

$(function() {
	init();
});

//-------------------------------------------------------------------------
function init() {
	var pageWidth = $(window).width();
	var pageHeight = $(document).height();
	$("#screen").css({width:pageWidth, height:pageHeight});
}

//-------------------------------------------------------------------------
function validateFormOnSubmit(theForm) {
	var reason = "";
	$('#formSubmitBtn').hide();
	$('#formSubmitBtnOff').show();

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// What fields you want to validate, and how to validate them.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	reason += validateEmpty(theForm.txtFirstname, 'Your First Name');
	reason += validateEmpty(theForm.txtLastname, 'Your Last Name');
	reason += validateEmail(theForm.txtEmail, 'Your Email Address');
	reason += validateEmpty(theForm.txtOrganization, 'Your Company or Organization');
//	reason += validatePhone(theForm.txtPhone, 'Your Phone Number');

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  if (reason != "") {
		$('#formSubmitBtnOff').hide();
		$('#formSubmitBtn').show();
    return false;
  }
  return true;
}


//-------------------------------------------------------------------------
function validateEmpty(fld, defaultTxt) {
    var error = "";
  	var tmpFldName;
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\?\/\|\{\}\!\#\$\%\^\&\*\+\']/g ;
		
		switch(fld.name)
		{
		case "txtFirstname":
			tmpFldName = "First Name"
			fld.value = fld.value.replace(illegalChars, '');
			break;   
		case "txtLastname":
			tmpFldName = "Last Name"
			fld.value = fld.value.replace(illegalChars, '');
			break;   
		default:
			tmpFldName = fld.name
		}
		
    if (fld.value.length == 0 || fld.value == defaultTxt) {
				$('#validation'+fld.name).css('background','url(/img/imgTemplate/formValidateBad.gif) no-repeat right 50%');
        error = "<p>The required field <strong>" + tmpFldName + "</strong> is empty.<p>";
    } else {
        $('#validation'+fld.name).css('background','url(/img/imgTemplate/formValidateGood.gif) no-repeat right 50%');
    }
    return error;   
}

//-------------------------------------------------------------------------
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

//-------------------------------------------------------------------------
function validateEmail(fld, defaultTxt) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
//		var tfld = fld.value.replace(/\s+/g,'');							// remove all spacess from a text string
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
//    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\?\/\|\{\}\!\#\$\%\^\&\*\+\']/ ;
    		
    if (tfld.value == "" || tfld.value == defaultTxt) {
        $('#validation'+fld.name).css('background','url(/img/imgTemplate/formValidateBad.gif) no-repeat right 50%');
        error = "<p>You didn't enter an <strong>email</strong>.</p>";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        $('#validation'+fld.name).css('background','url(/img/imgTemplate/formValidateBad.gif) no-repeat right 50%');
        error = "<p>Please enter a valid <strong>email</strong>.</p/>";
    } else if (tfld.match(illegalChars)) {
        $('#validation'+fld.name).css('background','url(/img/imgTemplate/formValidateBad.gif) no-repeat right 50%');
        error = "<p>The <strong>email</strong> contains illegal characters.</p>";
    } else {
        $('#validation'+fld.name).css('background','url(/img/imgTemplate/formValidateGood.gif) no-repeat right 50%');
    }
		fld.value = tfld;
    return error;
}

//-------------------------------------------------------------------------
function validatePhone(fld) {
		// clear error messages.
    var error = "";
		var stripped = fld.value
		
		// remove all non digits from the field.
//		stripped = stripped.replace(/\D/g,'');

//		if (stripped == "") {
		// no phone number entered.
//      error = "You didn't enter a phone number.<br/>";
//      fld.style.background = 'Yellow';
//    } else 
		
		if (stripped != "" && stripped.length > 21) {
		// phone number is less than 10 digits.
			error = "<p>Your <strong>phone number</strong> must be less than 22 characters.</p>";
      fld.style.background = 'Yellow';
//		} else if (stripped.length == 11) {
		// if this is an 11 digit #, and the first digit is 1, then remove the 1.
//			stripped = stripped.replace(/^1/, '');
    } else {
			fld.style.background = '#dff2ac';	
		}
	 
		// change the form field iun question to the cleaned value.
//		fld.value = stripped;
    return error;
}
