// use jQuery 1.3.2
function getMetaContent(metaName, attrName){
    var defaultValue = '';
    var tag = jQuery('meta[name*=' + metaName + ']');
    if (tag) {
        var ctn = tag.attr(attrName);
        if (ctn) {
            return ctn;
        }
    }
    return defaultValue;
}

function getQueryParam(paramName){
    var sStr = location.search;
    var queryParams = sStr.substring(sStr.indexOf('?') + 1).split("&");
    for (var i = 0; i < queryParams.length; i++) {
        var param = queryParams[i].split('=');
        if (param[0] == paramName) {
            return param[1];
        }
    }
    return '';
}

function getPathParam(iPos){
    var path = window.location.pathname;
    var pos = path.lastIndexOf('/');
    if (pos > 0) {
        var pathParams = path.substring(1, pos).split("/");
        if (pathParams[iPos]) {
            return pathParams[iPos];
        }
    }
    return '';
}

function getStoreFrontName(){
    return getMetaContent('storefront', 'content');
}

function getLanguage(){
    return getMetaContent('language', 'content');
}

function show(elementid){
	var elt = jQuery('#'+ elementid);
    if (elt) {
        elt.show();
    }
}

function hide(elementid){
	var elt = jQuery('#'+ elementid);
    if (elt) {
        elt.hide();
    }
}

function loadJsFile(scripturl){
    document.write('<script src="', scripturl, '" type="text/JavaScript"><\/script>');
}


/*
 * Code to handle loading pixels with a timeout.   
 */ 
function loadPixelWithTimeout(pixelUrl, timeout) {
	if (document.pixels == null) {
		document.pixels = new Array();
	}
	if (document.pixelInitTimes == null) {
		document.pixelInitTimes = new Array();
	}
	if (document.pixelTimeouts == null) {
		document.pixelTimeouts = new Array();
	}
	
	idx = document.pixels.length;
	document.pixels[idx] = new Image(1,1);
	document.pixelInitTimes[idx] = new Date();	
	document.pixelTimeouts[idx] = timeout;
	document.pixels[idx].src = pixelUrl;
	
	pixelLoader_startChecking();
}

function pixelLoader_startChecking() {
	if (document.pixelLoaderStarted == null) {
		document.pixelLoaderStarted = "yes";
		setTimeout('pixelLoader_setSrcToNull()', 2500);
	}
}

function pixelLoader_setSrcToNull(pixelUrl) {
	now = new Date();
	callAgain = false;
	
	if (document.pixels != null) {
		for (var i = 0; i < document.pixels.length; i++) {
			if (document.pixels[i] != null) {
				callAgain = true;
				if (document.pixelInitTimes[i].getTime() + document.pixelTimeouts[i] < now.getTime()) {
					// needed to not force another request to the page your loading.
					document.pixels[i].src = "/skylights/images/pixelLoaderBlank.gif";
					document.pixels[i] = null;
				}
			}
		}
	}
	
	// iframe loading timeouts held in the loadTimeout attribute in the iframe tag
	var iframeObjs = document.getElementsByName("pixelFrame");
	if (iframeObjs != null) {
		for (var i = 0; i < iframeObjs.length; i++) {
		    var anIFrame = iframeObjs[i];
			if (anIFrame.initTime == null) {
				anIFrame.initTime = new Date();
				callAgain = true;
			}
			else if (anIFrame.finishedLoading == "true")
			{
				continue;
			}
			else if (anIFrame.src.indexOf("pixelLoaderBlank.html") == -1) {
				 var timeout = Number(anIFrame.getAttribute("loadTimeout"));
				 if (timeout < 1000 || timeout > 120000) {
				 	// sanity check on the timeout; set to 30 seconds if insane
				 	timeout = 30000;
				 }
				 
				 if (anIFrame.initTime.getTime() + timeout < now.getTime()) {
				 	anIFrame.src = "/global/pixelLoaderBlank.html";
				 }
				 else {
				 	callAgain = true;
				 }
			}
		}	
	}
	  
	if (callAgain) {
		setTimeout('pixelLoader_setSrcToNull()', 2500);
	}
}

//get sabre exposed webstats data
function getAnalyticsParam(paramName){
	var value = "";
	if(typeof analyticsText != "undefined") {
	        for (var field in analyticsText) {
			if(field == paramName) {
				value = analyticsText[field];
				break;
			}
		 }
	}
	return escape(value);
}

/*
 Replace any href in anchor tag with SABRE_SONIC_WEB_URL token
 with application configured SSW url value
*/
function replaceHrefSSWUrlToken(){
	jQuery("a[href^='https://SABRE_SONIC_WEB_URL']").each(function() {
    	this.href = this.href.replace(/https:\/\/SABRE_SONIC_WEB_URL\//i, sabreSonicWebUrl);
     });
}

/*
 Replace any href in anchor tag with WJ_DOT_COM_HOST token
 with application configured WestJet dot com host value
*/
function replaceHrefWJDotComHostToken(){
	jQuery("a[href^='https://WJ_DOT_COM_HOST']").each(function() {
    	this.href = this.href.replace(/https:\/\/WJ_DOT_COM_HOST\//i, wjDotComHost);
    });
}

/**
 Initialize form action to use application specified action handler for 3rd party 
 integration.
*/
function initFormActionHandler(frm, actionUrl){
     jQuery("form#" + frm).attr("action", actionUrl);
}

/*
Sabre's booking form has one field named "action" which causes issue when the form's
action attribute needs to be dynamically set. To resolve this issue, the action 
field was not included in the oringal form and dynamically added back once the form's 
action attribute was set.
*/
function addHiddenFieldToForm(frm, fldName, fldValue) {
     var frmField = '<input type="hidden" name="' + fldName + '" value="' + fldValue + '" />';
     jQuery("form#" + frm).append(frmField);
}

//get booking process
function getFlightBookingProcess(){
	var searchOD = undefined;
	var changeFee = undefined;
	var refundAmt = undefined;
	
	if(typeof analyticsText != "undefined") {
	     for (var field in analyticsText) {
			if(field == "searchCities") {
				searchOD = analyticsText[field];
			}
			if(field == "totalChangeFee") {
				changeFee = analyticsText[field];
			}
			if(field == "refundAmount") {
				refundAmt = analyticsText[field];
			}
		 }
	}
	
	if (typeof searchOD != "undefined") {
		if (typeof changeFee != "undefined") {
			return "exchange";
		} 
		else {
		 	return "createbooking";
		}
	}
	
	if (typeof refundAmt != "undefined") {
		return "refund";
	}
	return "";
}