//-------------------------------------------------------------------------------------------------------------------------
// TOOLS REPLACEMENT - REQUESTS CF PAGE, STRIPS OUT META HEADER IF PRESENT, EVALS JS, CONTINUES TO FORMER UNCHANGED CODE
//-------------------------------------------------------------------------------------------------------------------------

var scripts = document.getElementsByTagName("script");
var scriptSrc = scripts[scripts.length - 1].src;
var phone = scriptSrc.split("phone=")[1];

var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
request.open("GET", "/js/phones_where-to-buy.cfm?phone=" + phone, false);
request.send(null);

var xmlSource = request.responseText;
xmlSource = request.responseText.split("<META NAME=\"ColdFusionMXEdition\" CONTENT=\"ColdFusion DevNet Edition - Not for Production Use.\">").join("");

//-------------------------------------------------------------------------------------------------------------------------

function model(id, name, status, countries)
{
   this.id = id;
   this.name = name;
   this.status = status;
   this.countries = countries;
}

function country(id, name, carriers)
{
   this.id = id;
   this.name = name;
   this.carriers = carriers;
}

function carrier(id, name, url, phone, buyonlineonly)
{
   this.id = id;
   this.name = name;
   this.url = url;
   this.shortUrl = url.split("/")[0];
   this.phone = phone;
   this.buyonlineonly = buyonlineonly;
}

eval(xmlSource);

//-------------------------------------------------------------------------------------------------------------------------

tData_StrContent = '';

//Verify we have data
if (typeof tData_models != 'undefined') {

	//Check to see if the model list is greater than 1 item. In the 
	//case of only 1 item, set the singleModelOnly var to true. Subsequent
	//functions will alter layout based on this.
	singleModelOnly = (tData_models.length == 1) ? true : false;
	
	//Construct Link block
	if (!singleModelOnly) { //hide navigation for single model only situations
		tData_StrContent += 'Click a model to view service providers.<br /><br />'
		for (n=0;n < tData_models.length; n++) {
			tData_StrContent += '<span class="smallprint"><a href="#' + tData_models[n].name + '">' + tData_models[n].name + '</a>&nbsp;<img src="/image/phones/showcase-arrow-red.gif" class="inline" />&nbsp;&nbsp;&nbsp;</span>';
            if (n % 5 == 0 && n != 0)
            {
                tData_StrContent += "<br/>";
            }
		}
	}
	
	//Setup Provider list Table
	tData_StrContent +=	'<table border="0" cellpadding="0" cellspacing="0" width="370">' +
						'<tr><td width="210" height="10"></td>' +
							'<td width="20"></td>' +
							'<td width="140"></td>' +
							'</tr>';
	
	//Begin Model List code block
	for (n=0;n < tData_models.length; n++) {
	
	//Create Model header
	tData_StrContent +=	'<tr><td height="10"></td></tr>';
	if (!singleModelOnly) { //hide Header row for single model only situations
		tData_StrContent +=	'<tr><td colspan="3" class="hd2Phones">Model ' + tData_models[n].name + '<a name="' + tData_models[n].name + '">&nbsp;</a></td></tr>' +
							'<tr><td height="7"></td></tr>';
	}
	
    if (tData_models[n].countries.length == 0)
    {
        if (tData_models[n].status == "C")
        {
            tData_StrContent +=	'<tr><td colspan="3" class="copy">The ' + tData_models[n].name + ' phone is no longer available for purchase. If you own a ' + tData_models[n].name + ', Kyocera will continue to provide information and support for this cell phone.</td></tr>';
        }
        else
        {
            tData_StrContent +=	'<tr><td colspan="3" class="copy">This phone is not yet available for purchase.</td></tr>';
        }
        tData_StrContent +=	'<tr><td height="7"></td></tr>';
    }

	//Open left column
	tData_StrContent += '<tr><td valign="top" class="copy">';
		
		//Divide model list for Left/Right column display
		leftColumnLimit = Math.ceil(tData_models[n].countries.length/2);
		
		//Create Country header - LEFT COLUMN
	    for (n2=0; n2 < leftColumnLimit ; n2++) {
			var country = tData_models[n].countries[n2];
			tData_StrContent +=	'<b>' + country.name + '</b><br />';
			

			//Create Service Provider listing
			for (var n3=0; n3 < country.carriers.length ; n3++) {
				var carrier = country.carriers[n3];
				(carrier.url.indexOf('mtnl.net.in') == -1) ? externalURL = "/external/?url=http://" : externalURL = "http://";

                if (carrier.buyonlineonly == 2)
                {
                    tData_StrContent +=	'<ul class="bulletListPhone" style="margin-bottom:0px;"><li style="margin:0px;">' + carrier.name + '</li></ul>' +
                        'Available online with <a href="buy-online.htm">LetsTalk</a> and many retail stores.<br /><br />';
                }
                else
                {
				tData_StrContent +=	'<ul class="bulletListPhone" style="margin-bottom:0px;"><li style="margin:0px;">' + carrier.name + '</li></ul>' +
							'<a href="' + externalURL + carrier.url + '" target="_blank">' + carrier.shortUrl + '</a><br />' +
							carrier.phone +'<br /><br />';
		}
			}
		}
	
		//Close left column, add spacer cell and open right column;
		tData_StrContent +=	'</td>' +
							'<td></td>' +
							'<td valign="top" class="copy">';

		//Create Country header - RIGHT COLUMN
	    for (n2=leftColumnLimit; n2 < tData_models[n].countries.length ; n2++) {
			var country = tData_models[n].countries[n2];
			tData_StrContent +=	'<b>' + country.name + '</b><br />';
			

			//Create Service Provider listing
			for (var n3=0; n3 < country.carriers.length ; n3++) {
				var carrier = country.carriers[n3];
				(carrier.url.indexOf('mtnl.net.in') == -1) ? externalURL = "/external/?url=http://" : externalURL = "http://";

                if (carrier.buyonlineonly == 2)
                {
                    tData_StrContent +=	'<ul class="bulletListPhone" style="margin-bottom:0px;"><li style="margin:0px;">' + carrier.name + '</li></ul>' +
                        'Available online with <a href="buy-online.htm">LetsTalk</a> and many retail stores.<br /><br />';
                }
                else
                {
    				tData_StrContent +=	'<ul class="bulletListPhone" style="margin-bottom:0px;"><li style="margin:0px;">' + carrier.name + '</li></ul>' +
									'<a href="' + externalURL + carrier.url + '" target="_blank">' + carrier.shortUrl + '</a><br />' +
									carrier.phone +'<br /><br />';
                }
			}
		}
		
		//Close Model List block code
		tData_StrContent +=	'</td>' +
							'</tr>' +
						'<tr><td colspan="3"><a href="#top"><img src="/image/button/go_to_top.gif" alt="go to top" width="66" height="12" border="0" align="right"></a></td></tr>';
	}
	
	//Close Provider list Table
	tData_StrContent += '</table>'
	
	//write final content
	document.write(tData_StrContent);
	
} else {
	//Data verification failed, display error message
	document.write('<i>This information is temporarily unavailable.</i>');
}

