﻿jQuery(function () {
	jQuery('.input-radio-set').buttonset();
	jQuery('body').append('<div id="auto-form-loader" style="text-align: center; height: 30px; width: 300px; background-image: url("http://thechampeon.com/net/images/processing-blue.gif"); background-repeat: no-repeat; background-position: center center">&nbsp;</div>');
	jQuery('#auto-form-loader').dialog({
		'title': 'Submitting Form...',
		'show': 'slide',
		'hide': 'fade',
		'width': 400,
		'height': 70,
		'autoOpen': false,
		'resizable': false,
		'modal': true
	});

	jQuery('.input-text, .input-textarea').bind('change', function () {
		fDomID = jQuery(this).attr('id');
		storeAutoFormField(fDomID);
	});
	initAutoForm();
});

function initAutoForm() {
	jQuery('.input-text, .input-textarea').each(function () {
		fDomID = jQuery(this).attr('id');
		fVal = jQuery.cookie('form' + window.formid + '-' + fDomID);
		if (fVal == undefined) {
			fVal = '';
		}

		jQuery(this).val(fVal);
	});

	jQuery("label.overlabel").overlabel();
}

function storeAutoFormField(fDomID) {
	fVal = jQuery('#' + fDomID).val();
	jQuery.cookie('form' + window.formid + '-' + fDomID, fVal);
}

function submitAutoForm(fFormID) {
	fURL = window.postURL+'?pid=' + window.pid + '&spid=update&formid=' + window.formid;
	if (autoFormCheck()) {
		fArray = jQuery('#' + fFormID).serializeArray();
		jQuery('.form-submit').hide();
		autoFormLoaderStart();
		jQuery.post(fURL, fArray, function (data) {
			jQuery('#sitepages-form-' + window.formid).html(data);
			autoFormLoaderStop();
		});
	}
}

function autoFormLoaderStart() {
	jQuery('#auto-form-loader').dialog('open');
}

function autoFormLoaderStop() {
	jQuery('#auto-form-loader').dialog('close');
}

function autoFormCheck() {
	fStatus = true;

	/* Check Required Fields */
	jQuery('.input-required').each(function () {
		fVal = jQuery(this).val();
		if (fVal == '' || fVal == 0 || fVal == '0') {
			jQuery(this).addClass('input-warning');
			fStatus = false;
		} else {
			jQuery(this).removeClass('input-warning');
		}
	});

	/* Check Email Fields */
	jQuery('.input-email').each(function () {
		fVal = jQuery(this).val();
		if (!valEmail(fVal)) {
			jQuery(this).addClass('input-warning');
			fStatus = false;
		}
	});

	return fStatus;
}

function valEmail(str) {
	var at = "@"
	var dot = "."
	var lat = str.indexOf(at)
	var lstr = str.length
	var ldot = str.indexOf(dot)
	if (str.indexOf(at) == -1) {
		return false
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
		return false
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
		return false
	}

	if (str.indexOf(at, (lat + 1)) != -1) {
		return false
	}

	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
		return false
	}

	if (str.indexOf(dot, (lat + 2)) == -1) {
		return false
	}

	if (str.indexOf(" ") != -1) {
		return false
	}

	return true
}

