/*
Prager Microsystems Voice Analytics v1.5
http://www.pragermicrosystems.com/

Copyright 2011 Prager Microsystems, Inc.

Date: Dec 12, 2011
Author: Matt Nye
*/

function GetReferringHost() {
	var refer = document.referrer;
	if (refer) {
		var parts = refer.split("/");
		var output = parts[2];
		// ***add clean host search (e.g., www.google.com >> search.google.com)***
	} else {
		var output = "No Referrer";
	}
	return output;
}
function GetSource() {
	var query = window.location.search;
	var parts = query.split("?");
	query = parts[1];
	// check for site referral
	if (query) {
		// get key/value pairs
		var pairs = query.split("&");
		
		// create key/value array
		var val = new Array();
		for(var i=0; i<pairs.length; i++) {
			parts = pairs[i].split("=");
			val[parts[0]] = parts[1];
		}
		if (val['utm_source']) {
			var output = val['utm_source'];
		} else {
			var output = "No Referrer";
		}
	} else {
		var output = "No Referrer";
	}
	return output;
}
function CheckReferringQueryString(term) {
	var refer = document.referrer;
	var parts = refer.split("?");
	var query = parts[1];
	
	// check for url and remove ***replace with clean query array***
	if (query) {
		query = query.replace("&url=http%3A%2F%2Fwww.winnerairportparking.net%2F","");
	}
	
	var regex = new RegExp(term,"i");
	var isBrand = regex.test(query);
	if (isBrand == true) {
		return true;
	} else {
		return false;
	}
}
function CheckBrand() {
	function CheckBrandReferringQueryString(term) {
		var refer = document.referrer;
		var parts = refer.split("?");
		var query = parts[1];
		
		// check for url and remove ***replace with clean query array***
		if (query) {
			query = query.replace("&url=http%3A%2F%2Fwww.winnerairportparking.net%2F","");
		}
		
		var regex = new RegExp(term,"i");
		var isBrand = regex.test(query);
		if (isBrand == true) {
			return true;
		} else {
			return false;
		}
	}
	var keyword = new Array();
	keyword[0] = "winner"; // anything including "winner"
	
	for(var i=0; i<keyword.length; i++) {
		var output = CheckBrandReferringQueryString(keyword[i]);
		if (output == true) break;
	}
	return output;
}
function CheckPaid() {
	function CheckCurrentQueryString(term) {
		var query = window.location.search;
		
		var regex = new RegExp(term);
		var isPaid = regex.test(query);
		if (isPaid == true) {
			return true;
		} else {
			return false;
		}
	}
	var paid_id = new Array();
	paid_id[0] = "gclid"; // google id
	paid_id[1] = "utm_source"; // yahoo id
	paid_id[2] = "GOO-SE"; // alternate google id
	paid_id[3] = "OVR-SE"; // alternate yahoo id
	paid_id[4] = "MSN-SE"; // alternate bing id
	
	for(var i=0; i<paid_id.length; i++) {
		var output = CheckCurrentQueryString(paid_id[i]);
		if (output == true) break;
	}
	return output;
}
function GetKeyword() {
	var refer = document.referrer;
	// get host
	var parts = refer.split("/");
	var host = parts[2];
	//// ***add clean host search (e.g., www.google.com >> search.google.com)***
	
	// get key/value pairs
	parts = refer.split("?");
	var query = parts[1];
	var pairs = query.split("&");
	
	// create key/value array
	var val = new Array();
	for(var i=0; i<pairs.length; i++) {
		parts = pairs[i].split("=");
		val[parts[0]] = parts[1];
	}
	
	// set keyword
	switch (host) {
		case "search.yahoo.com":
			var output = val['p'];
			break;
		default:
			var output = val['q'];
	}
	return unescape(output.replace(/\+/g," ")); // decode keyword

}
function CreateCookie(name,value,days,path,domain) {
	// set expire
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(1000*60*60*24*days));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	// set path
	if (path) {
		var pth = "; path="+path;
	} else var pth = "";
	// set domain
	if (domain) {
		var dmn = "; domain="+domain;
	} else var dmn = "";
	
	document.cookie = name+"="+value+expires+pth+dmn;
}
function ReadCookie(name) {
	var nameEQ = name+"=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while(c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function EraseCookie(name) {
	CreateCookie(name,"",-1);
}

// check source
// ***add clean host search (e.g., www.google.com >> search.google.com)***
var refHost = GetReferringHost();
switch (refHost) {
	case "www.google.com":
	case "search.yahoo.com":
	case "www.bing.com":
	case "search.aol.com":
	case "www.ask.com":
		var isSearch = true;
		break;
	default:
		var isSearch = false;
}
var va_source = GetSource();
var isBrand = CheckBrand();
var isPaid = CheckPaid();

// set keyword
/*if (isSearch == true) {
	var va_keyword = GetKeyword();
} else {
	var va_keyword = "No Keyword Set";
}*/

// set medium
//// site
if (isSearch == false && va_source == "about-airport-parking") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="aboutairport" />';
} else if (isSearch == false && va_source == "long-term-parking") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="longtermpark" />';
} else if (isSearch == false && va_source == "parking-reservations") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="apr" />';
} else if (isSearch == false && va_source == "ridefly") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="ridefly" />';
} else if (isSearch == false && va_source == "win10promo-merged") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="win10promo" />';
} else if (isSearch == false && va_source == "winner-6.48-promo-merged") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="win6.48promo" />';
} else if (isSearch == false && va_source == "winner-7.74-promo-merged") {
	var va_home = '<div id="site" class="right-call"></div>';
	var va_print = '';
	var va_promo = '<input type="hidden" name="res_promo_code" value="promo7.74" />';

//// organic brandless
} else if (isSearch == true && isPaid == false && isBrand == false) {
	var va_home = '<div id="org" class="right-call"><div id="print"><a href="print-coupon.php">Print Coupon</a></div></div>';
	var va_print = '<img src="_img/img_winner-coupon-org.jpg" />';
	var va_promo = '<input type="hidden" name="res_promo_code" value="ORGPHL6.65" />';

//// paid
} else if (isSearch == true && isPaid == true) {
	var va_home = '<div id="ppc" class="right-call"><div id="print"><a href="print-coupon.php">Print Coupon</a></div></div>';
	var va_print = '<img src="_img/img_winner-coupon-ga.jpg" />';
	var va_promo = '<input type="hidden" name="res_promo_code" value="" />';

//// direct + organic branded
} else {
	var va_home = '<div id="per" class="right-call"><div id="print"><a href="print-coupon.php">Print Coupon</a></div></div>';
	var va_print = '<img src="_img/img_winner-coupon-10.jpg" />';
	var va_promo = '<input type="hidden" name="res_promo_code" value="WIN10PERPHL" />';
}

// check for cookie or set cookie
var c1 = ReadCookie("va_home");
var c2 = ReadCookie("va_print");
var c3 = ReadCookie("va_promo");
if (c1 || c2 || c3) {
	va_home = c1;
	va_print = c2;
	va_promo = c3;
} else {
	var expire = 180; // 6 months
	CreateCookie("va_home",va_home,expire,"/",".winnerairportparking.net");
	CreateCookie("va_print",va_print,expire,"/",".winnerairportparking.net");
	CreateCookie("va_promo",va_promo,expire,"/",".winnerairportparking.net");
}
