/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2010-09-30
	Status:		live
	Version:	2.6.0
	Build:		20111021
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20100930:	Neuvo
	20101120:	Added SetUrls
	20110329:	Added Duplicate
	20110524:	Added ClassPageX.Popup method
	20110608:	Added div.tip selector
	20111021:	Added ShowProcessing
	---------------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	execute when the DOM is ready...
	-----------------------------------------------------------------------	*/

	// Attach mouse events to top menus
	$(function () {
		$("#headmenu > ul > li:has(ul)").mouseenter(
				function () {
					$(this).addClass("atv");
					$("div", $(this)).show();
				}).mouseleave(
				function () {
					$(this).removeClass("atv");
					$("div", $(this)).hide();
				});
	});


	/* radio button validation...
	// see: http://static.flowplayer.org/tools/forum/60/44416
	$.tools.validator.fn(".optionrequired", function (input) {
		if ($(input).is(":checked")) {
			return { en: "Please make a selection" };
		} else {
			api.reset();
			return true;
		};
	});
	*/

	// Execute tooltips and validaion for forms using jquerytools
	$(function () {
		// select all desired input fields and attach tooltips to them
		$(".xfm :input[type != checkbox][title], div.tip").tooltip({
			position: "center right",
			offset: [-2, 10],
			effect: "fade",
			opacity: 0.7,
			formEvent: null
		});
		$(".xfm").validator({ offset: [-2, 10] });

		/* apply with radio button validation above
		api = $(".xfm").validator().data("validator");
		api.reset();
		*/
	});

/*	-----------------------------------------------------------------------	
	Initialisation
	-----------------------------------------------------------------------	*/
	var x_page = new ClassPageX();

/*	-----------------------------------------------------------------------	
	ClassPageX:	Page javascript class
	-----------------------------------------------------------------------	*/
function ClassPageX(dbg) {
	/*	Public properties	*/
	this.parameter = "";

	/*	Private properties	*/
	var _self = this;	// self allows for a method/property to be called internally
	var _id = "ClassPageX";
	var _pop, _pop_name = "xPop";
	var _doc = document;
	var _href = _doc.location.href;
	
	/*	Accessor methods	*/
	this.GetID = function() { return(_id); };
	
	/*	Public methods	*/
	this.Link = _Link;
	this.LinkPost = _LinkPost;
	this.PopUp = _Popup;
	this.Popup = _Popup;
	this.PopPrint = _PopPrint;
	this.PopFrame = _PopFrame
	this.GoUrl = _GoUrl;
	this.PasswordCheck = _PasswordCheck;
	this.SetMigration = _SetMigration;
	this.PriceSubmit = _PriceSubmit;
	this.Processing = _Processing;
	this.ShowProcessing = _ShowProcessing;
	this.GoDrop = _GoDrop;
	this.Duplicate = _Duplicate;
	
	function _Link(lnk) {
		var re = /link=\w*/gi;
		document.location.href = _href.replace(re, "link=" + lnk);
	}

	function _LinkPost(lnk, frm) {
		var re = /link=\w*/gi;
		_frm = _getFormByName(frm);
		_frm.action = _href.replace(re, "link=" + lnk);
		_frm.submit();
	}
	
	function _getFormByName(frmName) {
		var frm = document.getElementById(frmName);
		if (!frm)
			frm = document.forms[frmName];
		return frm;
	}

	function _Popup(ul) {
		_popup(ul, "");
	}
	
	function _PopPrint(ul) {
		var url = (ul)? ul : document.location.toString();
		url = url.concat("&device=print");
		_popup(url, "");
	}

	function _PopFrame(ul) {
		var url = (ul) ? ul : document.location.toString();
		url = url.concat("&device=popup");
		_popup(url, "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=720, height=800");
	}

	function _popup(url, setting) {
		_pop = window.open(url, _pop_name, setting);
		try { _pop.focus(); }
		catch (e) { _pop.focus(); }
	}

	function _GoUrl(it) {
		if (it == "top")
			it = "#topofpage";
		if (it != "")
			document.location.href = it;
	}
	
	function _PasswordCheck(inFld1, inFld2) {
		var root = "images/iBurstBusiness/";
		var jfl1 = $("#".concat(inFld1));
		var jfl2 = $("#".concat(inFld2));
		var jimg = $("img#password-check");
		if (jfl1.val() == jfl2.val()) {
			jimg.attr("src", root.concat("password_yes.gif"));
			return true;
		} else {
			jimg.attr("src", root.concat("password_no.gif"));
			return false;
		}
	}

	function _SetMigration(val, fld) {
		if (x_migrations != null) {
			var jopt = $('Option[Code=' + val + ']', x_migrations);
			$(":input[name='ProductFee']").val("R" + jopt.attr("ProductFee"));
			$(":input[name='OneTimeFee']").val("R" + jopt.attr("OneTimeFee"));
		}
	}

	function _PriceSubmit(frm, nme) {
		var ht = "Please choose an option";
		if (_Radio(nme, ht)) {
			$("form#" + frm).get(0).submit();
		}
	}

	function _getF(fi) { return $(":text#" + fi).get(0); }
	function _getP(fi) { return $(":password#" + fi).get(0); }
	function _getC(fi) { return $(":checkbox#" + fi).get(0); }
	function _getR(fn) { return $(":radio[name='"+ fn +"']"); }
	function _getS(fi) { return $("select#" + fi).get(0); }
	function _getT(fi) { return $("textarea#" + fi).get(0); }

	// Method:_Radio
	function _Radio(fn, ht) {
		var rt = false;
		_getR(fn).each(
			function (i) {
				if (this.checked) return !(rt = true);
			}
		);
		if (rt) return true;
		alert(ht);
		return false;
	}

	function _Processing() {
		var api = $(".xfm").data("validator");
		if (api) {
			if (api.checkValidity()) {
				_ShowProcessing();
			}
		}
	}

	function _ShowProcessing() {
		$("#processing").overlay({
			top: 'center',
			mask: {
				color: '#BBB',
				speed: 'slow', //200,
				opacity: 0.8
			},
			closeOnClick: false,
			load: true
		});
	}

	function _GoDrop(url, fd) {
		var ul = url.concat("&id=", fd.value);
		document.location = ul;
	}

	function _Duplicate(fld, src, tar) {
		var dup = $(fld).attr("checked");
		var jfm = $(fld).parents("form:first");
		$(":input", jfm).each(function (ndx) {
			var nme = $(this).attr("name");
			var pre = nme.substr(0, src.length);
			var suf = nme.substr(src.length);
			if (pre == src) {
				var vlu = $(this).val();
				var upd = $(":input[name='" + tar + suf + "']").val((dup) ? vlu : "");
			}
		});
	}

}






