﻿(function() {
	
	String.prototype.startsWith = function(value) { return (this.match("^" + value) == value) };
	
})();

function popUpURL(url, message, size) {
	popUpURL(url, message, size, false);
}

function popUpURL(url, message, size, forceReload) {
    if (message == null)
        message = '';

    if (size == null)
        size = '';
        
	message = message.replace(' ', '');
	if (size != "") {
		if (size == 1)
			var properties = "height=210,width=" + ((window.screen.width) - 170) + ",location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,left=85,top=" + (window.screen.height / 4);
		else if (size == 2)
			var properties = "height=520,width=650,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,left=" + ((window.screen.width - 650) / 2) + ",top=" + ((window.screen.height - 520) / 4);
		else if (size == 3)
			var properties = "height=520,width=450,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,left=" + ((window.screen.width - 650) / 2) + ",top=" + ((window.screen.height - 520) / 4);
		else if (size == 5)
			var properties = "height=800,width=720,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,left=" + ((window.screen.width - 650) / 2) + ",top=" + ((window.screen.height - 520) / 4);
		else if (size == 6)
			var properties = "height=800,width=987,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,left=" + ((window.screen.width - 950) / 2) + ",top=" + ((window.screen.height - 800) / 4);
		else if (size == 7)
			var properties = "height=" + ((window.screen.height) -20) + ",width=" + ((window.screen.width) -20) + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,left=5, top=5";
		else if (size == 8)
			var properties = "height=748,width=1024,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,left=" + ((window.screen.width - 748) / 2) + ",top=" + ((window.screen.height - 1024) / 4);
		else
			var properties = "height=" + (window.screen.height / 2) + ",width=" + ((window.screen.width) - 200) + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,left=100,top=" + (window.screen.height / 4);
	}
	else
		var properties = "height=" + (window.screen.height / 2) + ",width=" + ((window.screen.width) - 200) + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,left=100,top=" + (window.screen.height / 4);



	var win = window.open(url, message, properties);

	if (win != 'undefined' && win != null) 
	{
		if (forceReload)
			win.location.reload();
			
		win.focus();
	}
}

function aceShown(sender, eventArgs) {

	var browser = BrowserDetect.browser;
	var version = BrowserDetect.version;

	var ace = sender._completionListElement;
	ace.style.width = "400px";

	var textField = sender._element;
	var tbposition = $common.getLocation(textField);

	var offSet = 0;

	switch (browser.toLowerCase()) {
		case 'explorer':
			break;

		case 'firefox':
			if (version == 2) {
				//alert(tbposition.y);
				if (tbposition.y > 500) {
					offSet = -351;
				}
				else if (tbposition.y > 220) {
					offSet = -3;
				}
			}
			break;
	}

	if (offSet != 0) {
		var totalOffset = tbposition.y + offSet;
		ace.style.top = totalOffset + "px";
	}
}

function fireBrowserEvent(control, eventName) {
	if (document.all) {
		control.fireEvent(eventName);
	}
	else {
		var customEvent = null;

		if (eventName.toString().indexOf('onClick', 0) > -1) {
			customEvent = window.document.createEvent('MouseEvent');
			customEvent.initEvent('click', false, true);
		}
		else if (eventName.toString().indexOf('onChange', 0) > -1) {
			customEvent = window.document.createEvent('HTMLEvents');
			customEvent.initEvent('change', false, true);
		}

		control.dispatchEvent(customEvent);
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}






var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
	var i;
	for (i = 0; i < s.length; i++) {
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag) {
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary(year) {
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
		if (i == 2) { this[i] = 29 }
	}
	return this
}

function isDate(dtStr) {
	if (dtStr == undefined || dtStr == null)
		return false;
		
	var daysInMonth = DaysArray(12)
	var pos1 = dtStr.indexOf(dtCh)
	var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
	var strDay = dtStr.substring(0, pos1)
	var strMonth = dtStr.substring(pos1 + 1, pos2)
	var strYear = dtStr.substring(pos2 + 1)
	strYr = strYear
	if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
	}
	month = parseInt(strMonth)
	day = parseInt(strDay)
	year = parseInt(strYr)
	if (pos1 == -1 || pos2 == -1) {
		return false;
	}
	if (strMonth.length < 1 || month < 1 || month > 12) {
		return false;
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
		return false;
	}
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
		return false;
	}
	if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
		return false;
	}
	return true;
}




function clickButton(e, buttonid) {
	var evt = e ? e : window.event;
	var bt = $('#' + buttonid);
	if (0 < bt.length && evt.keyCode == 13) {
		bt.trigger('click');
		if (bt.is('a')) {
			window.location = bt.attr('href');
		}
		return false;
	}
}

function UpdateImage(ctrl) {
	var img = document.getElementById(ctrl);
	if (img == null)
		img = $(ctrl)[0]; //try jquery
		
	var originalImage = img.src;
	img.src = originalImage;
}

function IsValidDate(dateStr) {

	//var datePattern = '/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2}|\d{4})$/';
	var datePattern = '(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d';

	var matchArray = dateStr.match(datePattern);
	//Check valid format
	if (matchArray == null) { return false; }

	day = matchArray[1];
	month = matchArray[3];
	year = matchArray[5];

	// check month range
	if (month < 1 || month > 12) { return false; }

	//Check day range
	if (day < 1 || day > 31) { return false; }

	//Check months with 30 days
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) { return false; }

	//Check Feb days
	if (month == 2) {
		var leapYr = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day > 28 && !leapYr)) { return false; }
	}

	return true;
}

function validateDOBInput(obj) {
	if (obj.value.length == 0) { return; }

	sDate = obj.value;

	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sDate)) {
		// now work out our min date....
		// min date is 18 years before today
		var d = new Date(sDate);

		d.setFullYear(sDate.substring(6, 10), sDate.substring(3, 5), sDate.substring(0, 2));

		var currentTime = new Date()
		var curyear = currentTime.getFullYear();
		var curmonth = currentTime.getMonth() + 1;
		var curday = currentTime.getDate();
		var aok = 0;

		if (d.getFullYear() < (curyear - 18) ||                                                  // if the entered year is less than the min year, reject
         d.getFullYear() <= (curyear - 18) && d.getMonth() < curmonth ||
         d.getFullYear() <= (curyear - 18) && d.getMonth() <= curmonth && d.getDate() < curday) {
			aok = -1;
		}

		if (aok != -1) {
			return;
		}
	}

	setTimeout("obj.focus();", 50);
	return;
}

function validateDOB(sender, args) {
	//obj = document.getElementById(sender.DOBid);

	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(args.Value)) {
		args.IsValid = true;
		return;
	}

	//setTimeout("obj.focus();", 50);
	args.IsValid = false;
	return;
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function validateIataCode(sender, args) {
	if (args.Value.length > 3) {
		args.IsValid = true;
		return;
	}

	if (args.Value.length == 3) {
		if (args.Value.match(/^[a-zA-Z]{3,}$/)) {
			args.IsValid = true;
		}
		else {
			args.IsValid = false;
		}
	}
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i = 0; i < gy.length; i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

// watermark
function watermark(textbox) {
	watermark_toggle(textbox, 1);
	textbox.blur(function() { watermark_blur(textbox) });
	textbox.focus(function() { watermark_focus(textbox) });
}

function watermark_blur(textbox) {
	if (textbox.val().length == 0)
		watermark_toggle(textbox, 1);
}

function watermark_focus(textbox) {
	watermark_toggle(textbox, 0);
}

function watermark_toggle(textbox, state) {
	if (state == 1) {
		textbox.css('color', '#aaa');
		textbox.css('font-style', 'italic');
		textbox.val(watermark_getDisplayText(textbox));
	} else {
		textbox.css('color', '');
		textbox.css('font-style', '');
		if (textbox.val() == watermark_getDisplayText(textbox))
			textbox.val('');
	}
}

function watermark_getDisplayText(textbox) {
	var text = textbox.attr('alt');
	if (text == '')
		text = textbox.attr('title');
		
	return text;
}

/* instant popup dialog */
function instantPopupDialog(selector) {
	var target = $(selector);
	if (0 < target.length) {
		var showDialog = false;
		if (target.not(':hidden')) {
			showDialog = true;
		}
		target.dialog({
			resizable: false,
			draggable: false,
			width: 330,
			minHeight: 50,
			modal: true,
			autoOpen: false,
			closeOnEscape: false,
			dialogClass: 'popup noClose',
			open: function(type, data) {
				$(this).parent().appendTo('form');
			}
		});
		if (showDialog) {
			target.dialog('open');
		}
	}
}

function moveWizardStep(dialog, step, width, height, title) {
	dialog.children().fadeOut('fast');
	var x = dialog.parent().offset().left;
	var y = dialog.parent().offset().top;
	var w = dialog.parent().width();
	var h = dialog.height();
	width = width == -1 ? w : width;
	height = height == -1 ? h : height;
	x = x + ((w - width) / 2);
	y = y + ((h - height) / 2);
	dialog.parent().animate({ width: width, left: x, top: y });
	dialog.animate({ height: height });
	dialog.dialog({ title: title });
	$('#step' + step, dialog).fadeIn('fast');
}

function sleep(milliseconds) {
	var start = new Date().getTime();
	for (var i = 0; i < 1e7; i++) {
		if ((new Date().getTime() - start) > milliseconds) {
			break;
		}
	}
}

DateUtils = function() {

	return {
	
		cascadeDates: function(dateText, source, target) {
			var dateParts = dateText.split('/');
			var dateTextUS = dateParts[1] + '/' + dateParts[0] + '/' + dateParts[2];
			var the_date = new Date(dateTextUS);
			the_date.addDays(1);
			target.datepicker('option', 'minDate', the_date);
			var defaultDate = new Date(the_date).addDays(6);
			target.datepicker('setDate', defaultDate);
			document.all ? source.get(0).fireEvent("onchange") : source.change();
		}	
		
	}
} ();
