var isIE = false;
//var completeField;
//var autorow;
var req;

var onlyTwoOptions;
var onlyOneOption;




function disDrop(id){
		//This function  un-Disables a selections box in the side nav flyouts then focus's on it.
		if(id=='drop2.3' || id=='drop4.2'){
			document.getElementById(id).disabled = false;
			document.getElementById(id).focus;
		}
		if(id=='drop2.2'){
			var obj1 = document.getElementById("drop2.2");
			if(obj1!=null){obj1.options.length = 0;obj1.disabled = true;}
			var obj2 = document.getElementById("drop2.3");
			if(obj2!=null){obj2.options.length = 0;obj2.disabled = true;}
		}
		//if there are only 2 values disable the third drop down
		if(onlyTwoOptions=='true'){
			var obj2 = document.getElementById("drop2.3");
			if(obj2!=null){obj2.options.length = 0;obj2.disabled = true;}
			
				var obj1 = document.getElementById("drop2.2");
				var firstOptionSelected = obj1.selectedIndex;

				var bo = document.getElementById("bo_submit");
				var viewLink = document.getElementById("view_bus");

				//If the first option is selected make View Products Greyout
				if(firstOptionSelected==0){
					if(bo!=null){bo.disabled = true;}
					if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
					if(bo!=null){bo.disabled = false;}
					if(viewLink!=null){viewLink.style.color="white";viewLink.href="javascript:submitForm()";}				
				}

		}else{
		   if('drop2.3'==id){
			   fillThirdOptions();
		   }
		}
		
		if(id=='drop2.4'){
		
				var obj1 = document.getElementById("drop2.3");
				var firstOptionSelected = obj1.selectedIndex;

				var bo = document.getElementById("bo_submit");
				var viewLink = document.getElementById("view_bus");

				//If the first option is selected make View Products Greyout
				if(firstOptionSelected==0){
					if(bo!=null){bo.disabled = true;}
					if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
					if(bo!=null){bo.disabled = false;}
					if(viewLink!=null){viewLink.style.color="white";viewLink.href="javascript:submitForm()";}				
				}
		}
		if(id=='drop3.3'){
				var obj1 = document.getElementById("drop3.2");
				var firstOptionSelected = obj1.selectedIndex;
				var viewLink = document.getElementById("view_indus");
				var bo = document.getElementById("ind_submit");
				
				if(firstOptionSelected==0){
					if(bo!=null){bo.disabled = true;}
					if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
					if(bo!=null){bo.disabled = false;}
					if(viewLink!=null){viewLink.style.color="white";viewLink.href="javascript:submitIndForm()";}				
				}
		}
		if(id=='drop4.3'){
				var obj1 = document.getElementById("drop4.2");
				var firstOptionSelected = obj1.selectedIndex;
				var bo = document.getElementById("job_submit");
				var viewLink = document.getElementById("view_job");

				if(firstOptionSelected==0){
					if(bo!=null){bo.disabled = true;}
					if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
					if(bo!=null){bo.disabled = false;}
					if(viewLink!=null){viewLink.style.color="white";viewLink.href="javascript:submitJobForm()";}				
				}
		}
		
		
		
		//choose the second options
		   if('drop2.2'==id){
		   
		   		//if the user has selected "Select Business Objective" then 2nd option should be disabled
				var obj1 = document.getElementById("drop2.1");
				var firstOptionSelected = obj1.selectedIndex;
				var viewLink = document.getElementById("view_bus");

				if(firstOptionSelected==0){
						var obj2 = document.getElementById("drop2.3");
						if(obj2!=null){obj2.options.length = 0;obj2.disabled = true;}
						if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
						var drop2 = document.getElementById("drop2.2");
						if(drop2!=null){
							drop2.options.length = 0;drop2.disabled = false;
						}
				}
				
				//fill second options
			    fillSecondOptions();

			    if(onlyOneOption=='false'){	
					var bo = document.getElementById("bo_submit");
					if(bo!=null){bo.disabled = true;}
					if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
					var bo = document.getElementById("bo_submit");
					if(bo!=null){bo.disabled = false;}

					if(firstOptionSelected==0){
						if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
					}else{
						if(viewLink!=null){viewLink.style.color="white";viewLink.href="javascript:submitForm()";}				
					}					
				}

		   }

   		   if('drop3.2'==id){
   			    var obj1 = document.getElementById("drop3.1");
   			    var obj2 = document.getElementById("drop3.2");
				var firstOptionSelected = obj1.selectedIndex;

			   	if(firstOptionSelected==0){
						if(obj2!=null){obj2.options.length = 0;obj2.disabled = true;}
						var bo = document.getElementById("ind_submit");
						if(bo!=null){bo.disabled = true;}
						
						var viewLink = document.getElementById("view_indus");
						if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				
				}else{
						var drop2 = document.getElementById("drop3.2")
						if(drop2!=null){drop2.options.length = 0;drop2.disabled = false;}
				}
				
			    fillIndSecondOptions();	
			    

		   }

		   if('drop4.2'==id){
			    var obj1 = document.getElementById("drop4.1");
				var firstOptionSelected = obj1.selectedIndex;

				//If the first option is selected make View Products Greyout
			   	if(firstOptionSelected==0){
						var obj2 = document.getElementById("drop4.2");
						if(obj2!=null){obj2.options.length = 0;obj2.disabled = true;}
						var bo = document.getElementById("job_submit");
						if(bo!=null){bo.disabled = true;}

						var viewLink = document.getElementById("view_job");
						if(viewLink!=null){viewLink.style.color="gray";viewLink.removeAttribute("href");}				

				}else{
						var drop2 = document.getElementById("drop4.2")
						if(drop2!=null){drop2.options.length = 0;drop2.disabled = false;}
				}
			    fillJobSecondOptions();	
		   }
}
		
	//fill Third Options
	function fillThirdOptions(selObj1){
		var selObj1 = document.getElementById("drop2.2");
		var firstOption = document.getElementById("drop2.1");
		var firstOptionSelected = firstOption.options[firstOption.selectedIndex].value;
                
		if (selObj1.selectedIndex != 0){
			var selValue = selObj1.options[selObj1.selectedIndex].value;
            //to post + symbol properly
            selValue = selValue.replace("+", "%2B");

			//submitting to following servlet
			var url = "/portal/innovateactionservlet?action=thirdLevel&selected=" + selValue+"&firstOpt="+firstOptionSelected + "&rand=" + (new Date()).getTime();

	        var req = initRequest(url);
	        req.onreadystatechange = function() {
	            if (req.readyState == 4) {
	                if (req.status == 200) {
	                    parseOptionThree(req,req.responseXML);
	                } else if (req.status == 204){
	                }
	            }
	        };
	        req.open("GET", url, true);
	        req.send(null);
		}
	
	}

	function fillSecondOptions(selObj1){
		var obj1 = document.getElementById("drop2.2");
			
		var selObj1 = document.getElementById("drop2.1");
		var firstOptionSelected = selObj1.options[selObj1.selectedIndex].value;
		
		if (selObj1.selectedIndex != 0){
			var selValue = selObj1.options[selObj1.selectedIndex].value;
			var url = "/portal/innovateactionservlet?action=secondLevel&selected=" + selValue+"&firstOpt="+firstOptionSelected + "&rand=" + (new Date()).getTime();
	        var req = initRequest(url);
	        
	        if(isIE){
					req.onreadystatechange = function() {
		        
		            if (req.readyState == 4) {
		                if (req.status == 200) {
		                    parseOptionTwo(req,req.responseXML);
		                } else if (req.status == 204){
		                }
		            }
		        };
		        req.open("GET", url, false);
		        req.send(null);
						        
	        }else{
		        // Because Firefox doesn't work with responseXML, create a div
				// and put responseText in the innerHTML. This does not work with
				// IE 7.
				req.open("GET", url, false);
	        	req.send(null);
				
				var doc = document.createElement("div");
				doc.innerHTML = req.responseText;
				//alert(req.responseText);
				var xmlDocument = req.responseXML;
	            if(!xmlDocument || xmlDocument.childNodes.length==0){ 
	               xmlDocument = (new DOMParser()).parseFromString(req.responseText, "text/xml");
					if(xmlDocument.getElementsByTagName("objective")[0]!=undefined)                      
			            parseOptionTwo(req,xmlDocument);
	            }else{
			            parseOptionTwo(req,xmlDocument);
	            }
					        
	        
	        }
		}
	}

	function initRequest(url) {
	    
		/* Check for running connections */ 
		// if (requester != null && requester.readyState != 0 && requester.readyState != 4) 
		 //{ 
		   //requester.abort(); 
		 //} 

	    if (window.XMLHttpRequest) {
 	        return new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
	        isIE = true;
	        return new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }	


  function doCompletion(id) {
        var url = "/portal/innovateactionservlet?action=topLevel&id=" + id + "&rand=" + (new Date()).getTime();
        var req = initRequest(url);
        req.onreadystatechange = function() {
          
            if (req.readyState == 4) {
                if (req.status == 200) {
                    parseMessages(req,req.responseXML);
                } else if (req.status == 204){
                }
            }
        };
        req.open("GET", url, true);
        req.send(null);

}


	function parseMessages(req,responseXML) {
		var bos = responseXML.getElementsByTagName("bos")[0];
 	    var selObj = document.getElementById("drop2.1");
	  	selObj.options[0] = new Option("Select a Business Objective",0)
    
	    for (loop = 0; loop < bos.childNodes.length; loop++) {
			var bo = bos.childNodes[loop];
	        var boId = bo.childNodes[0];
	        var boName = bo.childNodes[1];
			 appendBusinessObjs(selObj,loop,boId.firstChild.nodeValue,boId.firstChild.nodeValue);
     	}
	}

	function parseOptionThree(req,responseXML) {
		var objective = responseXML.getElementsByTagName("objective")[0];
		var selObj = document.getElementById("drop2.3");
		selObj.options[0] = new Option("Select an Industry",0)
    
	    for (loop = 0; loop < objective.childNodes.length; loop++) {
		    var opt2 = objective.childNodes[loop];
		    var attributes = opt2.attributes;
		    var opt2Value = attributes.getNamedItem("value");
		    var concateString = "";

	     	for (loop1 = 0; loop1 < opt2.childNodes.length; loop1++) {
	    	    var products = opt2.childNodes[loop1];
			     	if(products.childNodes.length!=0){
						 var productURL_attributes = products.attributes;
	    				 var productURL_Value = productURL_attributes.getNamedItem("value").nodeValue;

	    				 if(concateString!="")
							concateString = concateString+"+"+productURL_Value;
						 else
						 	concateString = productURL_Value;	

							for (loop2 = 0; loop2 < products.childNodes.length; loop2++) {
							      	 var productPromos = products.childNodes[loop2];
							      	 var productPromos_attributes = productPromos.attributes;
				    				 var productPromosName = productPromos_attributes.getNamedItem("value").nodeValue;
				    				 var productPromosURL = '';
					    				 if(productPromos.firstChild!=null)
						    				 productPromosURL = productPromos.firstChild.nodeValue;

				    				 concateString = concateString+"+"+productPromosName+"+"+productPromosURL;
							}

						    appendThirdLevelOptions(selObj,loop,opt2Value.nodeValue,concateString);
				    }else{
					    var productURL_attributes = products.attributes;
	    				var productURL_Value = productURL_attributes.getNamedItem("value");
						appendThirdLevelOptions(selObj,loop,opt2Value.nodeValue,productURL_Value.nodeValue);
	    			}	    
		    }
     		selObj.options[0].selected = true;
     		
     	}
	}
	
	
	function parseDirectProducts(req,responseXML) {
		var objective = responseXML.getElementsByTagName("objective")[0];
	    var selObj = document.getElementById("drop2.1");
		var concateString = "";
	    
	     	for (loop1 = 0; loop1 < objective.childNodes.length; loop1++) {
	    	    var options = objective.childNodes[loop1];
				 if(concateString!="")
					concateString = concateString+"+"+options.childNodes[0].data;
				 else
				 	concateString = options.childNodes[0].data;	

				for (loop2 = 1; loop2 < options.childNodes.length; loop2++) {
				      	 var productPromos = options.childNodes[loop2];
						 var productPromos1 = productPromos.childNodes[0].data;
						 var productPromos_attributes = productPromos.attributes;
						 var productPromosName = productPromos_attributes.getNamedItem("value").nodeValue;
						 var productPromosURL = '';
							 if(productPromos.firstChild!=null)
								 productPromosURL = productPromos.firstChild.nodeValue;
						
						 concateString = concateString+"+"+productPromosName+"+"+productPromosURL;
				         selObj.options[selObj.selectedIndex].value = concateString;
				}
		    }
			var obj2 = document.getElementById("drop2.2");
			if(obj2!=null){obj2.options.length = 0;obj2.disabled = true;}
	}

	function parseOptionTwoXML(req,responseXML) {
			var objective = responseXML.getElementsByTagName("objective")[0];
	
		    var objective_attributes = objective.attributes;
		    var objective_option_value = objective_attributes.getNamedItem("twoValues").value;
		
			if(objective_option_value=='true'){
				onlyTwoOptions = 'true';   
			}else{
				onlyTwoOptions = 'false';   
			}
	
	   		var selObj = document.getElementById("drop2.2");

		    for (loop = 0; loop < objective.childNodes.length; loop++) {
			    var opt = objective.childNodes[loop];
			    var attributes = opt.attributes;
			    var optValue = attributes.getNamedItem("value").value;
			    //insert the top label//what industry etc
			    appendSecondLevelOptions(selObj,loop,optValue,'');
			     	for (loop1 = 0; loop1 < opt.childNodes.length; loop1++) {
			     	    var concateString = "";//String to concatenate url for the top product and the product promos with "+"
			     	
			    	    var options = opt.childNodes[loop1];
					     	if(options.childNodes.length!=0){//to check if there are any product promos
						      	var optTitle = options.childNodes[0].data;
							    var opt_attributes = options.attributes;
							    if(opt_attributes!=null){
							    	var optionsValue = opt_attributes.getNamedItem("value");
							    	optValue = optionsValue.value;//both are diff for child nodes

									if(concateString!=""){
										concateString = concateString+"+"+optTitle;	
									}else{
										concateString = optTitle;	
									}
                                                                        if (optValue != null && optValue != "" && optValue.charAt(0) != "5") {
                                                                            for (loop2 = 1; loop2 < options.childNodes.length; loop2++) {
                                                                                    if(options.childNodes.length>1){//to check if there are any product promos
                                                                                     var productPromos = options.childNodes[loop2];

                                                                                     if(productPromos!=null){
                                                                                             var productPromos_attributes = productPromos.attributes;
                                                                                             var productPromosName = productPromos_attributes.getNamedItem("value").nodeValue;
                                                                                             var productPromosURL = '';
                                                                                             if(productPromos.firstChild!=null)
                                                                                                     productPromosURL = productPromos.firstChild.nodeValue;

                                                                                             concateString = concateString+"+"+productPromosName+"+"+productPromosURL;
                                                                                     }

                                                                                    }
                                                                            }
                                                                        }

								   appendSecondLevelOptions(selObj,loop1+1,optValue,concateString);	
							    	
								}else{
									optValue = optTitle;//both are same for child nodes
									appendSecondLevelOptions(selObj,loop1+1,optValue,optTitle);
								}
						    }	    
				    }
		     		selObj.options[0].selected = true;
		     		
		     	}
	}

	function parseOptionTwo(req,responseXML) {
		var objective = responseXML.getElementsByTagName("objective")[0];
	
	    var objective_attributes = objective.attributes;
	    var objective_option_value = objective_attributes.getNamedItem("twoValues").value;
		var directProduct = objective_attributes.getNamedItem("directProduct");
		//if there is a direct product under objective
		if(directProduct!=null){
			onlyOneOption = 'true';
			parseDirectProducts(req,responseXML);
		}else{
			onlyOneOption = 'false';	
			parseOptionTwoXML(req,responseXML);
		}
	}

	//Adding to the drop down business objective
	function appendBusinessObjs(selObj,loop1,boId,boName) {
	    	selObj.options[loop1+1]=new Option(boName , boId);
	}

	//Adding to the drop down second level
	function appendSecondLevelOptions(selObj,loop1,optLabel,boName) {
	    	selObj.options[loop1]=new Option(optLabel , boName);
	}
	
	//Adding to the drop down Third level if there is one
	function appendThirdLevelOptions(selObj,loop1,opt2Value,url) {
	    	selObj.options[loop1+1]=new Option(opt2Value , url);
	}

	//To get the background images for industry
   function getImage(){
	   		var obj1 = document.getElementById("drop2.1");
	    	var obj2 = document.getElementById("drop2.2");
	    	var obj3 = document.getElementById("drop2.3");
	    	
	    	var firstOptionSelected ='';

	    	//only if two options are present, we know that second options are industries so get the selected one
	    	if(obj2.selectedIndex== -1){
	   		 	 firstOptionSelected = obj1.options[obj1.selectedIndex].innerHTML;
	    	}else if(obj3.selectedIndex== -1){
		   		  firstOptionSelected = obj2.options[obj2.selectedIndex].innerHTML;
	    	}else{
	    		firstOptionSelected = obj3.options[obj3.selectedIndex].innerHTML;
	    	}
	   		doImageCompletion(firstOptionSelected, null);
   }

	function submitForm(){
		//submits the flyout submission form. 
		//get the images
			getImage();
		    var selObj;
		    var val;
		    var prod1;var prod2;var prod3;var prod4;

			//if directly products are placed under objective
			if(onlyOneOption=='true'){
			    selObj = document.getElementById("drop2.1");
			    val = selObj.options[selObj.selectedIndex].value;
			}

			//if it has only two options then consider just first two values
			if(onlyTwoOptions=='true'){
			    selObj = document.getElementById("drop2.2");
			    val = selObj.options[selObj.selectedIndex].value;
			}else if(onlyTwoOptions=='false'){
				selObj = document.getElementById("drop2.3");
		    	val = selObj.options[selObj.selectedIndex].value;
		   	}

			if(val.indexOf('+') > -1){
		    	var some = val.split("+");
				val = some[0];	    	

			    prod1 = document.getElementById("prod1");
		    	if(prod1!=null){
			    	prod1.name = "prod1"+some[1];
			    	prod1.value = some[2];
		    	}
				//prod2
			    prod2 = document.getElementById("prod2");
		    	if(prod2!=null){
			    	prod2.name = "prod2"+some[3];
			    	prod2.value = some[4];
				}
				//prod3
			    prod3 = document.getElementById("prod3");
		    	if(prod3!=null){
			    	prod3.name = "prod3"+some[5];
			    	prod3.value = some[6];
				}
				//prod4
			    prod4 = document.getElementById("prod4");
		    	if(prod4!=null){
			    	prod4.name = "prod4"+some[7];
			    	prod4.value = some[8];
				}
				
			}
			//keep in hidden field BO
			selObj = document.getElementById("drop2.1");

			var textNode = document.createElement('input');
			textNode.type = 'hidden';
			textNode.name = 'business_object';
			textNode.id = 'business_object';
			textNode.value = selObj.options[selObj.selectedIndex].value;
		    var subform = document.getElementById("form");
		    subform.action = val;

		    subform.appendChild(textNode);

			document.getElementById("form").submit();

		}
		
		function closeForm(){
		//submits the flyout submission form. 
			window.location = "index.jsp";
		}
		
function trackRecommend(id) {
    if (id != null) {
        var mysel = document.forms['form'].elements[id];
        var seltxt = mysel.options[mysel.selectedIndex].text;
        if (id == 'drop2.1' || id == 'drop3.1' || id == 'drop4.1') {
            document.forms['form'].elements['recommendSelection1'].value = seltxt;
        } else if (id == 'drop2.2' || id == 'drop3.2' || id == 'drop4.2') {
            document.forms['form'].elements['recommendSelection2'].value = seltxt;
        } else if (id == 'drop2.3') {
            document.forms['form'].elements['recommendSelection3'].value = seltxt;
        }
    }
}


		
