var priceList = {
"AL":"1028",
"AR":"1002",
"AT":"1003",
"AU":"1010",
"BA":"1028",
"BD":"1002",
"BE":"1003",
"BG":"1028",
"BH":"1002",
"BO":"1002",
"BR":"1022",
"CA":"1001",
"CH":"1005",
"CL":"1002",
"CN":"1029",
"CO":"1002",
"CR":"1002",
"CS":"1028",
"CY":"1002",
"CZ":"1024",
"DE":"1003",
"DK":"1009",
"DZ":"1002",
"EC":"1002",
"EG":"1002",
"ES":"1003",
"FI":"1003",
"FR":"1003",
"GB":"1008",
"GT":"1002",
"HK":"1018",
"HN":"1002",
"HR":"1028",
"HU":"1025",
"ID":"1014",
"IE":"1003",
"IL":"1002",
"IN":"1023",
"IQ":"1002",
"IT":"1003",
"JP":"1004",
"KR":"1016",
"KW":"1002",
"LB":"1002",
"LK":"1002",
"LU":"1003",
"MA":"1002",
"MK":"1028",
"MX":"1002",
"MY":"1013",
"NI":"1002",
"NL":"1003",
"NO":"1007",
"NZ":"1011",
"OM":"1002",
"PA":"1002",
"PE":"1002",
"PH":"1014",
"PK":"1002",
"PL":"1015",
"PR":"1000",
"PT":"1003",
"QA":"1002",
"RO":"1028",
"RU":"1021",
"SA":"1002",
"SE":"1006",
"SG":"1012",
"SI":"1028",
"SK":"1028",
"SV":"1002",
"TH":"1027",
"TN":"1002",
"TR":"1003",
"TW":"1019",
"US":"1000",
"VE":"1002",
"VN":"1014",
"YE":"1002",
"ZA":"1032"
}

var defaultLocales = {
	"us":"en-US",
	"de":"de-DE",
	"fr":"fr-FR",
	"it":"it-IT",
	"es":"es-MX",
	"ja":"ja-JP",
	"ko":"ko-KR",
	"zh-CN":"zh-CN",
	"zh-TW":"zh-TW"
}

var price       = {};
var countryInfo = {};
var user        = getUserInfo();


/*******************************************************
* Executes while page is loading. Loads helper scripts *
*******************************************************/
var script = document.getElementsByTagName("script");
for (var i = 0; i < script.length; i++) {
	if (script[i].src.indexOf("PNPR_main.js") > -1) {
		var path = script[i].src.replace(/PNPR_main\.js.*?$/, '');
		var includes = script[i].src.replace(/[^\?]*\?/, '');
		if (includes.indexOf("prices") > -1) {
			includeScript(path+"priceList_"+user.priceList+".js");
		}
		if (includes.indexOf("country") > -1) {
			includeScript(path+"countryInfo_"+user.countryCode+".js");
		}
	}
}


/*************************************
* Function to include helper scripts *
*************************************/
function includeScript(strFile) {
	document.write('<scr'+'ipt type="text/javascript" src="'+strFile+'" charset="utf-8"></scr'+'ipt>');
}


/**********************************************
* Function to append functions to page onload *
**********************************************/
function appendEvent(el, evname, func) {
	try {el.addEventListener(evname, func, false); return;} catch(e){}
	try {el.attachEvent("on" + evname, func); return;} catch(e){}
	try {el["on" + evname] = func; return;} catch(e){}
}


/*********************************************************
* Replaces all DIVs where countryInfo has been requested *
*********************************************************/
function loadData(strType) {
	var arrTN = new Array();
	var arrPN = new Array();
	var div = document.getElementsByTagName("div");
	for (var i = 0; i < div.length; i++) {
		try {
			divType = div[i].getAttribute("divType");
			divVal  = div[i].getAttribute("val");
			if (divType == "countryInfo" && divVal == strType) {
				arrTN[i] = document.createTextNode(countryInfo[strType]);
				div[i].appendChild(arrTN[i]);
			}
		} catch (e) {}
	}
}


/***************************************************************
* Get the locale, country code, and price list. Returns object *
***************************************************************/
function getUserInfo() {
	var match          = document.cookie.match(eval("/locale=([^; ]*)/i"));
	var locale         = ((match) ? unescape(match[1]) : getDefaultLocale());
	var isoLanguage    = getIsoLanguage(locale);
	var oracleLanguage = getOracleLanguage(locale);
	var countryCode    = locale.substring(3, 5);
	var pl             = (priceList[countryCode]) ? priceList[countryCode] : 1000;
	return {"locale" : locale, "isoLanguage" : isoLanguage, "oracleLanguage" : oracleLanguage, "countryCode" : countryCode, "priceList" : pl};
}


/******************************************************************
* Figures out the default locale based on the page lang attribute *
******************************************************************/
function getDefaultLocale() {
	try {
		var lang = document.getElementsByTagName("html")[0].getAttribute("lang");
		if (defaultLocales[lang]) {
			return defaultLocales[lang]
		} else {
			throw "err";
		}
	} catch (e) {
		return "en-US";
	}
}


function getIsoLanguage(locale) {
	lang = locale.substring(0,2);
	if (lang == "zh") { 
		lang = ((locale == "zh-TW") || (locale == "zh-HK")) ? "zht" : "zhs";
	} 
	return lang;
}


function getOracleLanguage(locale) {
	lang = locale.substring(0, 2);
	if (lang != "zh") {
		switch(lang) {
			case "en":
				lang = "US";
				break;
			case "ja":
				lang = "JA";
				break;
			case "de":
				lang = "D";
				break;
			case "it":
				lang = "I";
				break;
			case "fr":
				lang = "F";
				break;
			case "es":
				lang = "ESA";
				break;
			case "ko":
				lang = "KO";
				break;
			default:
				lang = locale.substring(0, 2);
				break;
		}
	} else {
		lang = ((locale == "zh-TW") || (locale == "zh-HK")) ? "zht" : "zhs";
	}
	return lang;
}


/************************************************
* For global gateway simulation on static pages *
************************************************/
function DisplayLocale() {
	switch(user.locale) {
		case "zh-TW":
			lang = "zht";
			break;
		case "zh-HK":
			lang = "zht";
			break;
		case "zh-CN":
			lang = "zhs";
			break;
		default:
			lang = user.locale.substring(0, 2);
			break;
	}
	var url = encodeURIComponent(location.href);
	location.href = "http://sine.ni.com/nigwy/cds/globalgateway/p/lang/"+lang+"?rtrn="+url; 
}


/************************************
* Japan's removal request on DIAdem *
************************************/
function writePartDiv(num) {
	var match = document.cookie.match(eval("/locale=[a-z]{2}-([^; ]*)/i"));
	var countryCode = ((match) ? unescape(match[1]) : "US");
	if (countryCode != "JP") {
		document.write('<div divType="partNumber" val="' + num + '"></div>');
	}
}

