/*
This file contains other functions and logic to perform
*/

// exclude old browsers
if (!document.getElementById){
	top.location.replace('http://www.iht.com/info/browser.html');
}

// disallow the page to be framed
if (window.self != window.top){ 
	top.location.replace(window.location.pathname); 
}

// used for the markets stock symbol look up form
function checkSymbolForm(){
	var obj = document.lookitup.symb;
	if (obj.value == ""){
		alert("Please enter a valid stock ticker symbol");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

// generic popup window
function pop_me_up(pURL,name,features){
	new_window = window.open(pURL,name,features);
	new_window.focus();
}

// rollover image function
function changeImage(image_name,image_src) {
   	document.images[image_name].src = image_src;
}

// preload nav and other images
function preloadImages(){
	nav_home_over = newImage("http://www.iht.com/images/nav/nav_home_on.gif");
	nav_business_over = newImage("http://www.iht.com/images/nav/nav_business_on.gif");
	nav_tech_over = newImage("http://www.iht.com/images/nav/nav_tech_on.gif");
	nav_travel_over = newImage("http://www.iht.com/images/nav/nav_travel_on.gif");
	nav_style_over = newImage("http://www.iht.com/images/nav/nav_style_on.gif");
	nav_culture_over = newImage("http://www.iht.com/images/nav/nav_culture_on.gif");
	nav_healthsci_over = newImage("http://www.iht.com/images/nav/nav_healthsci_on.gif");
	nav_sports_over = newImage("http://www.iht.com/images/nav/nav_sports_on.gif");
	nav_regions_over = newImage("http://www.iht.com/images/nav/nav_regions_on.gif");
	nav_opinion_over = newImage("http://www.iht.com/images/nav/nav_opinion_on.gif");
	nav_classifieds_over = newImage("http://www.iht.com/images/nav/nav_classifieds_on.gif");
	nav_clippings_over = newImage("http://www.iht.com/images/nav/nav_clippings_on.gif");
	nextpage_over = newImage("http://www.iht.com/images/icon/prevHot.gif");
	prevpage_over = newImage("http://www.iht.com/images/icon/nextHot.gif");
}

function newImage(image){
	if (document.images){
		rslt = new Image();
		rslt.src = image;
		return rslt;
	}
}

// event handler used for the navigation rollovers
sfHover = function(){
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i = 0; i < sfEls.length; i++){
		sfEls[i].onmouseover = function(){
			this.className += " sfhover";
			//document.getElementById("ultralingua").style.visibility = "hidden";
		}
		sfEls[i].onmouseout = function(){
			this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
			//document.getElementById("ultralingua").style.visibility = "visible";
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// pops up window for the ultralingua translation service
function ulPopUp(url){
	var templang = document.ultralingua.language[ultralingua.language.selectedIndex].value;
	//var templang = document.getElementById("ultralingua").language[ultralingua.language.selectedIndex].value;
	var valarray = templang.split("|");
	var lang = valarray[0];
	var langfull = valarray[1];
	var ulurl;
	if (lang == "whatisthis"){
		ulurl = "http://www.ultralingua.com/mock-up/enable-what.html";
	} else {
		var ulurlstart = "http://www.ultralingua.net/services/webtrans/iht/iht-";
		var ulurlend = "-enable.cgi?service=english2" + langfull + "&location=http://www.iht.com/bin/print_ipub.php?file=" + url;
		ulurl = ulurlstart + lang + ulurlend;
	}
	window.open(ulurl);
}

// Displays the google ad results. Google uses this function as a
// callback so it must be defined in the header of the document.
function google_ad_request_done(google_ads){

	// Proceed only if we have ads to display!
	if (google_ads.length < 1 ){
		return;
	} else {

		var width = "";
		var loopcount = 0;
		if (google_ads.length == 1){
			width = "100%";
			loopcount = 1;
		} else if (google_ads.length == 2){
			width = "50%";
			loopcount = 2;
		} else if (google_ads.length == 3){
			width = "33%";
			loopcount = 3;
		} else {
			width = "25%";
			loopcount = 4;
		}
	
		document.write('<table border="0" cellpadding="0" cellspacing="0" width="663">');
		document.write('<tr>');
		document.write('<td bgcolor="#225577">');
		document.write('<table border="0" cellpadding="4" cellspacing="1" width="663">');
		document.write('<tr>');
		document.write('<td align="left" colspan="4">');
		document.write('<span class="text2_white"><b>ADVERTISER LINKS</b></span><br>');
		document.write('</td>');
		document.write('</tr>');
		document.write('<tr>');
		
		// For text ads, display each ad in turn.
		if (google_ads[0].type == 'text'){
			for (var i = 0; i < loopcount; ++i){
				document.write('<td bgcolor="#ffffff" align="left" valign="top" width="' + width + '">');
				document.write('<span class="text1"><a href="' +  google_ads[i].url + '"><b>' + google_ads[i].line1 + '</b></a></span><br>');
				document.write('<span class="text2_google">' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '<br>');
				document.write('<a href="' +  google_ads[i].url + '">' + google_ads[i].visible_url + '</a></span>');
				document.write('</td>');
			}
		}
		
		document.write('</tr>');
		document.write('</table>');
		document.write('</td>');
		document.write('</tr>');
		document.write('</table>');
		
	}
}
