var formValidationString = '';  // the contents of this variable will be displayed when a form is not properly filled out.  

var validationStrings = new Array();
var validationErrors = new Array();

function setFieldRequired(FieldID) {
	jQuery('#' + FieldID).addClass('form_required');
}

function ValidateForm(FormID, showError, errorDialog) {
	var ret = true;
	var requiredFields = jQuery('#' + FormID + ' .form_required');
	var turnRed = false;
	var turnBlack = true;
	
	var specificErrors = '';

	requiredFields.each(function(i, v) {
		switch (v.tagName) {
		case 'INPUT':
			if (v.value.trim() == '') {
				ret = false;
				turnRed = true;
				turnBlack = false;
				//jQuery('#' + v.id).parent().prev().css('color', '#FF0000');
			} else {
				turnRed = false;
				turnBlack = true;
				//jQuery('#' + v.id).parent().prev().css('color','#000000');
			}
			if (turnBlack && typeof validationStrings[v.name] != 'undefined') {
				if (validationStrings[v.name].test(v.value)){
					ret = true;
					turnRed = false;
					turnBlack = true;
				}
				else {
					ret = false;
					turnRed = true;
					turnBlack = false;
					specificErrors += '<li>' + validationErrors[v.name] + '</li>'
				}
			}
			break;
		case 'SELECT':
			if (v.selectedIndex == 0) {
				ret = false;
				turnRed = true;
				turnBlack = false;
				//jQuery('#' + v.id).parent().prev().css('color', '#FF0000');
			} else {
				turnRed = false;
				turnBlack = true;
				//jQuery('#' + v.id).parent().prev().css('color','#000000');
			}
			break;
		}
		if (turnRed) {
			jQuery("label[for='" + v.id + "']").css('color', '#FF0000');
			jQuery("#required_legend").css('color', '#FF0000');
		} else if (turnBlack) {
			jQuery("label[for='" + v.id + "']").css('color', '#000000');
			jQuery("#required_legend").css('color', '#FFFFFF');
		}
	});

	if (!ret) {
		//display the error message
		//        var DialogString = '<h2 class="sepImage epBlue">While our website provides a wealth of information our <span class="epRed">FREE DVD</span> is still the best way to see how our swimming machines perform.</h2>';
		//        DialogString += '<table width="90%" align="center" border="0" cellpadding="5"><tr valign="middle"><td align="center" width="50%">';
		//        DialogString += '<img src="indexcimages/requestandproceed.gif" width="135" height="64" onclick="Dialog.closeInfo();" />'
		//       //DialogString += '<button style="height: 60px;" onclick="Dialog.closeInfo();">Request FREE DVD,<br />then proceed to the main website.</button>';
		//        DialogString += '</td><td align="center" width="50%">';
		//        DialogString += '<img src="indexcimages/proceed.gif" width="135" height="64" onclick="location.href = \'main.html\';" />'
		//        //DialogString += '<button style="height: 60px;" onclick="location.href = \'main.html\';">Proceed to the main website.</button>';
		//        DialogString += '</td></tr></table>';

		var DialogString = '<h2 class="sepImage epBlue">Oops!  In order to send you our <span class="epRed">FREE DVD</span> we require a few important pieces of information.  We\'ve hightlighted the missing pieces in <span class="epRed">red</span>.</h2>';
		DialogString += '<table width="90%" align="center" border="0" cellpadding="5"><tr valign="middle"><td align="center">';
		//DialogString += '<img src="http://www.endlesspools.com/indexcimages/requestandproceed.gif" id="PopupErrorDialogButton" width="135" height="64" onclick="Dialog.closeInfo();" />';
		DialogString += '<img src="http://www.endlesspools.com/indexcimages/requestandproceed.gif" id="PopupErrorDialogButton" width="135" height="64" onclick="gPopupErrorDialog.close();" />';
		DialogString += '</td></tr></table>';
		//DialogString += '<button style="height: 60px;" onclick="Dialog.closeInfo();">Request FREE DVD,<br />then proceed to the main website.</button>';
		//DialogString += '</td><td align="center" width="50%">';
		//DialogString += '<img src="indexcimages/proceed.gif" width="135" height="64" onclick="location.href = \'main.html\';" />'
		//DialogString += '<button style="height: 60px;" onclick="location.href = \'main.html\';">Proceed to the main website.</button>';
		//DialogString += '</td></tr></table>';
		//Dialog.info(DialogString, { windowParameters: {resizable: false,  effectOptions: {duration:0.25}, showEffect: Effect.Grow, hideEffect: Effect.Puff, width: 375, height:200, className:"alphacube"}});
		//gPopupErrorDialog = Dialog.info(DialogString, { windowParameters: {id:"gPopupErrorDialog", resizable: false, width: 375, height:200, zIndex:3000, className:"alphacube", onShow: function() {jQuery('#gPopupErrorDialog').click(function(){gPopupErrorDialog.close();});}}});
		formValidationString = '<h2 class="sepImage epBlue">Oops!  In order to register you and send you your <span class="epRed">Idea Kit</span> we require a few important pieces of information.  We\'ve hightlighted the missing pieces in <span class="epRed">red</span>.</h2>'
		if (specificErrors.length > 0) {
			formValidationString += '<ul>' + specificErrors + '</ul>';
		}
	}
	return ret;
}

jQuery(document).ready(function() {
	if (jQuery('input[name="ContactLast"]').length == 1) {
		//demographic form.  attach handlers to save the fields.
		//jQuery('input[name="ContactLast"]').parents('form').find('input').blur(function() {
		//	save_form_field(this);
		//});
		jQuery('input[name="ContactLast"]').parents('form').find('select, input').change(function() {
			save_form_field(this);
		});
		jQuery('input[name="ContactLast"]').parents('form').append('<input type="hidden" name="unique_form_id" value="' + unique_form_id + '" />');
	}
});

function save_form_field(element) {
	//alert (unique_form_id + " : " + element.name + " : " + element.value);
	var elem_val = jQuery.trim(element.value);
	if (elem_val.length > 0) {
		form_save_url = '/all/save_form_field.php?fid=' + escape(unique_form_id) + '&elem_name=' + escape(element.name) + '&elem_value=' + escape(element.value);
		jQuery.ajax({
			url: form_save_url,
			type:'GET',
			dataType:'json',
			async:true,
			timeOut:10000,
			success: function(json) {
			
			},
			error: function() {
			
			},
			complete: function() {
			
			}
		});
	}
}
