/* Handset Compatibility Tool
 *
 * Fido Solutions Inc. / Revolver 3
 * 2008-07-02
 */
var phone_param = "pcs_model";
var html = '';
function HC(phone_id)
{
	if (typeof(hc_config) == "undefined" || phone_id === undefined) return false;

	/* this.phones = eval(hc_config); */
	this.phones = hc_config;
	this.phonelist = hc_phonelist;
	this.phonelist.sort();
	if (lang === undefined || hc_avail_lang.indexOf(lang) == -1) lang = hc_lang_default;
	this.lang = lang;
	this.phone_id = phone_id;
	this.phone = this.parseInfo();
}

HC.prototype.parseInfo = function()
{
	return this.phones[this.phone_id];
}

HC.prototype.showInfo = function(info_id, phone_id)
{
	if (info_id === undefined || (info_id !== "name" && this.phone === undefined)) return "";
	
	if (phone_id == null && this.phone !== undefined) {
		value = this.phone[info_id];
	} else if (this.phones[phone_id] !== undefined) {
		value = this.phones[phone_id][info_id];
	} else {
		return "";
	}

	if (value === true || value === false) {
		// value if boolean
		value = (value) ? hc_boolean_true : hc_boolean_false;
	} else if (typeof(value) == "object") {
		// value is object, grab appropriate lingual value
		value = value[lang];
	} else if (value === undefined) {
		value = "";
	}
	return value;
}

HC.prototype.showPhoneImg = function()
{
	img = this.showInfo("img");
	url = this.showInfo("url");
	
	/* replace %s pattern in URL to insert language code */
	url = url.replace("%s", this.lang);
	
	name = this.showInfo("name");
	string = "";
	
	if (img !== "" && name !== "") {
		string = '<img src="' + img + '" alt="' + name + '" width="' + hc_image_width + '" border="0" />';
		if (url !== "") string = '<a title="' + name + '" href="' + url + '" target="_blank">' + string + '</a>';
	}
	return string;
}

HC.prototype.showPhoneList = function(label)
{
	if (label === undefined) label = "";
	var n = this.phonelist.length;
	var str = "";
	for (i=0; i<n; i++) {
		phone_id_temp = this.phonelist[i];
		str += '<option value="'+phone_id_temp+'"' + (this.phone_id==phone_id_temp ? ' selected="selected"' : '') + '>' + this.showInfo("name", phone_id_temp) + "</option>\n";
	}
	str = '<select name="phone" onchange="hc.loadPhone(this)"><option value="">' + label + '</option>' + str + '</select>';
	return str;
}

HC.prototype.loadPhone = function(o)
{
	var i = o.selectedIndex;
	var phone_id = o.options[i].value;
	if (phone_id === undefined || phone_id == "") return false;
	
	var newlocation = gnl(phone_id);
	window.location.href = newlocation;
	return true;
}

function hc_getPhone()
{
	phone_id = gup(phone_param);
	if (phone_id == "") phone_id = hc_phone_default;
	return phone_id;
}
/* get url param */
function gup(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec( window.location.href );

	if( results == null )
		return "";
	else
		return results[1];
}
/* get new phone href location */
function gnl(phone_id)
{
	w = window.location.href;
	current_phone_id = gup(phone_param);
	
	// if p param exists in url, replace. Otherwise, plain new url, append ?p to it
	if (current_phone_id === "") {
		// no phone detected, check if params already in url - append & or ? character
		append_char = (w.indexOf("?") == -1) ? "?" : "&";
		url = w + append_char + phone_param + "=" + phone_id;
	} else {
		// replace first occurrence of current_phone_id by new phone_id
		url = w.replace(current_phone_id, phone_id);
	}
	return url;
}

