/**
 * title: inix.js
 * purpose: Implements an object for managing the xmpl inix procedure and kickstarting the system.
 * licenses: 
 * This script uses jQuery 1.3.2 under the MIT License:
 * Copyright (c) 2009 John Resig, http://jquery.com/
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * This script uses jQuery UI 1.7.1 under the MIT License:
 * Copyright (c) 2009 Paul Bakaus, http://jqueryui.com/
 *
 * This software consists of voluntary contributions made by many
 * individuals (AUTHORS.txt, http://jqueryui.com/about) For exact
 * contribution history, see the revision history and logs, available
 * at http://jquery-ui.googlecode.com/svn/
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
function inix(_para) {
	// properties
	this.para = _para;
	this.objs = {};
	// methods
	this.init = inix_init;
}
function inix_init() {
	var _obj = this;
	// ajax
	{
		jQuery.ajaxSetup(
			{
				"type"			: "POST",
				"dataType"	: "json"
			}
		);
		$("body").ajaxComplete(
			function(_even,_xmlh,_ajax) {
				switch(_xmlh.responseText) {
					case _obj.objs["cons"].cons["XSES"]["vars"] :
						location.href = "/xbaby/core/login.aspx?sess=1";
						break;
				}
			}
		);
	}
	// cons
	{
		(this.objs["cons"] = new cons()).load();
	}
	// extd
	{
		jQuery.extend(
			{
				"fpar" : 
					function(_feld) {
						var _pmap = {};
						jQuery.each(_feld,
							function(_i,_v) {
								_pmap[_v["name"]] = _v["value"];
							}
						);
						return _pmap;
					},
				"xlnk" :
					function(_para) {
						_para["subj"].css({"cursor":"pointer"}).each(
							function() {
								$(this).click(
									function(_e) {
										if(_para["clbk"])
											_para["clbk"]($(this))
									}
								);
							}
						);
						if(array_key_exists("ulin",_para) && _para["ulin"])
							_para["subj"].css({"textDecoration":"underline"});
						if(array_key_exists("hlit",_para) && _para["hlit"])
							_para["subj"].css({"color":"#98aa66"});
						return _para["subj"];
					},
				"vald" :
					function(_para) {
						var _typs;
						for(var ii in (
							_typs = 
								{
									"emal" : {"ptrn":/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,"hmes":"Please enter a properly formatted email address (test@example.com)."},
									"pass" : {"ptrn":/^([a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\-\_\=\+\;\:\'\"\,\<\.\>\/\?\|\\]){8,20}$/,"hmes":"A password had to be between 8 and 20 characters and can only consist of letters, numbers, and the following characters: !@#$%^&*()-_=+;:'\",<.>/?|\\"},
									"cont" : {"ptrn":/^(.)+$/,"hmes":"Please enter a properly formatted country name (example: United States)."},
									"addr" : {"ptrn":/^(.)+$/,"hmes":"Please enter a properly formatted street address (example: 100 Main St)."},
									"city" : {"ptrn":/^(.)+$/,"hmes":"Please enter a properly formatted city (example: Los Angeles)."},
									"stat" : {"ptrn":/^[1-9]$/},
									"zcod" : {"ptrn":/^[0-9]{5}$/,"hmes":"Please enter a properly formatted zip code (example: 12345)"},
									"mont" : {"ptrn":/^(1([0-2])?)|([2-9])$/},
									"date" : {"ptrn":/^([1-9])|(1([0-9])?)|(2([0-9])?)|(3([0-1])?)$/},
									"year" : {"ptrn":/^(19|20)[0-9]{2}$/},
									"fnam" : {"ptrn":/^[a-zA-Z\s]{2,30}$/,"hmes":"The first name has to be between 2 and 30 characters and can only consist of letters and spaces (example: John)."},
									"lnam" : {"ptrn":/^[a-zA-Z\'\s]{2,30}$/,"hmes":"The last name has to be between 2 and 30 characters and can only consist of letters and spaces (example: Smith)."}
								}
							))
							if(ii == _para["type"]) {
								if(!jQuery.trim(_para["valu"])) {
									if(!array_key_exists("blnk",_para) || !_para["blnk"])
										return ret(0,array_key_exists("hmes",_typs[ii]) ? _typs[ii]["hmes"] : "");
									return ret(1);
								}
								return jQuery.trim(_para["valu"]).match(_typs[ii]["ptrn"]) ? ret(1) : ret(0,_typs[ii]["hmes"]);
							}
						return ret(0,"");
					},
				"wtab" :
					function(_para) {
						return '<table style="width:100%;height:100%" cellspacing="0" cellpadding="0">\
											<tr>\
												<td align="center" valign="middle">' + _para["xml"] + '</td>\
											</tr>\
										</table>';
					},
				"ximg" :
					function(_c,_i,_u) {
						return _u ? _inix.objs["cons"].cons["URLX"] + '/img/' + _c + '/' + _i : '<img src="' + _inix.objs["cons"].cons["URLX"] + '/img/' + _c + '/' + _i + '"/>';
					},
				"smes" :
					function(_para) {
						var _xml = "";
						_xml += '	<div>';
						switch(_para["mode"]) {
							case 1 :
								_xml +='	<div><b>' + _para["mesa"] + (_para["ecod"] != 0 ? ' (' + _para["ecod"] + (_para["loca"] ? ',' + _para["loca"] : '') + ')' : '') + '</b></div>';
								break;
							case 2 :
								_xml += '	<div>\
														<img src="' + jQuery.ximg("gene",_para["ecod"] == 0 ? "succ.png" : "fail.png",true) + '"width="10"/>\
														<b>' + _para["mesa"] + (_para["ecod"] != 0 ? ' (' + _para["ecod"] + (_para["loca"] ? ',' + _para["loca"] : '') + ')' : '') + '</b>\
													</div>';
								break;
							case 3 :
								_xml += '	<div>\
														<center>\
															<img src="' + _obj.objs["cons"].cons["URLX"] + '/xbaby/imag/gene/' + (_para["ecod"] != 0 ? 'fail' : 'succ') + '.png"/>\
														</center>\
														<div><b>' + _para["mesa"] + (_para["ecod"] != 0 ? ' (' + _para["ecod"] + (_para["loca"] ? ',' + _para["loca"] : '') + ')' : '') + '</b></div>' +
														(_para["mesb"] ?
															'<div>' + _para["mesb"] + '</div>'
															:
															''
														) + '\
													</div>';
								break;
						}
						_xml += '	</div>';
						return _xml;
					},
				"cmes" :
					function(_mode,_type) {
						return jQuery.smes({"mode":_mode,"ecod":_obj.objs["cons"].gerr(_type,"c"),"mesa":_obj.objs["cons"].gerr(_type,"m")});
					},
				"wait" :	
					function(_para) {
						switch(_para["wait"] ? 1 : 2) {
							case 1 :
								_para["suba"].hide();
								_para["subb"].show();
								break;
							case 2 :
								_para["suba"].show();
								_para["subb"].hide();
								break;
						}
					},
				"xmpb" :
					function(_para) {
						$("[xfco='" + _para["xfco"] + "'][bran='xmpb']").each(
							function() {
								(new xmpb(
									{
										"subj" : $(this),
										"prof" : $(this).attr("prof"),
										"labl" : $(this).attr("labl"),
										"xnam" : $(this).attr("xnam"),
										"crid" : $(this).attr("crid"),
										"c_ck" : array_key_exists("clbk",_para) ? _para["clbk"] : ""
									}
								)).init().draw();
							}
						);
					},
				"rema" :
					function(_para) {
						_para["suba"].keyup(
							function() {
								var _rema;
								switch((_rema = $(this).attr("maxlength") - $(this).val().length) < 0  ? 1 : 2) {
									case 1 :
										$(this).val(_para["suba"].val().substring(0,$(this).attr("maxlength")));
										$(this).trigger("keyup");
										break;
									case 2 :
										_para["subb"].html(_rema);
										break;
								}
							}
						).blur(
							function() {
								$(this).trigger("keyup");
							}
						).trigger("keyup");
					},
				"sizr" :
					function(_xml) {
						$("body").append('<div id="sizr" style="position:absolute;left:0px;top:0px;border:1px solid #000000">' + _xml + '</div>');
						var _dims = {"w":$("#sizr").width(),"h":$("#sizr").height()};
						$("#sizr").remove();
						return _dims;
					},
				"kase" :
					function _xfra_kase(_arry,_ksep,_lsep) {
						var _serz = "";
						{
							_ksep = _ksep ? _ksep : ":";
							_lsep = _lsep ? _lsep : ";";
						}
						jQuery.each(_arry,
							function(_k,_v) {
								_serz += (jQuery.trim(_serz) ? _lsep : "") + _k + _ksep + _v;
							}
						);
						return _serz;
					},
				"hlit" :
					function(_para) {
						return _para["rstr"].replace(eval('/('+_para["hlit"]+')/gi'),'<span style="font-weight:bold;color:#ff0000">$1</span>');
					},
				"sxmp" :
					function(_para) {
						_para["xmpb"].each(
							function() {
								(new xmpb(
									{
										"subj" : $(this),
										"prof" : $(this).attr("prof"),
										"xnam" : $(this).attr("xnam"),
										"labl" : $(this).attr("labl"),
										"c_ck" :
											function(_xpar) {
												_para["clbk"](_xpar);
											}
									}
								)).init().draw();
							}
						);
					},
				"bfex" :
					function(_para) {
						flashwid(_para.split(":")[0]);
						$("#diag").html(_para.split(":")[1]).dialog("open");
					}

			}
		);
	}
	// diag
	{
		$("#diag").dialog(
			{
				"autoOpen"	: false,
				"draggable"	: false,
				"resizable"	: false,
				"modal"			: true,
				"buttons"		:
					{
						"OK" :
							function() {
								$(this).dialog("close");
							}
					}
			}
		);
	}
}
