/* 
* dialog JQuery functions for OTM Sign up
* Version: 1.0
* Author Troy Baalham (troy@yokedesign.com.au)
*/


function blur_dialog_items(){
	$(".ui-button").blur();
	$(".ui-dialog-content a").blur();
	$("#con_card_issuer").blur();
	$("#con_card_type").blur();
	$("#con_card_number").blur();
	$("#conc_expiry_month").blur();
	$("#conc_expiry_year").blur();
	$("#conc_verify").blur();
}

function reset_overlay(){
	$(".ui-widget-overlay").css("width","100%");
	$(".ui-widget-overlay").css("height","100%");
}

function restyle_dialog(dialogname) {
	$(dialogname).siblings('div.ui-dialog-titlebar').remove();
	$(dialogname).parent().removeClass("ui-corner-all");
	$(dialogname).parent().addClass("ui-corner6-all");
	$(dialogname).css("padding","5px"); 
}

function init_error_dialog(){
	$('#error_dialog').dialog({
		autoOpen: false,
		title: 'Error Dialog',
		dialogClass: 'ui-error-dialog',
		modal: true,
		draggable: false,
		resizable: false,
		width: 350,
		open: function(){blur_dialog_items();}
	});
	restyle_dialog('#error_dialog');
	
	// add button handlers for dialog
	$("#error_dialog_close_button").click(function(){
		$("#error_dialog").dialog("close");
	});
}

function displayerror(title,content){
	$("#error_dialog_title").html(title);
	$("#error_dialog_content").html(content);
	$("#error_dialog").dialog("open");
}

function init_lifesupport_dialog(){
	var $lifesupport_dialog = $("#lifesupport_dialog").dialog({
		autoOpen: false,
		title: 'Life Support Info',
		dialogClass: 'ui-standard-dialog',
		modal: true,
		draggable: false,
		resizable: false,
		width: 680,
		closeOnEscape: false,
		open: function(){
			blur_dialog_items();
			
			/* if ( $.browser.msie ) {
			  	var vmajor = parseInt($.browser.version, 10);
				if (vmajor < 8){
					$("#lifesupport_dialog").height($("#lifesupport_dialog_container").height() + 22);
					$("#lifesupport_dialog").css("margin-left:20px");
				}
			} */
		}
	});
	restyle_dialog("#lifesupport_dialog");
	
	// add button handlers for dialog
	$("#lifesupport_dialog_close_button").click(function(){
		$("#lifesupport_dialog").dialog("close");
	});
	
	return($lifesupport_dialog);
}

function init_concession_dialog(){
	var $concession_dialog = $("#concession_dialog").dialog({
		autoOpen: false,
		title: 'Concession Dialog',
		dialogClass: 'ui-standard-dialog',
		modal: true,
		draggable: false,
		resizable: false,
		minWidth: 680,
		minHeight: 300,
		closeOnEscape: false,
		open: function(){blur_dialog_items();}
	});
	restyle_dialog("#concession_dialog");
	
	// add button handlers for dialog
	$("#concession_dialog_save_button").click(function(){
		if (validate_concession()){
			$("#conc_edit").html("<span class='mya' id='conc_edit_link'>[edit]</span>");
			$("#concession_dialog").dialog("close");
		}
	});
	
	$("#concession_dialog_cancel_button").click(function(){
		clear_concession();
		$("#conc_edit").html("");
		$("#concession_dialog").dialog("close");
	});
	
	return($concession_dialog);
}

function validate_concession(){
	var valid = true;
	if ($("#con_card_issuer").val() == "select") {
		$("#con_card_issuer_error").html("*");
		valid = false;
	}
	else {
		$("#con_card_issuer_error").html("");
	}
	if ($("#con_card_type").val() == "select") {
		$("#con_card_type_error").html("*");
		valid = false;
	}
	else {
		$("#con_card_type_error").html("");
	}
	if ($("#con_card_number").val().length < 10) {
		$("#con_card_number_error").html("*");
		valid = false;
	}
	else {
		$("#con_card_number_error").html("");
	}
	if (($("#con_card_expiry_month").val() == "MM") || ($("#con_card_expiry_year").val() == "YYYY")) {
		$("#con_card_expiry_error").html("*");
		valid = false;
	}
	else {
		$("#con_card_expiry_error").html("");
		var mm = $("#con_card_expiry_month").val();
		var yy = $("#con_card_expiry_year").val();
		var dt = new Date();
		dt.setFullYear(yy,mm - 1,1);
		var today = new Date();
		today.setDate(1);
		if (dt.getTime() < today.getTime()){
			$("#con_card_expiry_error").html("*");
			valid = false;
		}
		else {
			$("#con_card_expiry_error").html("");
		}
	}
	
	if (!$("#conc_verify").is(":checked")){
		$("#conc_verify_error").html("*");
		valid = false;
	}
	else {
		$("#conc_verify_error").html("&nbsp;");
	}
	if (!valid) {
		$("#conc_error_message").html("<p>error!</p>");
	}
	else {
		$("#conc_error_message").html("");
	}
	return(valid);
}

function clear_concession(){
	$("#con_card_issuer").val("select");
	$("#con_card_issuer_error").html("");
	$("#con_card_type").val("select");
	$("#con_card_type_error").html("");
	$("#con_card_number").val("");
	$("#con_card_number_error").html("");
	$("#conc_expiry_month").val("MM");
	$("#conc_expiry_year").val("YYYY");
	$("#con_card_expiry_error").html("");
	$("#conc_verify").attr("checked",false);
	$("#conc_error_message").html("");
	$("#concession_holder").attr("checked",false);
}

function init_final_consent_dialog(dialog_id,dialog_title){
	$("#"+dialog_id+"_dialog").dialog({
		autoOpen: false,
		title: dialog_title,
		dialogClass: 'ui-standard-dialog',
		modal: true,
		draggable: false,
		resizable: false,
		minWidth: 680,
		closeOnEscape: false,
		/* minHeight: 300, */
		open: function(){blur_dialog_items();},
		close: function (){
			if ($(this).data("button_action") != "accept"){
				$("#"+dialog_id+"_checkbox").attr("checked",false);
			}
		}
	});
	restyle_dialog("#"+dialog_id+"_dialog");
	
	// add button handlers for dialog
	$(".final_consent_decline_button").live("click",function(){
		var btnid = $(this).attr("id");
		var dialog_id = btnid.replace("_decline_button","");
		$("#"+dialog_id+"_checkbox").attr("checked",false);
		$("#"+dialog_id+"_dialog").data("button_action","decline");
		$("#"+dialog_id+"_dialog").dialog("close");	
	});
	$(".final_consent_accept_button").live("click",function(){
		var btnid = $(this).attr("id");
		var dialog_id = btnid.replace("_accept_button","");		
		$("#"+dialog_id+"_checkbox").attr("checked",true);
		$("#"+dialog_id+"_dialog").data("button_action","accept");
		$("#"+dialog_id+"_dialog").dialog("close");	
	});
}



function update_connect_dialogs(pcode){
	/* jQuery('#form_wrapper').showLoading(); */
	$(".connect_dialog_connectservice").each(function(){
		var sid = $(this).data("service_id");
		var scount = $(this).data("service_count");
		var myid = $(this).attr("id");
		var plandiv = "#"+myid+"_plan_content";
		var planinfodiv = "#connectservice"+scount+"_plan";
		var plancheckbox = "#connectservice"+scount+"_connect_selected";
		
		$.ajax({
			  url: "/signup_form/ajax/get_plan_data_ajax.php",
			  dataType: "text",
			  data: {postcode : pcode, serviceid : sid, servicecount: scount},
			  success: function (data, textStatus, jqXHR){
				  if (jQuery.trim(data) == ""){
					  $(planinfodiv).html("- no connection plans available");
					  // disable this service
					  $(plancheckbox).attr("disabled","disabled");
				  }
				  else {
					  $(plandiv).html(data);
				  }
			  }
		});
	});
	/* jQuery('#form_wrapper').hideLoading(); */
}
