//<script>
___AJAX_URL___ = 'http://www.flightlevel350.com/server.php?op=';
___SPACE___ = '&nbsp;';
___LOADING_ID___ = 'loading';

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/g,"");
}

function getIsValidEmail(email) {
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

	if(filter.test(email))
	return true;

	return false;
}

//Pop-UP
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}
function showLoading() {
	var elem = $(___LOADING_ID___);
	if (!elem) {
		elem = document.createElement("div");
		elem.id = ___LOADING_ID___;
		elem.style.border = "#000000 1px solid";
		elem.innerHTML = 'Loading ...';
		document.body.insertBefore(elem, document.body.firstChild);
	}
	elem.style.display = 'block';
}
function hideLoading() {
	var elem = $(___LOADING_ID___);
	if (elem) {
		elem.style.display = 'none';
	}
}


Ajax.Responders.register({
	onCreate: function() {
		Ajax.activeRequestCount++;
		showLoading();
	},

	onComplete: function() {
		Ajax.activeRequestCount--;
		hideLoading();
	}
});


var Message = Class.create();
Message.prototype = {
	_type: 'error', //error | warning | status
	_text: null,
	_msgPanelID: 'msgPanel_',
	_msgTitleID: 'msgTitle',
	_msgBodyContainerID: 'msgBodyContainer',
	_msgIconTypeID : 'msgIconType',
	_msgBodyID : 'msgBody',
	_containerID: null,

	initialize: function(type, text, containerID) {
		this.setType(type);
		this.setText(text);
		this.setContainer(containerID);
	},

	getID: function() {
		return this._msgPanelID + this._containerID;
	},

	setContainer: function(containerID) {
		this._containerID = containerID;
	},

	setText: function(text) {
		this._text = text;
	},

	getText: function() {
		return this._text;
	},

	setType: function(type) {
		if(type=='error' || type=='warning' || type=='status')
		this._type = type;
	},

	getType: function() {
		return this._type;
	},

	show: function() {
		var container = $(this._containerID);

		if(container) {
			var msgPanel = $(this.getID());

			if(!msgPanel) {

				var msgTitle = document.createElement('p');
				msgTitle.id = this._msgTitleID;
				msgTitle.innerHTML = 'ATTENTION!';

				var msgIconType = document.createElement('img');
				msgIconType.id = this._msgIconTypeID;
				msgIconType.alt = "ATTENTION!";
				msgIconType.title = 'ATTENTION!';

				var msgBody =  document.createElement('span');
				msgBody.id = this._msgBodyID;

				var msgBodyContainer = document.createElement('p');
				msgBodyContainer.id = this._msgBodyContainerID;
				msgBodyContainer.insertBefore(msgBody, msgBodyContainer.firstChild);
				msgBodyContainer.insertBefore(msgIconType, msgBodyContainer.firstChild);

				msgPanel = document.createElement(this.getID());
				msgPanel.id = this.getID();
				msgPanel.className = 'msgPanel';
				msgPanel.style.display = 'none';
				msgPanel.insertBefore(msgBodyContainer, msgPanel.firstChild);
				msgPanel.insertBefore(msgTitle, msgPanel.firstChild);

				container.insertBefore(msgPanel, container.firstChild);
			} else {
				var msgTitle = $(this._msgTitleID);
				var msgIconType = $(this._msgIconTypeID);
				var msgBody = $(this._msgBodyID);
				var msgPanel = $(this.getID());
			}

			msgTitle.style.backgroundImage = 'url(http://cdn-www.flightlevel350.com/images/message/title_' + this.getType() + '.gif)';
			msgIconType.src = "http://cdn-www.flightlevel350.com/images/message/icon_" + this.getType() + ".gif";
			msgBody.innerHTML = this.getText();
			Element.show(this.getID());
			msgPanel.focus();
		}

		/*<div id="msgPanel">
		<p id="msgTitle" style="background-image: url(path);" >
		ATTENTION!
		</p>
		<p id="msgBodyContainer">
		<img id="msgIconType" src="path" alt="ATTENTION!" title="ATTENTION!" />
		<span id="msgBody">
		the meessage
		</span>
		</p>
		</div>*/
	},

	hide: function() {
		var msgPanel = $(this.getID());
		if(msgPanel) {
			msgPanel.hide();
		}
	}
}

function tableRowColor(trID, orderClass) {
	var arr = $$('tr.row1', 'tr.row2');
	var i = 0;
	var className = '';

	arr.each(function(s, index) {
		s.style.backgroundColor = ''
		if(orderClass == true) {

			if((i % 2)==0) {
				className = 'row1';
			} else {
				className = 'row2';
			}

			s.className = className;

			i++;
		}
	});

	var tr = $(trID);
	if(tr) {
		tr.style.backgroundColor = '#8BD88B';
	}
}

function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;

	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
		break;
	}
	return null;
}
function setCookie(name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (2 < argc) ? argv[2] : null;
	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	domain = 'flightlevel350.com';
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}
function fromSelectToSelect(from, to, autoSelect) {
	var totalTo = to.options.length;
	var count = from.options.length;
	for(var i=0; i<count; i++) {
		if(from.options[i].selected==true) {
			to.options[totalTo] = new Option(from.options[i].text, from.options[i].value, autoSelect, autoSelect);
			totalTo++;
		}
		if(from.options[i].selected==true) {
			from.options[i] = null;
			count=count-1;
			i=i-1;
		}
	}
}
function sortSelect(obj) {
	var o = new Array();
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
	}
	if (o.length==0) { return; }
	o = o.sort(
	function(a,b) {
		if ((a.text+"") < (b.text+"")) { return -1; }
		if ((a.text+"") > (b.text+"")) { return 1; }
		return 0;
	});
	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}
function onConfirmDelete() {
	if(confirm("Are you sure you want to delete?")) {
		return true;
	} else {
		return false;
	}
}
function getExpireDate(d){
  var num = 14;
  if(d!='undefined' && d!=null)
    num = d;
	var expires = new Date();
	expires.setTime((new Date().getTime() + 1000*60*60*24*num));
	return expires;
}
function detectScreenSize(){
	setCookie("autoScreenWidth", screen.width, getExpireDate());
	setCookie("autoScreenHeight", screen.height, getExpireDate());
}
function testCookie(){
	var checkCookie = 'checkCookie' + (new Date().getTime());
	document.cookie = checkCookie + '=cookieValue';
	var yesCookie = document.cookie.indexOf(checkCookie) != -1;
	if (yesCookie){
		alert("Your browser supports cookies and JavaScript");
	}else{
		alert("Your browser does not support cookies");
	}
	deleteCookie(checkCookie);
}
function deleteCookie (cookieName) {
	var expire = new Date();
	document.cookie = cookieName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}
function hiddeDiv() {
  setCookie('hiddeDiv', 1, getExpireDate(60));
  document.getElementById('message__').style.display='none';
  document.getElementById('message2__').style.display='block';
}
