$(document).ready(function(){
	//add icon to the learn more links
  $('a.learn_more').each(function(){
  
    var newHtml = '<em>' + $(this).html() + '</em><span></span>';
    
    $(this).html(newHtml);    
  
  });    

}); 

$(document).ready(function(){
	//add icon to the pdf links
  $('a.pdf').each(function(){
  
    var newHtml = '<em>' + $(this).html() + '</em><span></span>'; 
    
    $(this).html(newHtml);    
  
  });
	
});


$(document).ready(function() {
	//add url to linkipww links
	$('a.linkipww').each(function() {
		pathArray = window.location.pathname.split( '/' );
		newPathname = "";
		for ( i = 1; i < pathArray.length - 1; i++ ) {
			newPathname += "/";
			newPathname += pathArray[i];
		};
		var newHref = newPathname + '/' + $(this).attr('href');
		$(this).attr('href', newHref);
	});
});

$(document).ready(function() {
	//add url to linkelit links
	$('a.linkelit').each(function() {
		var newHref = '/internet/comnelit.nsf/OpenURL?OpenAgent&docid=' + $(this).attr('href');
		$(this).attr('href', newHref);
	});
});
$(document).ready(function() {
	//add url to linkfilenet links
	$('a.linkfilenet').each(function() {
		var newHref = 'http://webservice.infoprint.com/internet/webserv.nsf/RetrieveContentServicePRODJ-B64-S?openagent&' + $(this).attr('href');
		$(this).attr('href', newHref);
		$(this).attr('target', '_blank');
	});
});

$(document).ready(function() {
	//add url to linkext links
	$('a.linkext').each(function() {
		$(this).attr('target', '_blank');
	});
});

$(document).ready(function() {
	//add target to linkelit links
	$('a.linkelit').each(function() {
		$(this).attr('target', '_blank');
	});
});

$(document).ready(function() {
	//add icon to the iconpdf links
	$('a.iconpdf').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the iconreg links
	$('a.iconreg').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the icondwl links
	$('a.icondwl').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});	//add icon to the iconxls links
	$('a.iconxls').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the iconext links
	$('a.iconext').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the iconvid links
	$('a.iconvid').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the iconlrg links
	$('a.iconlrg').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the iconlrg links
	$('a.iconpopover').each(function() {
		var newHtml = '<em>' + $(this).html() + '</em><span></span>';
		$(this).html(newHtml);
	});
	//add icon to the iconemail links
	$('a.iconemail').each(function() {
		var newHtml = '<span></span><em>' + $(this).html() + '</em>';
		$(this).html(newHtml);
	});
	//add icon to the iconphone spans
	$('span.iconphone').each(function() {
		var newHtml = '<span></span>' + $(this).html() + '';
		$(this).html(newHtml);
	});
});
function openPopover( url, height, width ) {
	var $newdialog = $('<div></div>')
	.html('<iframe style="height: 100%; width: 100%;" marginWidth="0" marginHeight="0" frameBorder="0" src="' + url + '" allowTransparency="true"/>')
	.dialog({
		autoOpen: false,
		modal: true,
		bgiframe: ie6browser, // ie6browser is true or false, and only ie6 needs bgiframe set
		title: '',
		closeText: 'X'
	})
	.dialog('option', 'height', height)
	.dialog('option', 'width', width);
	$newdialog.dialog('open');
}

//image management for dynamic alt and src
$(document).ready(function() {
	$('img.imgalt').each(function() {
		var cursrc = $(this).attr('src');
		if (imageIDs[$(this).attr('src')]) {	
			var luimageID = imageIDs[$(this).attr('src')].split('~~');
			var lusrc = luimageID[0];
			var lualt = luimageID[1];
			if (lualt == 'None') {
			   lualt = '';
			}
			$(this).attr('src',lusrc);
			$(this).attr('alt',lualt);
		}
	});
});

//
// ************************************************************************
//                    COUNTRY & LANGUAGE SELECTION COOKIE
// ************************************************************************
//

function setCookie(name, value, expires) {
	var tmpHostname = document.location.hostname.split(".");
	document.cookie = name + "=" + escape(value) + "; path=/; domain=" + tmpHostname[tmpHostname.length-2] + "." + tmpHostname[tmpHostname.length-1] + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

function getCookie(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 selectPosition(thisField) {
	for (var i = 0; i < thisField.length; i++) {
		if (thisField.options[i].selected ) {
			return i;
		}
	}
} 


function rightBack(sourceStr, keyStr){
arr = sourceStr.split(keyStr);
return (sourceStr.indexOf(keyStr) == -1 | keyStr=='') ? '' : arr.pop()
} 

function leftBack(sourceStr, keyStr){
	arr = sourceStr.split(keyStr)
	arr.pop();
	return (keyStr==null | keyStr=='') ? '' : arr.join(keyStr)
}

function changeCountryLanguage() { 
	var r10LangCodes = "en~~fXr~~iXt~~dXe~~eXs~~pXt";
	var ccf = document.getElementsByName( 'CountryLanguage' )
	var form = document.FormCountryLanguage;
	if (selectPosition(form.CountryLanguage) == 0) {
		return;
	}

	var cl = form.CountryLanguage.value;     // grab the string from the dropdown field selection
	var clArray = cl.split("~~");
	countryCode = clArray[0];     // country code is the first part of the string
	langCode = clArray[1];     // language code is the second part of the string

	if (cl == "ca~~en") {
		location.href = "sup_call-or-email-us_caen_en";
		return;
	}
	if (cl == "ca~~fr") {
		location.href = "sup_call-or-email-us_cafr_en";
		return;
	}
	if (cl == "es~~es") {
		location.href = "sup_call-or-email-us_eses_en";
		return;
	}
	if (cl == "de~~de") {
		location.href = "sup_call-or-email-us_dede_en";
		return;
	}
	if (cl == "at~~de") {
		location.href = "sup_call-or-email-us_atde_en";
		return;
	}
	if (cl == "be~~fr") {
		location.href = "sup_call-or-email-us_befr_en";
		return;
	}
	if (cl == "za~~en") {
		location.href = "sup_call-or-email-us_zaen_en";
		return;
	}
	if (cl == "ch~~de") {
		location.href = "sup_call-or-email-us_chde_en";
		return;
	}
	if (cl == "ch~~fr") {
		location.href = "sup_call-or-email-us_chfr_en";
		return;
	}
	if (cl == "ch~~it") {
		location.href = "sup_call-or-email-us_chit_en";
		return;
	}
	if (cl == "uk~~en") {
		location.href = "sup_call-or-email-us_uken_en";
		return;
	}
	if (cl == "kr~~ko") {
		location.href = "sup_call-or-email-us_krkr_en";
		return;
	}

	var exp = new Date();     //set new date object
	exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 365));     //set it 365 days ahead
	setCookie('IPWWCountryCode', countryCode, exp);
	setCookie('IPWWLanguageCode', langCode, exp);
     
	r08LangCode = "";
	switch(langCode) {
	case "de":
		r08LangCode = "de";
		break;
	case "es":
		r08LangCode = "es";
		break;
	case "fr":
		r08LangCode = "fr";
		break;
	case "it":
		r08LangCode = "it";
		break;
	case "ja":
		r08LangCode = "jp";
		break;
	case "ko":
		r08LangCode = "kr";
		break;
	case "pt":
		r08LangCode = "br";
		break;
	case "zhcn":
		r08LangCode = "cn";
		break;
	case "zhtw":
		r08LangCode = "tw";
		break;
	default:
		r08LangCode = "en";
	}

	// set R08 cookies as well
	setCookie('WWSitesCountryCode', countryCode, exp);
	setCookie('WWSitesLanguageCode', langCode, exp);
	setCookie('IPSMAPackCountryCode', r08LangCode, exp); //Form: /internet/custform.nsf/MAPackReg
	setCookie('IPSOTCCountryCode', r08LangCode, exp); //Form: /internet/custform.nsf/OCOrderFrm
	setCookie('IPSUpdateInsCountryCode', r08LangCode, exp); //Form: /apps/sales/formsext.nsf/UpdateInstall

	currentURL = location.href;
	currentHash = location.hash;
	currentBase = leftBack(currentURL, "_");
		
	// Adding these to support logic below - Jim 2/2/11

	if (currentHash == "") {
		currentShortname = rightBack(currentURL, "/");
	} else {
		currentShortname = leftBack(rightBack(currentURL, "/"), "#");
	}
	currentPath = leftBack(currentURL, "/");	// everything left of the shortname (and slash)


	// Adding special logic to handle US and UK home pages - Jim 2/2/11
	// When on the US home page, if UK is selected, must redirect to the UK home page.
	// When on the UK home page, if any other English country is selected, must redirect to the US home page.

	if (currentShortname == "home_en" && countryCode == "uk") {	// On US home page and selecting UK
		location.href = currentPath + "/home_uk_en";		// Redirect to UK home page
	} else if ( currentShortname == "home_uk_en" && langCode == "en" && countryCode != "uk") {	// On UK home page and selecting any other English country
		location.href = currentPath + "/home_en";			// Redirect to US home page
	} else if ( currentBase + "_" + langCode + currentHash == currentURL ) { 
		location.reload();
	} else if ( r10LangCodes.indexOf( langCode ) > -1 ) {
		if ( r08FormPath != "" ) {
			location.reload();
		} else {
			location.href = currentBase + "_" + langCode + currentHash;
		}
	} else {
		if ( r08FormPath != "" ) {
			location.href = r08FormPath;
		} else {
			location.href = r08DocPath + r08LangCode;
		}
	}
}

