//dialog code

function prepareDialog() {
	$('#dialog_panel').jqm({
		overlay: 30, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
		overlayClass: 'whiteOverlay'})
				  
	// Close Button Highlighting. IE doesn't support :hover. Surprise?
	$('input.jqmdX')
	  .hover(
		function(){ $(this).addClass('jqmdXFocus'); }, 
		function(){ $(this).removeClass('jqmdXFocus'); })
	  .focus( 
		function(){ this.hideFocus=true; $(this).addClass('jqmdXFocus'); })
	  .blur( 
		function(){ $(this).removeClass('jqmdXFocus'); });
}
	
function openDialog(diagname) {	
	$('html, body').scrollTop(0);
	switch (diagname) {
		case "get_intertainradio":
			$.post("../loadDialog.php",{ sendValue: "get_intertainradio" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
			}, "html");
			break;
		case "request_a_quote":
			$.post("../loadDialog.php",{ sendValue: "request_a_quote" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
			}, "html");
			break;
		case "request_a_callback":
			$.post("../loadDialog.php",{ sendValue: "request_a_callback" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
				$("#date").datepicker();
			}, "html");
			break;
		case "ask_about_hardware":
			$.post("../loadDialog.php",{ sendValue: "ask_about_hardware" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
			}, "html");
			break;
		case "ask_about_scalability":
			$.post("../loadDialog.php",{ sendValue: "ask_about_scalability" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
			}, "html");
			break;
		case "get_intertaintv":
			$.post("../loadDialog.php",{ sendValue: "get_intertaintv" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
			}, "html");
			break;
		case "get_intertainonhold":
			$.post("../loadDialog.php",{ sendValue: "get_intertainonhold" },
			function(data){
				$('div.dialog_container').html(data);
				prepareDialog();
				initializeForm();
				$('#dialog_panel').jqmShow();
			}, "html");
			break;
		default : alert("Error: Invalid Dialog Parameter");
	}
}

//form code

function setupAjaxForm(form_id, form_validations){
	var form = '#' + form_id;
	
	var disableSubmit = function(val){
		$(form + ' button[type=submit]').attr('disabled', val);
	};
	
	var options = {
		dataType:  'text',
		beforeSubmit: function(){
			if(typeof form_validations == "function" && !form_validations()) {
				return false;
			}
			disableSubmit(true);
		},
		success: function(message){
			disableSubmit(false);
			//$(form).clearForm();
			responseArray = message.split("~");
			if(responseArray[0] == 'success')
			{
				pageTracker._trackEvent('Leads', responseArray[1]);
				$('#dialog_panel').jqmHide();
				alert(responseArray[2]);
			}
			else if(responseArray[0] == 'error')
			{
				alert(responseArray[2]);
			}
		}
	};
	$(form).ajaxForm(options);
}

function initializeForm() {  
    new setupAjaxForm('lead_form', function(){
		var isValid = true;
		$(".required").removeClass('ui-state-error');
		isValid = isValid && checkLength("#email","email",6,80);
		isValid = isValid && checkEmail("#email", /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please use the following format:  john_smith@gmail.com");
		if (!isValid || !requiredFull(".required")) {
			isValid = false;
		}
	
		return isValid;	
    });
};

function updateTips(t) {
	$('.validateTips').text(t).css({'color' : '#FF0000', 'font-weight' : 'bold'});
}

function checkLength(o,n,min,max) {
	if ( $(o).val().length > max || $(o).val().length < min ) {
			$(o).addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
	} else {
		return true;
	}
}

function checkEmail(email, regexp, emailTip) {
	if ( !( regexp.test( $(email).val() ) ) ) {
		$(email).addClass('ui-state-error');
		updateTips(emailTip);
		return false;
	} else {
		return true;
	}
}
		
function requiredFull(reqclass) {
	var fullFields = true;
	$(reqclass).each( 
		function(intIndex) {
			if ($(this).val() == "")
			{
				$(this).addClass('ui-state-error');
				fullFields = false;
			}
	});
			
	if (!fullFields) {
		updateTips("Please fill in all highlighted fields");
	}
			
	return fullFields;
}