//*** Helper functions

function PromptForZip(arg) {
	//*** Create a reference to this instance;
	var me = this;
	this.arg = arg;
	this.dialogX = (typeof(arg.dialogX) == "undefined") ? null : arg.dialogX; 
	this.dialogY = (typeof(arg.dialogY) == "undefined") ? null : arg.dialogY; 
	this.dialogSelector = (typeof(arg.dialog) == "undefined") ? null : $("#" + arg.dialog);
	this.dialogOKSelector = this.dialogSelector.find("#zip_ok");
	this.cszSelector = (typeof(arg.csz) == "undefined") ? null : $("#" + arg.csz);
	this.inputFieldSelector = this.dialogSelector.find("#zip");
	this.hiddenFieldSelector = (typeof(arg.hiddenZip) == "undefined") ? null : $("#" + arg.hiddenZip);
	this.errorSelector = this.dialogSelector.find("#zip_error");
	this.errorMessage = (typeof(arg.errorMessage) == "undefined") ? null : arg.errorMessage;
	//*** Callbacks:
	this.onCancel = (typeof(arg.onCancel) == "undefined") ? null : arg.onCancel; 
	this.onSuccess = (typeof(arg.onSuccess) == "undefined") ? null : arg.onSuccess; 
	this.onMatchAction = (typeof(arg.onMatchAction) == "undefined") ? null : arg.onMatchAction;
	this.onNoMatchAction = (typeof(arg.onNoMatchAction) == "undefined") ? null : arg.onNoMatchAction;

	this.dialogSelector.dialog({
		modal: true,
		autoOpen: false,
		height: 170,
		title: "Please enter Zip code:",
		position: [this.dialogX, this.dialogY],
		close: function(event, ui) {
			if (event.button != 0)
				return;
			if (me.onCancel != null)
				me.onCancel(me.arg);
		}
	});
	

	this.dialogSelector.keyup(function(e) {
	    if (e.keyCode == 13) {
		me.zip_handler();
	    }
	});
	
	this.dialogOKSelector.click(function() {
		me.zip_handler();
	});
	
	PromptForZip.prototype.zip_handler = function () {
		if (me.inputFieldSelector.val() != "") {
			me.onSuccess(me.arg);
			//*** Kill this instance to avoid looping
			delete me;
		} else {
			me.inputFieldSelector.css("border","2px solid red");
			me.inputFieldSelector.focus();
		}
	}
	
	this.inputFieldSelector.val(this.hiddenFieldSelector.val());
	this.dialogSelector.css("visibility", "visible");
	this.dialogSelector.dialog('open');
	this.inputFieldSelector.focus();
} //*** PromptForZip class

function PromptForLogin(arg) {
	//*** Create a reference to this instance;
	var me = this;
	this.arg = arg;
	this.ajaxURL = (typeof(arg.ajaxURL) == "undefined") ? null : arg.ajaxURL;
	this.dialogSelector = (typeof(arg.dialog) == "undefined") ? null : $("#" + arg.dialog);
	this.top = (typeof(arg.top) == "undefined") ? null : arg.top; 
	this.left = (typeof(arg.left) == "undefined") ? null : arg.left;
	this.fadeSelector = (typeof(arg.fade) == "undefined") ? null : $("#" + arg.fade);
	this.redirectTo = (typeof(arg.redirectTo) == "undefined") ? null : arg.redirectTo;
	this.ajaxURL = (typeof(arg.ajaxURL) == "undefined") ? null : arg.ajaxURL;
	this.errorPage = (typeof(arg.errorPage) == "undefined") ? null : arg.errorPage;
	this.redirectOnError = (typeof(arg.redirectOnError) == "undefined") ? false : arg.redirectOnError;
	this.loginParam = (typeof(arg.loginParam) == "undefined") ? "" : arg.loginParam;
	
	//*** Implied fields contained within login box
	this.usernameFieldSelector = this.dialogSelector.find("#user_name");
	this.passwordFieldSelector = this.dialogSelector.find("#password");
	this.loginButtonSelector = this.dialogSelector.find("#btn_login");
	this.closeButtonSelector = this.dialogSelector.find("#btn_close");
	this.rememberCheckSelector = this.dialogSelector.find("#chk_remember");
	this.invalidLoginSelector = this.dialogSelector.find("#invalid_login");
	
	if (this.top != null && this.left != null) {
		//*** Place the box where specified
		this.dialogSelector.css({ 
			"top" : me.top + "px", 
			"left" : me.left + "px"
		});
	}
	
	this.dialogSelector.show();

	this.usernameFieldSelector.focus();
	
	this.dialogSelector.click(function(e) {
		var x = parseInt(e.pageX - $(this).offset().left);
		var y = parseInt(e.pageY - $(this).offset().top);
		
		if (x >= 175 && y <= 20) {
			$(this).hide();
		}
	});
	
	this.loginButtonSelector.click(function(e) {
		me.login_handler();
	});
	
	PromptForLogin.prototype.login_handler = function () {
		if (me.usernameFieldSelector.val() == "" || me.passwordFieldSelector.val() == "") {
			this.usernameFieldSelector.focus();
			return;
		}
		
		$.ajax({
		    url: me.ajaxURL,
		    type: 'POST',
		    data: "user_name=" + me.usernameFieldSelector.val() + "&password=" + me.passwordFieldSelector.val() + "&remember_me=" + me.rememberCheckSelector.is(":checked"),
		    success: function(data) {
		    	//*** Ajax returns URL for redirection on success
		    	if (data == "Login Failed") {
		    		if (me.redirectOnError)
		    			document.location = me.errorPage + "&invalid=true&user_entered=" + me.usernameFieldSelector.val();
		    		else
		    			me.invalidLoginSelector.html("Invalid User name or Password");
		    	} else {
		    		if (me.loginParam != "")
		    			me.loginParam += data.toString();
		    		me.goto_location(me.redirectTo + me.loginParam);
		    	}
		    	if (me.redirectOnError)
					me.dialogSelector.hide(); 
		    }
		});
	},
	
	PromptForLogin.prototype.fade_and_go = function (whereTo, loc) {
		me.fadeSelector.html("<div style='white-space: nowrap;'>Redirecting to " + whereTo + " . . .</div>");
		me.fadeSelector.dialog({
			modal: true,
			autoOpen: true,
			resizable: false
		});
		$(".ui-dialog-titlebar").hide();
		//*** Perform redirection after a brief delay
		setTimeout(function() { me.goto_location(loc) },2000);
	}
		
	PromptForLogin.prototype.goto_location = function (loc) {
		document.location = loc;
	}
}

function RedirectTo(arg) {
	var me = this;
	this.arg = arg;
	this.dialogSelector = (typeof(arg.dialog) == "undefined") ? null : $("#" + arg.dialog);
	this.top = (typeof(arg.top) == "undefined") ? 0 : arg.top; 
	this.left = (typeof(arg.left) == "undefined") ? 0 : arg.left;
	this.redirect = (typeof(arg.redirectTo) == "undefined") ? null : arg.redirectTo;
	this.url = (typeof(arg.url) == "undefined") ? null : arg.url;
	this.timer = null;
	
	RedirectTo.prototype.fade_and_go = function () {
		var childDiv = me.dialogSelector.children("div");
		var childBtn = me.dialogSelector.find("input");
		
		$(childBtn[0]).css({ display: "block" });
		$(childBtn[0]).click(function() {
			me.dialogSelector.dialog("close");
			clearTimeout(me.timer);
			$(this).unbind();
			me = null;
		});
		
		$(childDiv[0]).html("<div style='white-space: nowrap;'>Redirecting to " + me.redirect + " . . .</div>");
		me.dialogSelector.css({ background : "url('./images/ajax-loader.gif') center center no-repeat" });
		me.dialogSelector.dialog({
			modal: true,
			autoOpen: true,
			height: 250,
			resizable: false
		});
		$(".ui-dialog-titlebar").hide();
		//*** Perform redirection after a brief delay
		me.timer = setTimeout(function() { me.goto_location() },4000);
	}
	
	RedirectTo.prototype.goto_location = function () {
		document.location = me.url;
	}
	
	this.fade_and_go();
	
}

//*** Helper functions
function checkZipCode(arg) {
	var ajaxURL = (typeof(arg.ajaxURL) == "undefined") ? null : arg.ajaxURL;
	var dialogSelector = (typeof(arg.dialog) == "undefined") ? null : $("#" + arg.dialog);
	var hiddenFieldSelector = (typeof(arg.hiddenZip) == "undefined") ? null : $("#" + arg.hiddenZip);
	var inputFieldSelector = this.dialogSelector.find("#zip");
	var resultsSelector = (typeof(arg.results) == "undefined") ? null : $("#" + arg.results);
	var resultsMessage = (typeof(arg.resultsMessage) == "undefined") ? "" : arg.resultsMessage;
	var errorPage = (typeof(arg.errorPage) == "undefined") ? "" : arg.errorPage;

	$.ajax({
	    url: ajaxURL,
	    type: 'POST',
	    data: "ZIP=" + inputFieldSelector.val(),
	    success: function(data){
	    	if (data != "TRUE") {
	    		document.location = errorPage + data;
	    	} else {
	    		resultsSelector.html(resultsMessage + inputFieldSelector.val());
	    		hiddenFieldSelector.val(inputFieldSelector.val());
	    	}
			dialogSelector.dialog('close'); 
	    }
	});
}

function goBack() {
	history.back();
}

function estimate() {
	var comma = "";
	var chargeParts;
	checked = "";
	$(".pestcheck").each(function(index) {
		if ($(this).css('display') == "block") {
			checked += comma + $(this).attr("id").replace("_check","");
			comma = ",";
		}
	});
	
	if (checked == "") {
		$("#quick_estimate").html("$" + webBasePrice);
	} else {
		$("#pest_services").val(checked);
		$.ajax({
		    url: './include/get_estimate.php',
		    type: 'POST',
		    data: "pests=" + checked,
		    error: function(){
		        $('#err_msg').html('Error loading document');
		    },
		    success: function(data){
		    	chargeParts = data.split(";");
		    	$("#quick_estimate").html("$" + chargeParts[0] + "*");
		    	if (chargeParts[1] > 0) {
		    	   	$("#quarterly_estimate").html("*Add $" + chargeParts[1] + " per quarter for <nobr>re-inspection.</nobr>");
		    	} else {
		    		$("#quarterly_estimate").html("");
		    		$("#quick_estimate").html($("#quick_estimate").html().replace("*",""));
		    	}
		    }
		});
	}
}

function resetZip(arg) {
	var ajaxURL = (typeof(arg.ajaxURL) == "undefined") ? null : arg.ajaxURL;
	var dialogSelector = (typeof(arg.dialog) == "undefined") ? null : $("#" + arg.dialog);
	var cszSelector = (typeof(arg.csz) == "undefined") ? null : $("#" + arg.csz);
	var hiddenFieldSelector = (typeof(arg.hiddenZip) == "undefined") ? null : $("#" + arg.hiddenZip);
	var inputFieldSelector = this.dialogSelector.find("#zip");
	var errorSelector = this.dialogSelector.find("#zip_error");
	var errorMessage = (typeof(arg.errorMessage) == "undefined") ? null : arg.errorMessage;
	var zipVal;
	
	$.ajax({
	    url: ajaxURL,
	    type: 'POST',
	    data: "ZIP=" + inputFieldSelector.val(),
	    success: function(data){
	    	if (data != "FALSE") {
	    		cszSelector.html(data);
				dialogSelector.dialog('close'); 
    			hiddenFieldSelector.val(inputFieldSelector.val());
		    } else {
	    		if (errorSelector != null) {
	    			zipVal = inputFieldSelector.val();
	    			errorSelector.html(errorMessage + zipVal);
	    		}
	    	}
	    }
	});
}


function showMore(thisLink) {
	var tag = thisLink.id;
	currentPest.attr("class","pest");
	showPestPop(tag,false,"pest");
}

function showPestPop(seek_name, is_group, pop_type) {
	var top = parseInt($("#content").offset().top) + 10; 
	var left = parseInt($("#content").offset().left) + 10;
	var mysteriousOffset = 0;
	var ajaxURL;
	var urlString;
	
	$("#content").fadeTo('fast', 0.4);
	
	if($.browser.msie) {
		curvyCorners(settings, "#pest_popup");
	} else {
		mysteriousOffset = 34;
	}

	$("#content, #nav").block({
		message: null,
		overlayCSS:  { 
    	    backgroundColor: 'transparent', 
       		opacity: 0.0,
       		cursor: 'default'
    	} 
    });
    
	$("#pest_popup").css({ "left" : left + "px", "top" : top + "px", "z-index" : 1001 });
	$("#pest_popup").show();
	
	if (is_group)
		urlString = "AJAX=True&Group=" + seek_name;
	else
		urlString = "AJAX=True&Pest=" + seek_name;
	
	if (pop_type == "pest")
		ajaxURL = "./content/get_pest_pages.php";
	if (pop_type == "damage") {
		ajaxURL = "./content/get_damage_pages.php";
		urlString = "AJAX=True&Damage=" + seek_name;
	}

	$.ajax({
	    url: ajaxURL,
	    type: 'POST',
	    data: urlString,
	    success: function(data){
			$('#pest_popup_inner').html(data);
			$("#pest_popup_inner").css("background","white");
	    }
	});
		
	return $("#pest_popup").offset().left + $("#pest_popup").width() + mysteriousOffset;
}

function hidePestPop() {
	$("#pest_popup").hide();
	$("#pointer").hide();
	$("#content, #nav").unblock();
	$("#content").fadeTo('fast', 1.0);
}


