﻿var chosenProductFamily = 0;
var chosenProduct = 0;
var optCount = 0;

function findLocale()
{
	var pathArray = new Array();
	
	pathArray = top.location.pathname.split( '/' );

	switch (pathArray[1].toLowerCase())
	{
		case 'br':  return 'br';
		case 'cn':  return 'cn';
		case 'de':  return 'de';
		case 'fr':  return 'fr';
		case 'it':  return 'it';
		case 'la':  return 'la';
		default:    return 'us';
	}
}

function findPSLocale()
{
	var pathArray = new Array();
	
	pathArray = top.location.pathname.split( '/' );

	switch (pathArray[1].toLowerCase())
	{
		case 'br':  return 'br';
		case 'de':  return 'de';
		case 'fr':  return 'fr';
		case 'it':  return 'it';
		case 'la':  return 'la';
		default:    return 'us';
	}
}

function findXMLLocale()
{
	var pathArray = new Array();
	
	pathArray = top.location.pathname.split( '/' );

	switch (pathArray[1].toLowerCase())
	{
		case 'cn':  return 'cn';
		case 'kr':  return 'kr';
		default:    return 'us';
	}
}

function defaultTranslation()
{
	var pathArray = new Array();
	
	pathArray = top.location.pathname.split( '/' );

	switch (pathArray[1].toLowerCase())
	{
		case 'cn':  return '请选择一项';
		case 'us':  return 'Please Select a Value';
		default:    return 'Please Select a Value';
	}
}




function buildXMLprefix ()
{
	var hostname = top.location.host;
	
	hostname = hostname.replace('www','support');
	hostname = hostname.replace('sites','support');
	hostname = hostname.replace('www-auth','support-auth');
	hostname = hostname.replace('www-dev', 'support-dev'); 
	
	return 'http://' + hostname;
	
}

function buildURLprefix ()
{
	var hostname = top.location.host;
	
	hostname = hostname.replace('www','support');
	hostname = hostname.replace('sites','support');
	hostname = hostname.replace('www-auth','support-auth');
	hostname = hostname.replace('www-dev', 'support-dev'); 
	
	return 'http://' + hostname + '/' + findLocale();
	
}

function buildPSURLprefix ()
{
	var hostname = top.location.host;
	
	hostname = hostname.replace('www','support');
	hostname = hostname.replace('sites','support');
	hostname = hostname.replace('www-auth','support-auth');
	hostname = hostname.replace('www-dev', 'support-dev'); 
	
	return 'http://' + hostname + '/' + findPSLocale();
	
}

function loadXMLDoc(dname)
{
	if (window.XMLHttpRequest)
  	{
  		xhttp=new XMLHttpRequest();
  	}
	
	else //if IE
  	{
  		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	xhttp.open("GET",dname,false);
	xhttp.send("");
	
	return xhttp.responseXML;
} 

if (top.location.host == "support.amd.com" || top.location.host == "support-prod.amd.com"|| top.location.host == "support-auth.amd.com" || top.location.host == "support-dev.amd.com")
{
	xmlPath = buildXMLprefix() + "/DriverSearchFiles/driver_09_us.xml";
	xmlTranslator=loadXMLDoc(buildXMLprefix() + "/DriverSearchFiles/translator.xml");
	
}
else
{
	xmlPath = "/Style%20Library/driver_09_us.xml";
	xmlTranslator=loadXMLDoc("/Style%20Library/translator.xml");
	
}

xmlDoc=loadXMLDoc(xmlPath);
getProductFamily = xmlDoc.getElementsByTagName("productfamily");
getPlatform = xmlDoc.getElementsByTagName("platform");

function printPlatform(formName0)
{
	document.forms[0][formName0].options.length = 0;	
	document.forms[0][formName0].options[0] = new Option(defaultTranslation(), "default");

	
	for(i=0; i<getPlatform.length; i++)
	{ 
		x = getPlatform[i].attributes;
		document.forms[0][formName0].options[i+1] = new Option(translateWords(x.getNamedItem("name").nodeValue), x.getNamedItem("value").nodeValue);				
	}
	
	document.forms[0].prod.options.length = 0;
	document.forms[0][formName0].selectedIndex = 0;
}

function populateList(boxSelected)
{
	if (boxSelected=="platform")
	{
		document.forms[0].prod.options.length = 0;
		document.forms[0].os.options.length = 0;
		printFamily(document.forms[0].platform.options[document.forms[0].platform.selectedIndex].value, "type");
	}
		
	else if (boxSelected=="type")
	{
		document.forms[0].os.options.length = 0;
		printProduct(document.forms[0].type.options[document.forms[0].type.selectedIndex].value);
	}
	else if (boxSelected=="prod")
	{
		printOS(document.forms[0].prod.options[document.forms[0].prod.selectedIndex].value, 'os');
	}
}

function printFamily(platformSelected, formname)
{	
	document.forms[0][formname].options.length = 0;
	
	for(i=0; i<getPlatform.length; i++)
	{
		platformAttributes = getPlatform[i].attributes;
 		var platformValue = platformAttributes.getNamedItem("value").nodeValue;
	
		if(platformSelected == platformValue)
		{
			chosenPlatform=getPlatform[i];
		}
	}

	getProductFamily = chosenPlatform.getElementsByTagName("productfamily");
		
	if(formname == "type")
	{
		document.forms[0][formname].options[0] = new Option(defaultTranslation(), "default");
		optCount=1;
	}
	
	for(i=0; i<getProductFamily.length; i++)
	{ 
		x = getProductFamily[i].attributes;
		document.forms[0][formname].options[i+optCount] = new Option(translateWords(x.getNamedItem("name").nodeValue), x.getNamedItem("value").nodeValue);			
	
	}
	
}

function printProduct(typeselected)
{
	document.forms[0].prod.options.length = 0;	
	
	findChosenFamily(typeselected);

	getProduct = chosenProductFamily.getElementsByTagName("product");
	
	first = getProduct[0].attributes;
	checkFirst = first.getNamedItem("label").nodeValue
	
	document.forms[0].prod.options[0] = new Option(defaultTranslation(), "default");
	
	if ( checkFirst == "N/A" && optCount==1)
	{
		document.forms[0].prod.options[optCount] = new Option("N/A","N/A");
		document.forms[0].prod.selectedIndex = optCount;

		if(optCount==0)
		{
			printOS("NA","type");
		}
		else
		{
			printOS("NA","os");
		}
		
	}
	else
	{
		for(j=0; j<getProduct.length; j++)
		{ 	
			x = getProduct[j].attributes;
			document.forms[0].prod.options[j+optCount] = new Option(translateWords(x.getNamedItem("label").nodeValue), x.getNamedItem("label").nodeValue);			
		}
	}
}	

function printOS(typeselected, formName2)
{
	document.forms[0][formName2].options.length = 0;
	
	findChosenProduct(typeselected);	

	getVersions = chosenProduct.getElementsByTagName("version");
	
	var indexOpt=0;
	
	document.forms[0][formName2].options[indexOpt] = new Option(defaultTranslation(), "default");
	
	if(typeselected == "NA")
	{
		indexOpt = indexOpt+optCount;
		document.forms[0][formName2].options[indexOpt] = new Option("N/A", "NA");
		document.forms[0][formName2].selectedIndex = indexOpt;

	}
	else
	{
		indexOpt = indexOpt+optCount;
	
		for(i=0; i<getVersions.length; i++)
		{		
			getOS = getVersions[i].childNodes;
			
				for(j=0; j<getOS.length; j++)
				{
					if (getOS[j].nodeType==1)
					{
					
						osName = getOS[j].attributes;
										
							document.forms[0][formName2].options[indexOpt] = new Option(osName.getNamedItem("name").nodeValue, getOS[j].nodeName);		
						indexOpt++;
					}		
				}
			}
	
	}	
}

function findChosenFamily(typeSelected)
{
	
	for(i=0; i<getProductFamily.length; i++)
	{
		familyAttributes = getProductFamily[i].attributes;
 		var familyValue = familyAttributes.getNamedItem("value").nodeValue;
	
	 if(typeSelected == familyValue)
		{
			chosenProductFamily=getProductFamily[i];
		}
	}
}


function findChosenProduct()
{	
	var prodSelected = document.forms[0].prod.options[document.forms[0].prod.selectedIndex].value;
	
	getProducts = chosenProductFamily.getElementsByTagName("product");
	
	for(i=0; i<getProducts.length; i++)
	{
		productAttributes = getProducts[i].attributes;
 		var productValue = productAttributes.getNamedItem("label").nodeValue;
	
	 if(prodSelected == productValue)
		{
			chosenProduct=getProducts[i];	
		}
	}
}

function changePage(formName3)
{
	
	if(formName3=='os')
	{
		if (document.forms[0].platform.options.selectedIndex < 1)
		{
			alert("Please Choose a Platform from the list");
			return false;
		}
		else if (document.forms[0].type.options.selectedIndex < 1)
		{
			alert("Please Choose a Product Family from the list");
			return false;
		}
		else if (document.forms[0].prod.options[0].value !="N/A" && document.forms[0].prod.options.selectedIndex < 1)
		{
			alert("Please Choose a Product from the list");
			return false;
		}
		else if (document.forms[0].os.options[0].value !="NA" && document.forms[0].os.options.selectedIndex < 1)
		{
			alert("Please Choose an Operating System from the list");
			return false;
		}
	}
	
	if(optCount==1)
	{
		SUPprocessTracking();
	}
	
	var osSelected = document.forms[0][formName3].options[document.forms[0][formName3].selectedIndex].value;
	sitePrefix = buildURLprefix();		
		
	chosenOSNode = chosenProduct.getElementsByTagName(osSelected)[0].childNodes[0];
	
	chosenOS = translateURL(chosenOSNode.nodeValue);

	httpCheck = chosenOS.substring(0,4);
	
	if (httpCheck == "http")
	{
		url = chosenOS;
	}
	else
	{
		url = sitePrefix + chosenOS;
	}
	
	if(optCount==1)
	{
	document.forms[0].platform.selectedIndex = 0;
	}
	top.location.href = url;
	
	return false;

}


function translateWords(wordToLookUp)
{
	var getWordsToTranslate= xmlTranslator.getElementsByTagName("translateword");
	var wordArray = new Array();
	var pathArray = new Array();
	var translatedWord;
	var translateLocal=null;
	pathArray = top.location.pathname.split( '/' );
	
	var local = pathArray[1];
	
	
	for(w=0;w<getWordsToTranslate.length; w++)
	{
		translateAttributes = getWordsToTranslate[w].attributes;
 		var wordToTranslate = translateAttributes.getNamedItem("word").nodeValue;
		translateLocal = getWordsToTranslate[w].getElementsByTagName(local);
		
		if(wordToLookUp == wordToTranslate && translateLocal.length!=0)
		{
			translatedWord=translateLocal[0].childNodes[0].nodeValue;
			return translatedWord;
		}
		else
		{
			wordArray = wordToLookUp.split(' ');
			for(k=0; k<wordArray.length; k++)
			{
				if(wordArray[k] == wordToTranslate && translateLocal.length!=0)
				{
					translatedWord=translateLocal[0].childNodes[0].nodeValue;
					return wordToLookUp.replace(wordArray[k], translatedWord);
				}
			}
		}
	}
	return wordToLookUp;
}

function translateURL(urlToLookUp)
{
	var getURLsToTranslate= xmlTranslator.getElementsByTagName("translateurl");
	var pathArray = new Array();
	var translatedURL;
	var translateLocal;
	pathArray = top.location.pathname.split( '/' );
	
	var local = pathArray[1];
	
	
	for(i=0;i<getURLsToTranslate.length; i++)
	{
		translateAttributes = getURLsToTranslate[i].attributes;
 		var urlToTranslate = translateAttributes.getNamedItem("url").nodeValue;
		translateLocal = getURLsToTranslate[i].getElementsByTagName(local);
		
		if(urlToLookUp == urlToTranslate && translateLocal.length!=0)
		{
			translatedURL=translateLocal[0].childNodes[0].nodeValue;
			return translatedURL;
		}
	}
	return urlToLookUp;
}

function SUPprocessTracking()
{
	var str = '';
	var sCurrURL;
	var sTrackingAccounts = 'amdvsupport,amdvglobal';
	var element;
//	var tmpAcct = hbx.acct;
	var pathArray = new Array();
	var sCurrentHost = window.location.host;

	sCurrURL =  cfPageName(top.location.href,0,"");
	pathArray = top.location.pathname.split( '/' );

	element = document.getElementById("platform");
	if (!element.disabled && !element.selectedIndex == 0)
	{
		str = str + element[element.selectedIndex].value;

		element = document.getElementById("type");
		if (!element.disabled && !element.selectedIndex == 0)
			str = str + '|' + element[element.selectedIndex].value;
		else
			str = str + '|NA';
	
		element = document.getElementById("prod");
		if (!element.disabled && !element.selectedIndex == 0)
			str = str + '|' + element[element.selectedIndex].value;
		else
			str = str + '|NA';
		
		element = document.getElementById("os");
		if (!element.disabled && !element.selectedIndex == 0)
			str = str + '|' + element[element.selectedIndex].text;
		else
			str = str + '|NA';
	}
	
	str = str.replace(/series/i, '');
	str = str.replace(/radeon/i, 'rad-');
	str = str.replace(/all-in-wonder/i, 'AIW-');
	str = str.replace(/Crossfire/i, 'xfire-');
	str = str.replace(/Mobility/i, 'mob-');
	str = str.replace(/\s+/g,'');
	str = str.replace(/[^a-zA-Z_| 0-9]+/g,'');
	
	if (str!='')
	{
		str = 'DriverDL|' + pathArray[1].toString().toUpperCase() + '|' + str;
		if (70 < String(str).length)   // Invalid bound, return
			str = String(str).substring(0,70);
		
		//Set Tracking Accounts
		if (sCurrentHost.indexOf('support') > -1)
			sTrackingAccounts = 'amdvsupport,amdvglobal';
		if (sCurrentHost.indexOf('sites') > -1)
			sTrackingAccounts = 'amdvsites,amdvglobal';
			
		//Process Omniture Tracking
		var s=s_gi(sTrackingAccounts);
		s.linkTrackVars='prop12,prop13,prop14,prop15,prop22,events';
		s.linkTrackEvents='event16';
		s.events='event16';
		s.trackingServer="metrics.amd.com";
		
		s.prop12 = sCurrURL; 	//current page URL
		s.prop13 = str; 			//current option values
		s.prop14 = 'destinationURL'; //destination page URL
		s.prop15 = 'DriverDL|' + pathArray[1].toString().toUpperCase(); //current region
		s.prop22 = sTrackingAccounts; //tracking accounts
	
		s.tl(s.prop14,'o',s.prop13);

	}
}
