// Jeevan 26/05/2006: Declaring the variable. Its checked in the checkSicCode function to see if sample size is fetched.
var isSampleSizeReady = false;

/*
	Added by Abhaya on 17/03/2006
	Done in order to disable link once report gen. link has been clicked once
*/
function cancelLink() {
  return false;
}

function disableLink (context,link) {
  if (link.onclick) {
    link.oldOnClick = link.onclick;
  }
  link.onclick = cancelLink;
  //send to action
  location.href = context+'/downloadReport.do';
  
  if (link.style) {
    link.style.cursor = 'default';
  }
}

//added by abhaya on 080306 to by pass updating DB each time a user goes throught TIT
function setUpdated() {
	
	if (document.forms[0].updated.value == 'NO') {
		document.forms[0].updated.value = 'YES';
	}
	
	return;
}

function saveFinancialServiceMiniAndExit(context) {
	document.forms[0].action = context+"/saveFinancialServiceMiniAndExit.do";
	document.forms[0].submit();
	return;}

function saveCustomerServiceMiniAndExit(context) {
	document.forms[0].action = context+"/saveCustomerServiceMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveProcessServiceMiniAndExit(context) {
	document.forms[0].action = context+"/saveProcessServiceMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveDevelopmentServiceMiniAndExit(context) {
	document.forms[0].action = context+"/saveDevelopmentServiceMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveFinancialManufacturingMiniAndExit(context) {
	document.forms[0].action = context+"/saveFinancialManufacturingMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveCustomerManufacturingMiniAndExit(context) {
	document.forms[0].action = context+"/saveCustomerManufacturingMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveProcessManufacturingMiniAndExit(context) {
	document.forms[0].action = context+"/saveProcessManufacturingMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveDevelopmentManufacturingMiniAndExit(context) {
	document.forms[0].action = context+"/saveDevelopmentManufacturingMiniAndExit.do";
	document.forms[0].submit();
	return;
}

function saveFinancialServiceMaxiAndExit(context) {
	document.forms[0].action = context+"/saveFinancialServiceMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveCustomerServiceMaxiAndExit(context) {
	document.forms[0].action = context+"/saveCustomerServiceMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveProcessServiceMaxiAndExit(context) {
	document.forms[0].action = context+"/saveProcessServiceMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveDevelopmentServiceMaxiAndExit(context) {
	document.forms[0].action = context+"/saveDevelopmentServiceMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveFinancialManufacturingMaxiAndExit(context) {
	document.forms[0].action = context+"/saveFinancialManufacturingMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveCustomerManufacturingMaxiAndExit(context) {
	document.forms[0].action = context+"/saveCustomerManufacturingMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveScoBmDetailsAndExit(context) {
//Added by Navin 11-06-2005: Save n Exit for Scottish Domain diagnostic page
//	document.forms[0].action = context+"/saveCustomerManufacturingMaxiAndExit.do";
	document.forms[0].action = context+"/saveScoBmDetailsAndExit.do";
	document.forms[0].submit();
	return;
}

function saveProcessManufacturingMaxiAndExit(context) {
	document.forms[0].action = context+"/saveProcessManufacturingMaxiAndExit.do";
	document.forms[0].submit();
	return;
}

function saveDevelopmentManufacturingMaxiAndExit(context) {
	document.forms[0].action = context+"/saveDevelopmentManufacturingMaxiAndExit.do";
	document.forms[0].submit();
	return;
}



function formatEmps()
{
	if ((document.forms[0].EMP1.value == "NULL")&&(document.forms[0].EMP2.value == "NULL")) {
		document.forms[0].NO_EMPS.value="";
	} else if ((document.forms[0].EMP1.value != "NULL")&&(document.forms[0].EMP2.value == "NULL")) {
		document.forms[0].NO_EMPS.value = " >= " + document.forms[0].EMP1.value;
	} else if ((document.forms[0].EMP1.value == "NULL")&&(document.forms[0].EMP2.value != "NULL")) {
		document.forms[0].NO_EMPS.value= " <= " + document.forms[0].EMP2.value;
	} else {
		//Jeevan 26/05/2006: If valueOf(number of employees more than) > valueOf(number of employees less than) show error
		var emp1 = document.forms[0].EMP1.value;
		var emp2 = document.forms[0].EMP2.value;
		if(parseInt(emp1) > parseInt(emp2))
		{
			alert(document.getElementById("errEmpOneGrtTwo").value);
			document.forms[0].EMP1.options[0].selected = true;
			//document.forms[0].EMP2.options[0].selected = true;
		}
		//Jeevan 26/05/2006: If valueOf(number of employees more than) = valueOf(number of employees less than) show error
		else if(parseInt(emp1) == parseInt(emp2))
		{
			alert(document.getElementById("errEmpOneEqlTwo").value);
			document.forms[0].EMP2.options[0].selected = true;
		} else {
			document.forms[0].NO_EMPS.value=document.forms[0].EMP1.value + " - " + document.forms[0].EMP2.value;
		}
	}
	
}

function formatTurnover()
{
	if ((document.forms[0].TURN1.value == "NULL")&&(document.forms[0].TURN2.value == "NULL")) {
		document.forms[0].TURNOVER.value="";
	} else if ((document.forms[0].TURN1.value != "NULL")&&(document.forms[0].TURN2.value == "NULL")) {
		document.forms[0].TURNOVER.value = " >= " + document.forms[0].TURN1.value;
	} else if ((document.forms[0].TURN1.value == "NULL")&&(document.forms[0].TURN2.value != "NULL")) {
		document.forms[0].TURNOVER.value= " <= " + document.forms[0].TURN2.value;
	} else {
		//Jeevan 26/05/2006: If valueOf(turnover more than) > valueOf(turnover less than) show error
		var turn1 = document.forms[0].TURN1.value;
		var turn2 = document.forms[0].TURN2.value;
		if(parseFloat(turn1) > parseFloat(turn2))
		{
			alert(document.getElementById("errTurnOneGrtTwo").value);
			document.forms[0].TURN1.options[0].selected = true;			
		} 
		//Jeevan 26/05/2006: If valueOf(turnover more than) = valueOf(turnover less than) show error
		else if(parseFloat(turn1) == parseFloat(turn2))
		{
			alert(document.getElementById("errTurnOneEqlTwo").value);
			document.forms[0].TURN2.options[0].selected = true;
		}
		else
		{
			document.forms[0].TURNOVER.value=document.forms[0].TURN1.value + " - " + document.forms[0].TURN2.value;
		}
	}
}

function getCreateCompSICCode(context, action, status) {
	if (eval(status) == 1) {
		if (document.forms[0].ind1.selectedIndex != 0) {
			document.forms[0].action = context+"/"+action+".do";
			document.forms[0].submit();
			return true;
		}
	}
	if (eval(status) == 2) {
		if (document.forms[0].td1.selectedIndex != 0) {
			document.forms[0].action = context+"/"+action+".do";
			document.forms[0].submit();
			return true;
		}
	}
}

function getEditCompSICCode(context, action, status) {
	if (eval(status) == 1) {
		document.forms[0].action = context+"/"+action+".do";
		document.forms[0].submit();
		return true;
	}
	if (eval(status) == 2) {
		if (document.forms[0].td1.selectedIndex != 0) {
			document.forms[0].action = context+"/"+action+".do";
			document.forms[0].submit();
			return true;
		}
	}
}

function getCriteriaSICCode(context, action, status) {
	if (eval(status) == 1) {
		if (document.forms[0].ind1.selectedIndex != 0) {
			document.forms[0].action = context+"/"+action+".do";
			document.forms[0].submit();
			return true;
		}
	}
	if (eval(status) == 2) {
		if (document.forms[0].td1.selectedIndex != 0) {
			document.forms[0].action = context+"/"+action+".do";
			document.forms[0].submit();
			return true;
		}
	}
}

function getSicCodeList(context, val) {

		if (eval(val) == 2) {
			document.forms[0].siccode.value ="";											
			if (document.forms[0].list1.selectedIndex != 0) {
				document.forms[0].action = context+"/getSicCode2.do";
				document.forms[0].list.value=val;
				document.forms[0].submit();
			} 
		}

		if (eval(val) == 3) {
			document.forms[0].siccode.value ="";											
			if (document.forms[0].list2.selectedIndex != 0) {
				document.forms[0].action = context+"/getSicCode3.do";
				document.forms[0].list.value=val;
				document.forms[0].submit();
			} 
		}
		
		if (eval(val) == 4) {
			if (document.forms[0].list3.selectedIndex != 0) {
				document.forms[0].siccode.value =  document.forms[0].list3.value;
				document.forms[0].list.value=val;				
				//document.forms[0].action = context+"/nextAction.do";
				//document.forms[0].submit();
			}
			else
			{
				document.forms[0].siccode.value ="";												
			}
		}

		return true;
}

function getCriteriaRegion(context, action, status) {
	if (eval(status) == 1) {
		if (document.forms[0].COUNTRY.selectedIndex != 0) {
			document.forms[0].action = context+"/"+action+".do";
			document.forms[0].submit();
			return true;
		}
	}
}

function invokeAction(context, action, type) {
	document.forms[0].action = context+"/"+action+".do?type="+type;
	document.forms[0].submit();
	return true;
}

var sicOpen = false;
function displaySICWindow(context) {
        // Open a popup-window when user clicks	"Search sic" link 
		if (window.sicWin && !sicWin.closed) 
		{
			sicWin.focus();
		}	
		else
		{
			var busiareas = document.getElementById("INDUSTRY").value;
			sicWin=	window.open(context+"/searchSicCode1.do?list=1&ba="+busiareas,"","toolbar=no,menubar=no,scrollbars=no,resizable=no,status=yes,location=no,directories=no,copyhistory=no,height=420,width=560,top=80,left=200");
			sicOpen = true;								
		}			
  }
  
  function searchSicCodeList(context, val) {		
  		//Jeevan: get the selected business area
  		var busiareas = window.opener.document.getElementById("INDUSTRY").value;
		if (eval(val) == 2) {			
			if (document.forms[0].list1.selectedIndex != 0) {
				document.forms[0].action = context+"/searchSicCode2.do?ba="+busiareas;
				document.forms[0].list.value=val;
				document.forms[0].submit();
			} 
		}

		if (eval(val) == 3) {
			if (document.forms[0].list2.selectedIndex != 0) {
				document.forms[0].action = context+"/searchSicCode3.do?ba="+busiareas;
				document.forms[0].list.value=val;
				document.forms[0].submit();
			} 
		}
		
		return true;
 }
 
 // Jeevan 340: For selecting more than one SIC codes
  function clearSicCodes()
  {
  		var confirmDel = confirm(document.forms[0].clearalert_hidden.value);
  		if(confirmDel == true)
  		{
  			document.forms[0].SIC.value = "";
  		}
  		document.forms[0].SIC.focus();
  } 
 
 // Jeevan 340: For selecting more than one SIC codes
  function getSelectedSicCodes()
  {
  		var prevSIC = window.opener.document.forms[0].SIC.value;
  		var newSIC = document.forms[0].siccode.value;
  		
  		if(newSIC.length < prevSIC.length)
  		{
  			document.forms[0].siccode.value = window.opener.document.forms[0].SIC.value;
  		}
  }
  
 // Jeevan 340: For selecting more than one SIC codes
 function selectSICCodeOnClose() 
 {
 	window.opener.document.forms[0].SIC.value = document.forms[0].siccode.value;
 	window.close();
    window.opener.document.forms[0].SIC.focus();
 }
 
/* Jeevan: 
  function selectSICCodeOnClose() {
		 	if (document.forms[0].list3.selectedIndex != 0) {
		 		var selSIC = document.forms[0].siccode.value;
		 		var prevSIC = window.opener.document.forms[0].SIC.value;
			 	var v_temp = prevSIC.indexOf(selSIC);
			 	if(v_temp == -1)
			 	{
		             if (prevSIC.length >  0)
	              	 { 
	              	 	// Check to see if the SIC code is previously selected
	              	 	var prev_arr = prevSIC.split(" ");
	              	 	var sic_codes = document.forms[0].siccode.value;
	              	 	var sic_arr = sic_codes.split(" ");
	              	 	
	              	 	for(i=0; i<sic_arr.length; i++)
	              	 	{
	              	 		for(j=0; j<prev_arr.length; j++)
	              	 		{	              	 			
	              	 			if(sic_arr[i] == prev_arr[j])
	              	 			{
	              	 				break;      	 			
	              	 			}             	 	
	              	 		}
	              	 		if(j == prev_arr.length)
	              	 		{
	              	 			// SIC Not present in the list already
	              	 			window.opener.document.forms[0].SIC.value += sic_arr[i] + " ";
	              	 		}
	              	 	}	              	 	
     				 }
     				 else 
     				 {
	 	                window.opener.document.forms[0].SIC.value = selSIC;
	 				 }
	 				 window.close();
		             window.opener.document.forms[0].SIC.focus();
		        }	            
			}			
 }
 */
 
 // Jeevan 340: For selecting more than one SIC codes
 function selectSICCode() 
 {
	 	if (document.forms[0].list3.selectedIndex != 0) 
	 	{
	 		var selSIC = document.forms[0].list3.value;
	 		var prevSIC = document.forms[0].siccode.value;
	 			 		
	 		// Select All selected
	 		if(selSIC == 1)
	 		{
	 			selSIC = "";
	 			//alert("Inside Select All");
	 			for(i=2; i<document.forms[0].list3.length; i++)
	 			{
	 				selSIC += document.forms[0].list3[i].value + " "; 				
	 			}	
	 			//alert("Selected: " + selSIC);	 				
	 		}		 		
	 		else
	 		{
	 			selSIC = "";
	 			//alert("Not select all");
	 			for(i=2; i<document.forms[0].list3.length; i++)
	 			{		 					 				
	 				if(document.forms[0].list3[i].selected == true)
	 				{
	 					selSIC += document.forms[0].list3[i].value + " ";				 					
			        }
				}
				//alert("Selected: " + selSIC);
	        }
	        
	        //alert("Previous length: " + prevSIC.length);
	        
	        // Check to see if the SIC code is previously selected
	        if(prevSIC.length == 0)
	        {
		        // Check if limit of 25 SIC codes has reached
	    	 	if(selSIC.length > 150)
	    	 	{
	    	 		alert(window.opener.document.forms[0].maxalert_hidden.value);
	    	 	}
	    	 	else
	    	 	{
	        		document.forms[0].siccode.value = selSIC;
	        	}
	        }      	 	
	        else
	        {
	      	 	var prev_arr = prevSIC.split(" ");          	 	
	      	 	var sic_arr = selSIC.split(" ");
		        
		        //alert("Prev sic codes: "+prev_arr.length);
		        //alert("Selected sic codes: "+sic_arr.length);
		        
		        for(i=0; i<sic_arr.length; i++)
	      	 	{
	      	 		for(j=0; j<prev_arr.length; j++)
	      	 		{	              	 			
	      	 			//alert("Prev: "+prev_arr[j] + " " +"Sel: "+sic_arr[i]);
	      	 			if(sic_arr[i] == prev_arr[j])
	      	 			{
	      	 				break;      	 			
	      	 			}             	 	
	      	 		}
	      	 		if(j == prev_arr.length)
	      	 		{
	      	 			// Check if limit of 25 SIC codes has reached	      	 			
	      	 			var len_chk = document.forms[0].siccode.value;
	      	 			if(len_chk.length == 150)
	      	 			{
	      	 				alert(window.opener.document.forms[0].maxalert_hidden.value);
	      	 				break;
	      	 			}
	      	 			else
	      	 			{
	      	 				// SIC Not present in the list already
	      	 				document.forms[0].siccode.value += sic_arr[i] + " ";	 					
	      	 			}
	      	 		}
	      	 	}	
	     	}
      	}
 }

function popPdf(url,title) {
	var OpenWindow;
	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers || document.getElementById)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax=800, yMax=600;

    var xOffset = (xMax - 800)/2, yOffset = ((yMax - 600)/2);
	
    var OpenWindow = window.open(url,'','width=794,height=546,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=1,top='+yOffset+',left='+xOffset+'')
    
		if (window.focus) {
			OpenWindow.focus();
	}
}

function saveTranslation() {
	document.forms[0].navigation.value="SAVE";
	document.forms[0].submit();
	return;
}

function continueTranslation() {
	document.forms[0].navigation.value="CONTINUE";
	document.forms[0].submit();
	return;
}

function autoCommit(servlet) {
	alert(servlet);
	if(servlet.indexOf('I18n.do') != -1) {
		timerID=setTimeout('doCommit()',5000)
		
	}
}
 function doCommit() {
 	//document.forms[0].action
 	
 	alert('commit done');
 	
 }


 var req;
 var which;
 var ajaxSection = 0;
 function getSampleSize(context) 
 {
 	// Jeevan 26/05/2006: This variables is set to true after AJAX has returned the sample size
 	isSampleSizeReady = false;
    var country = getCountryInfo();	
    var numEmp = formatEmpsSampleSize();
    var turnover = formatTurnoverSampleSize();
    var sic = document.getElementById("SIC").value;
    var industries = document.getElementById("IND_DESC").value;    
    var hratio = getHratio();
    
    // Jeevan 20/04/2007: Commenting out the compId, not required anymore for Sample Size
    //var comp = document.getElementById("compId").value;
    //var qryStr = "comp="+escape(comp)+"&country="+escape(country)+"&numEmp="+escape(numEmp)+"&turnover="+escape(turnover)+"&sic="+escape(sic)+"&hratio="+escape(hratio)+"&industries="+escape(industries); 
    
    var qryStr = "country="+escape(country)+"&numEmp="+escape(numEmp)+"&turnover="+escape(turnover)+"&sic="+escape(sic)+"&hratio="+escape(hratio)+"&industries="+escape(industries);
    if (document.forms[0].REGION != undefined) {
    	var reg = getRegionInfo();
    	qryStr += "&regions="+escape(reg);
    	//alert(qryStr);
    }
    ajaxSection = 1;
    
    retrieveURL(context+"/getSampleSize.do?"+qryStr);
 }
 
// Added by Navin on 17/01/2006 for getting sample size for scottish domain
 function getSampleSizeSco(context) 
 {
 	// Jeevan 26/05/2006: This variables is set to true after AJAX has returned the sample size 
    isSampleSizeReady = false;
    var country = 'UK';	
    var numEmp = formatEmpsSampleSize();
    var turnover = formatTurnoverSampleSize();
    var sic = document.getElementById("SIC").value;
    var industries = document.getElementById("IND_DESC").value;    
    var hratio = document.getElementById("HRATIO_B").value;
    
    // Jeevan 20/04/2007: Commenting out the compId, not required anymore for Sample Size
    //var comp = document.getElementById("compId").value;
    //var qryStr = "comp="+escape(comp)+"&country="+escape(country)+"&numEmp="+escape(numEmp)+"&turnover="+escape(turnover)+"&sic="+escape(sic)+"&hratio="+escape(hratio)+"&industries="+escape(industries);
    
    var qryStr = "country="+escape(country)+"&numEmp="+escape(numEmp)+"&turnover="+escape(turnover)+"&sic="+escape(sic)+"&hratio="+escape(hratio)+"&industries="+escape(industries);
    if (document.forms[0].REGION != undefined) {
    	var reg = getRegionInfoSco();
    	qryStr += "&regions="+escape(reg);
    }
    ajaxSection = 1;
    
    retrieveURL(context+"/getSampleSizeSco.do?"+qryStr);
 }
 
 
 
 function calculateCurrentTotalTurnover(context) {
 	var hf1 = document.getElementById("HF1").value;
 	var hf2 = document.getElementById("HF2").value;
 	var qryStr = "hf1="+escape(hf1)+"&hf2="+escape(hf2);
 	ajaxSection = 2;
 	retrieveURL(context+"/getLocalisedTurnover.do?"+qryStr);
 }	
 
 
 function calculatePreviousTotalTurnover(context) {
 	var hf1 = document.getElementById("GHF1").value;
 	var hf2 = document.getElementById("GHF2").value;
 	var qryStr = "hf1="+escape(hf1)+"&hf2="+escape(hf2);
 	ajaxSection = 3;
 	retrieveURL(context+"/getLocalisedTurnover.do?"+qryStr);
 }
 
 
 function retrieveURL(url) {
    if (window.XMLHttpRequest) { // Non-IE browsers
      req = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE  
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    try {
      req.open("GET", url, true);
    } catch (e) {
      //alert(e);
    }
    req.onreadystatechange = processStateChange;
    //req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(null);
  }
  
  function processStateChange() {
    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
        switch (ajaxSection) {
        	case 1:	document.getElementById("sampleSize").innerHTML = req.responseText;
		        	isSampleSizeReady = true;
        			break;
        	case 2: document.getElementById("HF28").value = req.responseText;
        			break;
        	case 3: document.getElementById("GHF28").value = req.responseText;
        			break;
        	case 4: doRegions(req.responseText);
        			break;
      	}
      } else {
        ;//alert("Problem: " + req.status);
      }
    }
  }
  
  function getCountryInfo() {
  	if(document.forms[0].COUNTRY.options[0].value == 0) 
  	{
  		return 'all';
  	}  	
	var countryList = '';
	for(i=0;i<document.forms[0].COUNTRY.options.length;i++) {
		if(document.forms[0].COUNTRY.options[i].selected) {
			countryList += document.forms[0].COUNTRY.options[i].value + ',';
		}
	}
	countryList = countryList.substring(0, (countryList.length-1));	
  	return countryList;
  }
  
  function getRegionInfo() {
  	if(document.forms[0].REGION.options[0].value == 0) 
  	{
  		return 'all';
  	}
  	
  	var regionList = '';
	for(i=0;i<document.forms[0].REGION.options.length;i++) {
		if(document.forms[0].REGION.options[i].selected) {
			regionList += document.forms[0].REGION.options[i].value + ',';
		}
	}
	regionList = regionList.substring(0, (regionList.length-1));
  	
  	return regionList;
  }
  
  function getRegionInfoSco() {
  	//Added by Navin on 17/01/2005 to get RegionInfo for Scottish domain
  	if(document.forms[0].REGION.options[0].selected) {
  		return 'all';
  	}
  	var regionList = '';
  	for(i=0;i<document.forms[0].REGION.options.length;i++) {
  		if(document.forms[0].REGION.options[i].selected) {
  			regionList += document.forms[0].REGION.options[i].value + ',';
  		}
  	}
  	regionList = regionList.substring(0, (regionList.length-1));
  	return regionList;
  }
  
  function formatEmpsSampleSize()
{
	var val = ""
	
	if ((document.forms[0].EMP1.value == "NULL")&&(document.forms[0].EMP2.value == "NULL")) {
		val="";
	} else if ((document.forms[0].EMP1.value != "NULL")&&(document.forms[0].EMP2.value == "NULL")) {
		val = " >= " + document.forms[0].EMP1.value;
	} else if ((document.forms[0].EMP1.value == "NULL")&&(document.forms[0].EMP2.value != "NULL")) {
		val = " <= " + document.forms[0].EMP2.value;
	} else {
		val =document.forms[0].EMP1.value + " - " + document.forms[0].EMP2.value;
	}
	return val;
}

function formatTurnoverSampleSize()
{
	var val = "";
	if ((document.forms[0].TURN1.value == "NULL")&&(document.forms[0].TURN2.value == "NULL")) {
		val="";
	} else if ((document.forms[0].TURN1.value != "NULL")&&(document.forms[0].TURN2.value == "NULL")) {
		val = " >= " + document.forms[0].TURN1.value;
	} else if ((document.forms[0].TURN1.value == "NULL")&&(document.forms[0].TURN2.value != "NULL")) {
		val = " <= " + document.forms[0].TURN2.value;
	} else {
		val = document.forms[0].TURN1.value + " - " + document.forms[0].TURN2.value;
	}
	return val;
}

function getHratio() {
	var val = 0;
	for(i=0;i<document.forms[0].HRATIO_B.length;i++) {
		if(document.forms[0].HRATIO_B[i].checked) {
			val = document.forms[0].HRATIO_B[i].value;
			break;
		}
	}
	return val;
}

var sicOpen = false;
function displayINDUSTRYWindow(context) {
        // Open a popup-window when user clicks	"BUSINESS AREAS" link 
		if (window.sicWin && !sicWin.closed) 
		{
			sicWin.focus();
		}	
		else
		{
			var sicCodes = document.getElementById("SIC").value;
			sicWin=	window.open(context+"/searchIndustries.do?sic="+sicCodes,"","toolbar=no,menubar=no,scrollbars=no,resizable=no,status=yes,location=no,directories=no,copyhistory=no,height=360,width=560,top=80,left=200");
			sicOpen = true;
		}			
}

// Jeevan: In the pop-up of Business areas, mark the ones which are already selected.
function markSelectedBusinessAreas()
{
  		var prevBA = window.opener.document.forms[0].INDUSTRY.value;
		var prevBA_arr = prevBA.split(" ");  	
		
		//alert("Selected BAs: "+prevBA_arr.length);
		for(var i=0; i<prevBA_arr.length; i++)	
		{
			for(var j = 0; j < document.forms[0].industry.options.length; j++)
			{				
  		  		if(document.forms[0].industry.options[j].value == prevBA_arr[i]) 
  		  		{
  		  			//alert(document.forms[0].industry.options[j].value);
  		  			document.forms[0].industry.options[j].selected = true;
  				}
  			}
  		}
}

// Jeevan: function added to clear the selected business areas
function clearBusinessAreas()
{
	var confirmDel = confirm(document.forms[0].clearbusialert_hidden.value);
  	if(confirmDel == true)
  	{
  		document.getElementById("IND_DESC").value = "";
  		document.forms[0].INDUSTRY.value = "";  		  		
  		document.forms[0].INDUSTRY.focus();
  	}
}

// Jeevan: clear the IND_DESC which is used to calculate sampleSize
function updateIndDescField()
{	
	if(document.forms[0].INDUSTRY.value == "")
	{
		document.getElementById("IND_DESC").value = "";
	}
}

function selectIndustryCode() {

  var industryString = "";
  var hiddenString = "";
  
  for(var counter = 0; counter < document.forms[0].industry.options.length; counter++){
  
  	if(document.forms[0].industry.options[counter].selected) {
  		industryString += document.forms[0].industry.options[counter].value  + " ";  		
		hiddenString += "/" + document.forms[0].industry.options[counter].value + "/,";
  	}
}
	
  window.opener.document.forms[0].INDUSTRY.value = industryString;  
  window.location.reload();
  window.close();
    
  // POPULATE HIDDEN STRING IN FORM
  window.opener.document.forms[0].IND_DESC.value = hiddenString;
  window.opener.document.forms[0].INDUSTRY.focus();
}

 
var ajaxSection = 0;
function getRegions(context) {
    var country = getCountryInfo();
    var qryStr = "country="+escape(country);
    
    ajaxSection = 4;
    retrieveURL(context+"/getCriteriaRegionsAjax.do?"+qryStr);
}
 
 
function doRegions(regStr) {
   	regStr = regStr.replace(/^\s*|\s*$/g,"");    
    var tempCode='';
	var tempVal='';
	var count = eval(regStr.substring(0,regStr.indexOf(",")));
	
	var comma = 1;
	if (count >= 10)
		comma = 2;
	
	document.forms[0].REGION.options.length = 1;
	
	for(i=1;i<=count;i++) {
        var code = regStr.substring(comma+1,regStr.length);
        var comma = code.indexOf(',');
        tempCode = code.substring(0, comma);
        var val = code.substring(comma+1);
        if (i == count) {
            tempVal = val.substring(0);
        } else {
             comma = val.indexOf(',');
             tempVal = val.substring(0,comma);
        }
        var opt = new Option(tempVal, tempCode);
        document.forms[0].REGION.options[i] = opt;
        if (i < count) {
            regStr = val.substring(comma+1);
            comma = -1;
            tempCode='';
            tempVal='';
        }
    }
}
    
function populateSelectedCountries() {

	var tmpString = '';

	for(i=0; i<document.forms[0].COUNTRY.options.length; i++) {
		if(document.forms[0].COUNTRY.options[i].selected) {
			tmpString += document.forms[0].COUNTRY.options[i].value + ",";
		}
	}
	document.forms[0].COUNTRY_HIDDEN.value = tmpString;
	
}

// Jeevan: 102 - To display contextual help in the Super user page.
function displayContextualHelp(selection)
{			
	switch (selection) 
	{
       	case 1:	document.getElementById("contextHelp").innerHTML = document.forms[0].help1.value;
       			break;
       	case 2: document.getElementById("contextHelp").innerHTML = document.forms[0].help2.value;
       			break;
       	case 3: document.getElementById("contextHelp").innerHTML = document.forms[0].help3.value;
       			break;
       	case 4: document.getElementById("contextHelp").innerHTML = document.forms[0].help4.value;
       			break;       			
       	case 5: document.getElementById("contextHelp").innerHTML = document.forms[0].help5.value;
       			break;		
       	case 6: document.getElementById("contextHelp").innerHTML = document.forms[0].help6.value;
       			break;
       	case 7: document.getElementById("contextHelp").innerHTML = document.forms[0].help7.value;
       			break;
       	case 8: document.getElementById("contextHelp").innerHTML = document.forms[0].help8.value;
       			break;
       	case 9: document.getElementById("contextHelp").innerHTML = document.forms[0].help9.value;
       			break;
       	case 10: document.getElementById("contextHelp").innerHTML = document.forms[0].help10.value;
       			break;
       	case 11: document.getElementById("contextHelp").innerHTML = document.forms[0].help11.value;
       			break;
       	case 12: document.getElementById("contextHelp").innerHTML = document.forms[0].help12.value;
       			break;
       	case 13: document.getElementById("contextHelp").innerHTML = document.forms[0].help13.value;
       			break;
       	case 14: document.getElementById("contextHelp").innerHTML = document.forms[0].help14.value;
       			break;
       	case 15: document.getElementById("contextHelp").innerHTML = document.forms[0].help15.value;
       			break;       		
       	case 99: document.getElementById("contextHelp").innerHTML = "";
       			break;			
  	}
}

// Jeevan: To display a popup for choosing country and regions
var sicOpen = false;
function displayCountryRegionWindow(context) 
{
    // Open a popup-window when user clicks	"Search sic" link 
	if (window.sicWin && !sicWin.closed) 
	{
		sicWin.focus();
	}	
	else
	{		
		sicWin = window.open(context+"/popupCountryRegions.do?country=all","","toolbar=no,menubar=no,scrollbars=no,resizable=no,status=yes,location=no,directories=no,copyhistory=no,height=360,width=350,top=80,left=200");		
		sicOpen = true;								
	}			
}
 
// Jeevan: get the selected countries and regions from the criteria page and populate the 'selected country' and 'selected region' in popup window
function getSelectedCountryAndRegions()
{
	var selCon = window.opener.document.forms[0].COUNTRY.length;
	var selReg = window.opener.document.forms[0].REGION.length; 
	var i = 0;
		
	// Fetch countries selected from the main page
	if(window.opener.document.forms[0].COUNTRY.options[0].value != 0)	
	{	
		for(i=0; i<selCon; i++)
		{	
			document.forms[0].selCountry.options[i] = new Option(window.opener.document.forms[0].COUNTRY.options[i].text, window.opener.document.forms[0].COUNTRY.options[i].value);	
		}
	}
	
	// fetch regions selected from the main page
	if(window.opener.document.forms[0].REGION.options[0].value != 0)	
	{	
		for(i=0; i<selReg; i++)
		{
			document.forms[0].selRegions.options[i] = new Option(window.opener.document.forms[0].REGION.options[i].text, window.opener.document.forms[0].REGION.options[i].value);
		}
	}
}

// Jeevan: Retrieve all the regions for the selected country after clicking 'get regions' link
function retrieveRegions(context)
{
	// Mark the entries as selected, so that the action can pick the selections
	for(var i=0; i<document.forms[0].selCountry.options.length; i++)
	{
		document.forms[0].selCountry.options[i].selected = true;
	}
	for(var i=0; i<document.forms[0].selRegions.options.length; i++)
	{
		document.forms[0].selRegions.options[i].selected = true;
	}
	
	document.forms[0].submit();
}

// Jeevan: Select the chosen countries and regions in POPUP
// Used to copy countries and regions in the POPUP window
function selectCountriesAndRegions()
{  		
	// clear the country and region boxes
	//clearAllCountryAndRegions();
	
	// Checking if both contry and regions boxes have some values to transfer to the selected boxes
	if(document.forms[0].country.options.length > 0 && document.forms[0].regions.options.length > 0)
	{
		// GETTING THE SELECTED COUNTRIES
		for(var i=0; i<document.forms[0].country.options.length; i++)	
		{
			if(document.forms[0].country.options[i].selected == true) 
		  	{	  		
				for(var j=0; j<document.forms[0].selCountry.options.length; j++)
				{
					// This country is already present in the selected countries
					if(document.forms[0].country.options[i].value == document.forms[0].selCountry.options[j].value)
					{
						break;
					}
				}
				// Need to add this country to the selected countries
				if(j == document.forms[0].selCountry.options.length)
				{
					document.forms[0].selCountry.options[j] = new Option(document.forms[0].country.options[i].text, document.forms[0].country.options[i].value);			
				}
		  	}
		}
		  	
	  	// GETTING THE SELECTED REGIONS
	  	// All regions option is selected
	  	if(document.forms[0].regions.options[0].selected)
	  	{
	  		for(var i=1; i<document.forms[0].regions.options.length; i++)	
			{
				for(var j=0; j<document.forms[0].selRegions.options.length; j++)
				{
					// This region is present in the selected regions
					if(document.forms[0].regions.options[i].value == document.forms[0].selRegions.options[j].value)
					{
						break;
					}			
				}
				// Need to add this region to the selected regions
				if(j == document.forms[0].selRegions.options.length)
				{
					document.forms[0].selRegions.options[j] = new Option(document.forms[0].regions.options[i].text, document.forms[0].regions.options[i].value);			
				}
			}
	  	}
	  	// Specific regions selected
		else
		{  	
		  	for(var i=1; i<document.forms[0].regions.options.length; i++)	
			{
				if(document.forms[0].regions.options[i].selected == true) 
			  	{	  		
					for(var j=0; j<document.forms[0].selRegions.options.length; j++)
					{
						// This region is present in the selected regions
						if(document.forms[0].regions.options[i].value == document.forms[0].selRegions.options[j].value)
						{
							break;
						}			
					}
					// Need to add this region to the selected regions
					if(j == document.forms[0].selRegions.options.length)
					{
						document.forms[0].selRegions.options[j] = new Option(document.forms[0].regions.options[i].text, document.forms[0].regions.options[i].value);			
					}
				}		
		  	}
		} // End of else
	} // End of if 'checking if both country and regions boxes have something to select'
}
 
// Jeevan: Revert the country and regions selections back to default, on criteria page
function clearCountryRegions()
{
	var confirmDel = confirm(document.forms[0].clearcountregsalert_hidden.value);
  	if(confirmDel == true)
  	{
		// Remove all countries
		for(var i=document.forms[0].COUNTRY.options.length; i>=0; i--)	
		{			
			document.forms[0].COUNTRY.options[i] = null;			
		}		
	  	
	  	// Remove all regions  	
		for(var i=document.forms[0].REGION.options.length; i>=0; i--)	
		{
			document.forms[0].REGION.options[i] = null;			
		}
		
		// Add all countries and all regions
		document.forms[0].COUNTRY.options[0] = new Option("All countries", 0, true, true);			
		document.forms[0].REGION.options[0] = new Option("All regions", 0, true, true);					
		document.forms[0].COUNTRY.focus();
  	}
}

// Jeevan: Clear the selected countries and regions, in POPUP
function clearSelectedCountriesAndRegions()
{
	// Remove selected countries, and remove the corresponding regions
	for(var i=0; i<document.forms[0].selCountry.options.length; i++)	
	{			
		if(document.forms[0].selCountry.options[i].selected)
		{
			var cCode = document.forms[0].selCountry.options[i].value;
			
			// Remove all regions for the selected country	
			for(var j=document.forms[0].selRegions.options.length-1; j>=0; j--)	
			{
				var rCode = document.forms[0].selRegions.options[j].value;
				
				// E.g. cCode = UK and rCode = UK123
				if(rCode.indexOf(cCode) != -1)
				{
					document.forms[0].selRegions.options[j] = null;			
				}
			}
			// Remove this country
			document.forms[0].selCountry.options[i] = null;			
		}		
	}
	
	// Remove the selected regions
	for(var i=document.forms[0].selRegions.options.length-1; i>=0; i--)	
	{
		if(document.forms[0].selRegions.options[i].selected)
		{
			document.forms[0].selRegions.options[i] = null;			
		}
	}
	
	// Check all the countries if they have atleast one region present in the regions box, if not, remove the country
	for(var i=document.forms[0].selCountry.options.length-1; i>=0; i--)	
	{			
		for(var j=document.forms[0].selRegions.options.length-1; j>=0; j--)
		{
			var cCode = document.forms[0].selCountry.options[i].value;
			var rCode = document.forms[0].selRegions.options[j].value;
				
			// E.g. cCode = UK and rCode = UK123
			if(rCode.indexOf(cCode) != -1)
			{				
				break;
			}
		}
		// no regions present for this country, remove it from the list
		if(j < 0)
		{
			document.forms[0].selCountry.options[i] = null;			
		}
	}	
}

// Jeevan: To Clear all the selections from the country and regions, in POPUP window
function clearAllCountryAndRegions()
{
	// Remove all countries
	for(var i=document.forms[0].selCountry.options.length; i>=0; i--)	
	{			
		document.forms[0].selCountry.options[i] = null;			
	}		
  	
  	// Remove all regions  	
	for(var i=document.forms[0].selRegions.options.length; i>=0; i--)	
	{
		document.forms[0].selRegions.options[i] = null;			
	}
}

// Jeevan: save the selected countries and regions from the popup window into the criteria page
function saveAndExitSelectedCountriesRegions()
{  	
	if((document.forms[0].selCountry.options.length > 0) && (document.forms[0].selRegions.options.length > 0))
	{		
		// Removing all the entries from the criteria page (country and regions field)		
		window.opener.document.forms[0].COUNTRY.options.length = 0;
		window.opener.document.forms[0].REGION.options.length = 0;
		
		// Copy all the selected countries from popup window onto the criteria page			
	  	for(var i=0; i<document.forms[0].selCountry.options.length; i++)
	  	{		  		
			// addCountry function defined in the createBMCriteriaDetails.jsp page
			// this is required for select box !!! the function to add new option must be in the same jsp page
			window.opener.addCountry(document.forms[0].selCountry.options[i].text, document.forms[0].selCountry.options[i].value);
		}
		
		// Copy all the selected regions from popup window onto the criteria page	  	
	  	for(var i=0; i<document.forms[0].selRegions.options.length; i++)
	  	{		
			// addRegion function defined in the createBMCriteriaDetails.jsp page
			// this is required for select box !!! the function to add new option must be in the same jsp page
			window.opener.addRegion(document.forms[0].selRegions.options[i].text, document.forms[0].selRegions.options[i].value);
		}			
	}	
	window.opener.document.forms[0].COUNTRY.focus();  	
  	window.close();  
}


// Jeevan: Generating popup in case of system maintenance
function maintenancePopup(context)
{
        document.getElementById("userId").disabled = true;
        document.getElementById("password").disabled = true;
        
        sicWin= window.open(context+"/systemError.jsp","","toolbar=no,menubar=no,scrollbars=no,resizable=no,status=yes,location=no,directories=no,copyhistory=no,height=240,width=560,top=80,left=200");
        sicOpen = true;        
}

//DARREN 09/02/2006: AUTOMATICALLY OPEN A POP-UP AFTER A PDF OUTPUT REPORT HAS BEEN GENERATED
function openPDFWindow(serverString,url,title){
	if(url == 1){
		/*var mine = window.open('','','width=1,height=1,left=1,top=1,scrollbars=no');
		if(mine != null) {
	    	var popUpsBlocked = false;
	    	mine.close();
	    }
	 	else {
		    var popUpsBlocked = true;
			alert(warningText);
		}*/
		;
	}
	else{
		popPdf(serverString+url,title);
	}
}

// Jeevan 08/05/2006: Check if the country and regions are all filled in. Used from Country/Region translation interface.
function chkIfCountryRegionIsEmpty()
{
	var countryRegNames = document.getElementById("countryRegNames").value;	
	var elementNames = countryRegNames.split(",");
	var isError = false;
	//alert(countryRegNames);
	//alert(elementNames.length);
	
	for(var i=0; i<elementNames.length; i++)
	{
		var val = document.getElementById(elementNames[i]).value;
		
		val = RTrim(val);
		val = LTrim(val);
		
		//alert(val);
				
		if(val == "")
		{
			isError = true;
			document.getElementById(elementNames[i]).value = "";			
		}
	}
	
	if(isError == false)
	{
		document.forms[0].submit();
	}	
	else
	{
		alert(document.getElementById("emptyErrorMsg").value);
	}
}

// Jeevan 09/05/2006: This function will trim off the Right spaces from the String
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
	{
		return"";
	}
	
	var iTemp = v_length -1;
	
	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;	
	} 
	
	return strTemp;
} 

// Jeevan 09/05/2006: This function will trim off the Left spaces from the String
function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	
	if(v_length < 1)
	{
		return"";
	}
	
	var v_length = VALUE.length;
	var strTemp = "";	
	var iTemp = 0;
	
	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	
	return strTemp;
} 

// Jeevan 26/05/2006: Function called from the Criteria page to check if there are any invalid SIC codes selected
function submitCheckInvalidSiccodes()
{	
	var chkSic = document.getElementById("sampleSize").innerHTML;	
	
	// This condidtion checks if the sample size function has executed and got back the sample size.
	// This is done as it might get back with invalid sic codes
	if(isSampleSizeReady == true)
	{		
		// If the sample size is a numeric value, then its a valid SIC code
		if(parseInt(chkSic) == chkSic)
		{	
			isSampleSizeReady = false;
			document.forms[0].submit();
		}
		else 
		{		
			alert(chkSic);
		}
	}
}


/*ADDED BY DARREN H 26/05/2006: ER-527-0031 REQUIRES BENCHMARK COMPANY TO POSSIBLY HAVE 2 SIC-CODES*/
/*THEREFORE NEED POP-UP TO SELECT THE SIC-CODES*/
var sicOpen = false;
function selectSicCode(context, nace) {
	
	/* GET VALUE OF SIC-CODE FROM THE FORM */
	var formNaceVal = document.getElementById(nace).value;

		if (window.sicWin && !sicWin.closed) 
		{
			sicWin.focus();
		}	
		else if(formNaceVal == '') {
			/*IF VALUE IN FORM IS EMPTY STRING THEN FORWARD USER TO TOP LEVEL SIC-CODE SELECTION JSP*/
			sicWin=	window.open(context+"/BMCompanyAndSic.do?list=1&nace="+nace,"","toolbar=no,menubar=no,scrollbars=no,resizable=no,status=yes,location=no,directories=no,copyhistory=no,height=300,width=560,top=40,left=400");
			sicOpen = true;	
		}
		else  {
			/* FORM CONTAINS A SIC-CODE. FORWARD USER TO THIRD LEVEL SIC-CODE SELECTION JSP */
			sicWin=	window.open(context+"/editBMCompanyNoCurrencyAndSic.do?list=1&nace="+nace+"&formNaceVal="+formNaceVal,"","toolbar=no,menubar=no,scrollbars=no,resizable=no,status=yes,location=no,directories=no,copyhistory=no,height=300,width=560,top=40,left=400");
			sicOpen = true;
		}			
}

/*ADDED BY DARREN H 26/05/2006: ER-527-0031 REQUIRES BENCHMARK COMPANY TO POSSIBLY HAVE 2 SIC-CODES*/
/*THIS FUNCTION SETS THE VALUE OF THE SELECTED SI-CODE, EG SIC1 OR SIC2, IN THE PARENT WINDOW FORM*/
function saveSelectedSICCode(nace) 
{
	/* GET VALUE OF SIC-CODE FROM THE PARENT WINDOW */
	var formNaceVal = window.opener.document.getElementById(nace).value;
	
	/* GET VALUE OF SIC-CODE FROM THE POP-UP */
	var sicCodeValue = document.forms[0].siccode.value;
	
	/* TRIM THE SIC-CODE STRING */
	sicCodeValue = LTrim(sicCodeValue);
	sicCodeValue = RTrim(sicCodeValue);
	
	/* IF NACE = NACE THEN THE USER HAS SELECTED sic1 */
	if(nace == 'nace'){
		/* IF VALUE OF SIC-CODE IN POP-UP IS AN EMPTY STRING THEN sic1 STAYS UNCHANGED IN THE PARENT WINDOW/FORM */
		if(sicCodeValue == '') {
			window.opener.document.getElementById("nace").value = formNaceVal;
		} else {
			/* ASSIGN SIC-CODE VALUE IN POP-UP TO THE PARENT WINDOW/FORM */
			window.opener.document.getElementById("nace").value = sicCodeValue;
		}
		
	} else { /* THE USER HAS SELECTED sic2 */
	
		if(sicCodeValue == '') {
			/* IF VALUE OF SIC-CODE IN POP-UP IS AN EMPTY STRING THEN sic1 STAYS UNCHANGED IN THE PARENT WINDOW/FORM */
			window.opener.document.getElementById("nace2").value = formNaceVal;
		} else {
			/* ASSIGN SIC-CODE VALUE IN POP-UP TO THE PARENT WINDOW/FORM */
			window.opener.document.getElementById("nace2").value = sicCodeValue;
		}
		
	}
	window.close();
} 

/* DARREN H 09/06/2006: ER-527-0020 FUNCTION CALLED IN HTML OUTPUT REPORT PAGES TO SEND USER BACK TO CRITERIA PAGE TO ALTER BENCHMARKING CRITERIA */
function backToCriteria(context, bmtype, count, beanmessage){
	var urlString = context+"/setupBMCriteria.do?category="+bmtype;
	
	if(count < 3){
		//SEND USER TO URL
		window.location=urlString;
	} else {
		//DISPLAY CONFIRM BOX TO USER
		var confirmString = confirm(beanmessage);
		if(confirmString == true){
			window.location=urlString;
		}
	}
	
}