
//Site specific constants
var AGENCY_CODE = "allincdisc";
var YOU_TUBE_WALK_THROUGH = "http://www.youtube.com/watch?v=xQnp4e9oFTE";
var GA_TRACKER_CODE = "UA-6693216-2";
var PRETTY_SITE_NAME = "Allinclusive-Discounts.com";


var PROGRAM_INSTAL_PATH = "/tools/";
var DELIMITER = ";;";
//var SERVLET_DIR = "http://www.iwanttolol.com/tools/";
var SERVLET_DIR = "/tools/";
//var PROXY_URL = "proxy.php?page=";
var PROXY_URL = "";

function gup( name, url, retval)
	{
	  if(!url) { url = window.location.href; }
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( url );
	  if( results == null ) {
		if(retval === undefined || retval.length == 0) {
			return "";
		} else if(retval.length == 1) {
			return retval[0];
		} else {
			return retval;
		}
	  } else {
		if(retval === undefined || retval == null) {
			retval = new Array();
			retval[0] = results[1];
		} else {
			retval[retval.length] = results[1];
		}
		
		return gup(name, url.substring(results.index + name.length), retval);
	  }
	}
	
getElementsByClassName = function(rootNode, cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = rootNode.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function getHotelList(destCode, dropdownId, activeHotel) {
	if(!destCode) {
		$('#'+dropdownId).parent().hide();
		return;
	}
	
	//clear exisitng options
	$('#'+dropdownId).html("");
	$('#'+dropdownId).append($("<option></option>").attr("value","").text("- Select a Hotel -"));
	
	//Added for sub destination
	var subDest=false;
	var areaCodes=new Array();
	if(destCode!=null && destCode.endsWith("SD")){
		subDest=true;
		for(i=0;i<subDestCodes.length;i++){
			var cd=subDestCodes[i][0];
			if(cd.substring(3,cd.length)==destCode.substring(0,3)) {
				destCode=cd.substring(0,3);
				if(subDestCodes[i][1]!=null){
					areaCodes=subDestCodes[i][1].split(",");
				}	
			}	
		}
	}
	//Ends
	
	//hotelCodes loaded from hotels.js
	var foundTheStart=false;
	for(var i=0;i<hotelCodes.length;i++) {
		if(hotelCodes[i][0]==destCode) {
			if(!foundTheStart) {
				foundTheStart=true;
			}
			if(subDest) {
				if(areaCodes.contains(hotelCodes[i][3])) {
					if(activeHotel == hotelCodes[i][1]) {
						$('#'+dropdownId).append($("<option></option>").attr("selected", true).attr("value",hotelCodes[i][1]).text(hotelCodes[i][2]));
					} else {
						$('#'+dropdownId).append($("<option></option>").attr("value",hotelCodes[i][1]).text(hotelCodes[i][2]));
					}
				}
			} else {
				if(activeHotel == hotelCodes[i][1]) {
					$('#'+dropdownId).append($("<option></option>").attr("selected", true).attr("value",hotelCodes[i][1]).text(hotelCodes[i][2]));
				} else {
					$('#'+dropdownId).append($("<option></option>").attr("value",hotelCodes[i][1]).text(hotelCodes[i][2]));
				}
			}
		} else if(foundTheStart) {
			//If it isn't a match and we have started then short circuit
			//	since the list is sorted
			break;
		}
	}
	$('#'+dropdownId).parent().show();
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
	  return false;
   else
	  return true;
}

function setCookie(name,value,days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
function setVideoClosedCookie() {
	setCookie("videoClosed",true, 14);
}
function getVideoClosedCookie() {
	return getCookie("videoClosed");
}
function setGatewayCodeInCookie(gatewayCode) {
	setCookie("gatewayCode",gatewayCode, 365);
}
function getGatewayCodeFromCookie() {
	return getCookie("gatewayCode");
}
function setDeptDateInCookie(deptDate) {
	setCookie("deptDate",deptDate, 14);
}
function getDeptDateFromCookie() {
	return getCookie("deptDate");
}
function setRetDateInCookie(retDate) {
	setCookie("retDate",retDate, 14);
}
function getRetDateFromCookie() {
	return getCookie("retDate");
}
function isIELessThan8() {
	if(YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
		return true;
	} else {
		return false;
	}
}
function convertLongCountryCodes(countryCode) {
	if(countryCode.length > 3) {
		var countryStart = countryCode.substring(0, 2).toUpperCase();
		if(countryStart == "MB") {
			countryCode = "MBJ";
		} else if(countryStart == "CU") {
			countryCode = "CUN";
		}
	}
	return countryCode;
}

function isUndefined(myVar) {
	if(typeof(myVar) !== 'undefined') {
		return false;
	} else {
		return true;
	}
}

function validateEmail(email) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return re.test(email);
}

function validatePhone(phone) {
	var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
	return regexObj.test(phone);
}

function printYear() {
	var d = new Date();
	document.write(d.getFullYear());
}

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
	if (this[i] === obj) {
	  return true;
	}
  }
  return false;
}
String.prototype.endsWith = function(str) {
    var lastIndex = this.lastIndexOf(str);
    return (lastIndex != -1) && (lastIndex + str.length == this.length); 
}
String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
}
Number.prototype.formatMoney = function(c, d, t){
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
