var strDiv = "dQuestions";
var numFacet = 0;
var strProdFam = eatCookie("dProdFam");
var strProd = eatCookie("dProd");
var strProdVer = eatCookie("dProdVer");
var strProdVerSel = eatCookie("verUp");
var binLoad = false;
function bakeCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eatCookie(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 crushCookie(name) {
	bakeCookie(name,"",-1);
}
function cookieMonster(name) {
	var arrCookies = new Array("verUp","dProdVer","dProd");
	var arrAnalytics = new Array("analytics.version","analytics.product_name","analytics.product_family");
	for(var i=0;i<arrCookies.length;i++) {
		if (arrCookies[i] != name || name == 'dProd') {
			crushCookie(arrCookies[i]);
			if(document.getElementById(arrCookies[i])!=undefined) {
				document.getElementById(arrCookies[i]).innerHTML = "<select style=\"width:100%\" name=\"" + arrCookies[i] + "\" disabled=\"disabled\"><option>All</option></select>"; 
			}
			if(document.question_form.elements[arrAnalytics[i]]!=undefined) {
				document.question_form.elements[arrAnalytics[i]].value = "All"; 
			}
			
		} else {
			break;	
		}
	}
	if (name == 'dProd') {
		
		var prodRoot = eatCookie("prodRoot");
		if (prodRoot!=null && prodRoot.length>0) {
			
			if (document.question_form.elements['restriction.level.product'] != undefined) {
				var strTemp = document.question_form.elements['restriction.level.product'].value;
				if(strTemp.indexOf(",")>-1) {
					strTemp = strTemp.substring(strTemp.indexOf(","),strTemp.length);	
				} else {
					strTemp = "";	
				}
				document.question_form.elements['restriction.level.product'].value = "CMS-CATEGORY_REF.*." + prodRoot + strTemp;
			}
		} else if (document.question_form.elements['restriction.level.product'] != undefined) {
				//alert("you are without root");
				bakeCookie("prodRoot",document.question_form.elements['restriction.level.product'].value,1);
			
		} 
	}
}
function makeRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		/*
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
		*/
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}

/*
function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById(strDiv).innerHTML = result; 
			if(binLoad && strDiv=="dProdFam" && strProd!=null) {
				//alert("strProd:" + strProd + \n + "strProdVer:" + strProd + \n +);
				loadDrop(strProd,"dProd");
			} else if(binLoad && strDiv=="dProd" && strProdVer!=null) {
				loadDrop(strProdVer,"dProdVer");
			} 
		} else {
			alert('There was a problem with the request. ' + http_request.status);
		}
	} 
}
*/

function alertContents() {
	try{
		if (http_request.readyState == 4) {
			if (http_request.status == 200 /*|| http_request.status == 0 || http_request.status == 302*/) {
				//alert(http_request.responseText);
				result = http_request.responseText;
				document.getElementById(strDiv).innerHTML = result; 
				if(binLoad && strDiv=="dProdFam" && strProd!=null) {
					//alert("strProd:" + strProd + \n + "strProdVer:" + strProd + \n +);
					loadDrop(strProd,"dProd");
				} else if(binLoad && strDiv=="dProd" && strProdVer!=null) {
					loadDrop(strProdVer,"dProdVer");
				} 
			} else {
				//alert('There was a problem with the request. ' + http_request.status);
				//window.location.reload(true);
				window.location.reload(false);
				//makeRequest("/support/sites/gefanuc/components/categories/c_aj_buildDrop.jsp", "");
			}
		}
	}catch( e ) {
		//alert('Caught Exception: ' + e.description);
		//window.location.reload(true);
    }
}

function getDrop(strCat,strDivIn) {
	binLoad = false;
	strDiv = strDivIn;
	cookieMonster(strDivIn);
	var strCatTemp = null;
	if(strDivIn=='dProdVer' && strCat.length==0) {
		strCatTemp = document.frmFilter.elements['dProdFam'].value;
	}
	if(strDivIn=='verUp' && strCat.length==0) {
		strCatTemp = document.frmFilter.elements['dProd'].value;
	}
	strCatTemp = ((strCatTemp==null || strCatTemp.length==0) && strCat.length >0) ? strCat : strCatTemp;
	strCatTemp = ((strCatTemp==null || strCatTemp.length==0)) ? eatCookie("prodRoot") : strCatTemp; 
	if ((strCat!=null && strCat.length>0) || (strCatTemp!=null && strCatTemp.length>0)) {
		if (strDivIn== 'dProd' && (strCat=='PRODUCTSAUTOMATION')) {
			
		} else {
			if(document.question_form.elements['restriction.level.product'] != undefined) {
				var strTemp = document.question_form.elements['restriction.level.product'].value;
				if(strTemp.indexOf(",")>-1) {
					strTemp = strTemp.substring(strTemp.indexOf(","),strTemp.length);	
				} else {
					strTemp = "";	
				}
				document.question_form.elements['restriction.level.product'].value = "CMS-CATEGORY_REF.*." + strCatTemp + strTemp;
			}
			if(strDivIn== 'dProd' && document.question_form.elements['analytics.product_family'] != undefined) {
				document.question_form.elements['analytics.product_family'].value = document.getElementById('dProdFamSel').options[document.getElementById('dProdFamSel').selectedIndex].text;
			}
			if(strDivIn== 'dProdVer' && document.question_form.elements['analytics.product_name'] != undefined) {
				var strCatName = (strCat.length>0) ? document.getElementById('dProdSel').options[document.getElementById('dProdSel').selectedIndex].text : "All";
				document.question_form.elements['analytics.product_name'].value = strCatName;
			}
			if(strDivIn== 'verUp' && document.question_form.elements['analytics.version'] != undefined) {
				var strCatName = (strCat.length>0) ? document.getElementById('dProdVerSel').options[document.getElementById('dProdVerSel').selectedIndex].text : "All";
				document.question_form.elements['analytics.version'].value = strCatName;
			}
			//alert(document.question_form.elements['cat']==undefined);
			if(document.question_form.elements['cat'] != undefined)
				document.question_form.elements['cat'].value = strCat;
			getstr = "?cat=" + strCat + "&nxt=" + strDiv;  // NOTE: no '?' before querystring 
			if (strCat.length>0) {
				bakeCookie(strDivIn,strCat,1);	
				makeRequest("/support/sites/gefanuc/components/categories/c_aj_buildDrop.jsp", getstr);
			}
		}
	} 
	if (strCat.length==0) {
		crushCookie(strDivIn);	
	}
}
function fnToolPop(strDivIn,strType) {
	if(strDivIn.span==undefined) { 
		binLoad = false;
		strDiv = strDivIn;
		getstr = "?page=tooltip&subj=" + strDivIn + "&type=" + strType;
		makeRequest("/support/index",getstr);
	}
}
function loadDrop(strCat,strDivIn) {
	binLoad = true;
	strDiv = strDivIn;
	fnLoadFields();
	getstr = "?cat=" + strCat + "&nxt=" + strDiv;          
	if(strDivIn=="dProdFam" && strProd!=null) {
		getstr += "&sel=" + strProd;
	} else if(strDivIn=="dProd" && strProdVer!=null) {
		getstr += "&sel=" + strProdVer;
	} else if(strDivIn=="dProdVer" && strProdVerSel!=null) {
		getstr += "&sel=" + strProdVerSel;
	} 
	makeRequest("/support/sites/gefanuc/components/categories/c_aj_buildDrop.jsp", getstr);
}
function fnLoadFields() {
	var strCat = "";
	if (strProdVerSel!=null) {
		strCat = strProdVerSel;
	} else if (strProdVer!=null) {
		strCat = strProdVer;
	} else if (strProd!=null) {
		strCat = strProd;
	} else if (strProdFam!=null) {
		strCat = strProdFam;
	}
	//if(document.question_form.elements['language'] != undefined && document.frmFilter.elements['locale'] != undefined) {
	if(document.question_form.elements['language'] != undefined && document.question_form.elements['locale'] != undefined) {
		var strTemp = document.question_form.elements['language'].value;
		if (strTemp!=null) {
			rExp = /-/;
			strTemp = strTemp.replace(rExp, "_");
			if (strTemp.indexOf("ja-JA")>-1) {
				strTemp = "ja_JP";
			}
			//document.frmFilter.elements['locale'].value=strTemp;
			document.question_form.elements['locale'].value=strTemp;
		}
	}
	if (strCat.length>0) {
		if(document.question_form.elements['restriction.level.product'] != undefined) {
			var strTemp = document.question_form.elements['restriction.level.product'].value;
			if(strTemp.indexOf(",")>-1) {
				strTemp = strTemp.substring(strTemp.indexOf(","),strTemp.length);	
			} else {
				strTemp = "";	
			}
			//document.question_form.elements['restriction.level.product'].value = "CMS-CATEGORY_REF.*." + strCat + strTemp;
		}
		if(document.question_form.elements['cat'] != undefined)
			document.question_form.elements['cat'].value = strCat;
	}
}
function setTarget(objForm){
	var form = document.question_form;
	//alert(form.dProdFam);
	/*
	var prodF = objForm.dProdFam.value;
	var prodN = objForm.dProd.value;
	var prodV = objForm.dProdVer.value;
	var prodL = objForm.locale.value;
	
	if(prodV!=null && prodV != "" && prodV.lastIndexOf("VERSIONS") < (prodV.length-8)){
		objForm.cat.value = prodV;
	}else if(prodN!=null && prodN!="" && objForm.dProd.text != "All"){
		objForm.cat.value = prodN;
	}else if(prodF!=null && objForm.dProdFam.text != ""){
		objForm.cat.value = prodF;
	}
	*/
	if(objForm.cat.value!=null && objForm.cat.value!=""){
		objForm.submit();
	}
	
}
function fnSetLocale(strField) {
	if (strField!=null && strField.length>0 && document.question_form.language != undefined) {		
		if (strField.indexOf("ja_JP")>-1) {
			//strField = "ja_JA";
			strField = "ja_JP";

		}
		document.question_form.language.value = strField.replace('_','-');
		
	}
}
function fnSearchCommercial() {
	
	window.open("http://www.ge-ip.com/search?query=" + document.question_form.question_box.value, '_blank');
	//location.href="http://www.ge-ip.com/search?query=" + document.question_form.question_box.value;
}
