// JavaScript Document
var clientVerVar = "cv"; // E.g. "1.7", "3.0"
var clientTypeVar = "ct"; // E.g. "jv"=java, "sp"=smartphone (calculated from the value of "cp")
var clientPlatVar = "cp" // E.g. "java", "javs40", "javmoto", "s60", "sp03", "wm5", "uiq".
var clientMakeVar = "cm" // E.g. "nokia", "se", "orange" etc.
var clientModelVar = "cx" // E.g. "6680", "n70", "k750i" etc.
var phoneIdVar = "make" // E.g. "se_k750i";

var phoneId = null;
var phoneDetails = null;
var clientVersion = null;
var clientType = null;
var phonePlatform = null;
var phoneMake = null;
var phoneModel = null;

var nameOfConditionalElements = "conditional";
var nameOfHiddenClass = "hidden";

var queryVars = document.location.search.substring(1).split('&');
var url = new Object();

/* Function for opening the "Compatible phones" screen. */
function handsetpopup(){
    popupWin = window.open("/portal/tour.do?operation=handsets&amp;tourmode=tmimg", 
    "ShoZu","resizable=yes,titlebar=no,scrollbars=yes,width=800,height=800");
	popupWin.focus();
	return false;
}

// Add any URL variables to the "url" array.
for(i=0; i<queryVars.length; i++) {
	eq = queryVars[i].indexOf('=');
	url[queryVars[i].substring(0,eq)] = unescape(queryVars[i].substring(eq + 1));
}

// Automatically adds the client version and phone ID to links on the page.
function addClientInfoToLinks(clientVersion, phoneId) {
	// Don't add links if the client version and phone ID are undefined
	if (clientVersion == null && phoneId == null) {
		return false;
	}
	
	var allLinks = document.links;
	for (i=0; i<allLinks.length; i++) {
		
		var thisHref = document.links[i].href;
		
		// Skip links that already contain the infomation (such as anchor links on the current page).
		if (thisHref.indexOf(clientTypeVar + '=') > -1 || thisHref.indexOf(phoneIdVar + '=') > -1) {
			continue;
		}
		
		//Skip Javascript: links.
		if (thisHref.indexOf('javascript:') > -1) {
			continue;
		}

		// Determine whether the link aleady contains other URL variables
		var urlAppend = "";
		var urlVarSeparator = "";
		if (thisHref.indexOf('?') == -1) {
			urlVarSeparator = "?";
		} else {
			urlVarSeparator = "&";
		}
		
		if (clientVersion != null) {
			urlAppend+= urlVarSeparator + clientVerVar + "=" + clientVersion;
			urlVarSeparator = "&";
		}
		
		if (phoneId != null) {
			urlAppend+= urlVarSeparator + phoneIdVar + "=" + phoneId;
			urlVarSeparator = "&";
		}
		
		//Add client version and phone ID to the URL string before anchor link.
		if (thisHref.indexOf('#') == -1) {
			// Link doesn't contain an anchor tag so append the client version and type.
			document.links[i].href = thisHref + urlAppend;
		} else {
			var href = thisHref.split('#');
			document.links[i].href = href[0] + urlAppend + "#" + href[1];
		}
	}
	
	return true;
}

// Hides conditional help if it doesn't apply to the user's client version and phone.
function hideConditionalHelp(clientVersion, clientType, clientPlatform, clientMake, clientModel) {
	// If no client version or type is specified, don't hide anything.
	if (clientVersion == null && clientType == null && clientPlatform == null && clientMake == null && clientModel == null) {
		return false;
	}
	
	var aElements = document.body.getElementsByTagName("*");
	
	for (i=0; i<aElements.length; i++) {
		var element = aElements[i];
		
		var hideElement = false;
		var supportedClientVersions = element.getAttribute(clientVerVar);
		var supportedClientTypes = element.getAttribute(clientTypeVar);
		var supportedClientPlatforms = element.getAttribute(clientPlatVar);
		var supportedClientMakes = element.getAttribute(clientMakeVar);
		var supportedClientModels = element.getAttribute(clientModelVar);
		
		// Hide the element if the client version does not match.
		if (supportedClientVersions != null) {
			if (supportedClientVersions.indexOf(clientVersion) == -1) {
				hideElement = true;
			}
		}
		
		// Hide the element if the client type does not match.
		if (supportedClientTypes != null) {
			if (supportedClientTypes.indexOf(clientType) == -1) {
				hideElement = true;
			}
		}
		
		// Hide the element if the client platform does not match.
		if (supportedClientPlatforms != null) {
			if (supportedClientPlatforms.indexOf(clientPlatform) == -1) {
				hideElement = true;
			}
		}
		
		// Hide the element if the client make does not match.
		if (supportedClientMakes != null) {
			if (supportedClientMakes.indexOf(clientMake) == -1) {
				hideElement = true;
			}
		}
		
		// Hide the element if the client model does not match.
		if (supportedClientModels != null) {
			if (supportedClientModels.indexOf(clientModel) == -1) {
				hideElement = true;
			}
		}
		
		if (hideElement) {
			element.className= nameOfHiddenClass;
		}
	}
	
	return true;
}

function pageLoaded() {
	// If the phone ID is not given in the URL, get the phone make from the cookie, 
	phoneId = url[phoneIdVar];
	if (phoneId == null) {
		phoneId = getCookie("make");
	}
	
	var phoneName = "Unknown";
	
	// Check if the phone ID (e.g. "nokia_n70") is provided.
	if (phoneId == null || phoneId == "unknown") {
		// If the phone ID isn't provided, are some phone details available from the URL? (The phone platform is included in links from the provisioning wizard.)
		phonePlatform = url[clientPlatVar];
		if (phonePlatform != null) {
			phoneId = "unknown";
			clientVersion = "unknown";
			clientType = getClientTypeFromPlatform(phonePlatform);

			// If the phone make is provided in the URL, use it.
			if (url[clientMakeVar] != null) {
				phoneMake = url[clientMakeVar];
			} else {
				phoneMake = "unknown";
			}
			
			phoneModel = "unknown";
			hideConditionalHelp(clientVersion, clientType, phonePlatform.toLowerCase(), phoneMake.toLowerCase(), phoneModel.toLowerCase());
		} 
	} else {
		// Extract the phone details from the provided phone ID.
		phoneDetails = getPhoneDetails(phoneId);
		
		clientVersion = url[clientVerVar];
		clientType = getClientTypeFromPlatform(phoneDetails["platform"].toLowerCase());
		phonePlatform = phoneDetails["platform"].toLowerCase();
		phoneMake = getPhoneMake(phoneId);
		phoneModel = getPhoneModel(phoneId);
	
		addClientInfoToLinks(clientVersion, phoneId)
		hideConditionalHelp(clientVersion, clientType, phonePlatform.toLowerCase(), phoneMake.toLowerCase(), phoneModel.toLowerCase());

		// Create the phone name from the make and model.
		phoneName = phoneDetails["make"] + " " + phoneDetails["model"];
	}
	
	// Store the phone ID in a cookie. ("unknown" if the phone type is not known).
	setCookie("make", phoneId, 365, "shozu.com");
	
	// Display the user's phone type on the page.
	var pe = document.getElementById("phoneType");
	pe.innerHTML = "Your phone: <strong>" + phoneName + "</strong> (<a href=\"selectphone.html\">Change</a>)";
	
	// If some help has been hidden, anchor links may open at the old location. To fix this, scroll the anchor link into view.
	if (location.hash != "") {
		var anchorName = location.hash.substring(1, location.hash.length); // remove the hash '#' from the string.
		if (document.anchors[anchorName] != null) {
			document.anchors[anchorName].scrollIntoView(true);
		}
	}
}

function getPhoneMake(phoneId) {
	// Return if a phoneId is not provided.
	if (phoneId == null) {
		return;
	}
	
	var a = phoneId.split("_");
	
	return a[0];
}

function getPhoneModel(phoneId) {
	// Return if a phoneId is not provided.
	if (phoneId == null) {
		return;
	}
	
	var a = phoneId.split("_");
	
	return a[1];
}

function setCookie(name, value, days, domain) {
	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=/ ;domain=" + domain;
	document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var splitCookie = document.cookie.split(';');
	for(var i=0;i < splitCookie.length;i++) {
		var seg = splitCookie[i];
		while (seg.charAt(0)==' ') seg = seg.substring(1,seg.length);
		if (seg.indexOf(nameEQ) == 0) {
			return seg.substring(nameEQ.length,seg.length);
		}
	}
	return null;
}

function wipeCookie(name) {
	setCookie(name,"",-1);
}
