var xmlHttp; 
var xmlHttp2;
var xmlHttp3; 
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
	
// XMLHttp send GET request 
function xmlHttp_Get(xmlhttp, url) 
{ 
	xmlhttp.open('GET', url, true); 
    xmlhttp.send(null); 
} 

function GetXmlHttpObject(whichBox) 
{ 
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
	if (is_ie)
	{ 
		//The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
             
        //Attempt to create the object 
        try
        { 
			objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = function() { materialSelectStateChangeHandler(whichBox) };
		} 
		catch(e)
		{ 
            //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
         } 
	} 
    else if (is_opera)
    { 
		//Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
	} 
    else
    { 
		// Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
	} 
         
	//Return the instantiated object 
	return objXmlHttp; 
}

function GetXmlHttpObject2() 
{ 
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
	if (is_ie)
	{ 
		//The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
             
        //Attempt to create the object 
        try
        { 
			objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = function() { materialSelectStateChangeHandler2() };
		} 
		catch(e)
		{ 
            //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
         } 
	} 
    else if (is_opera)
    { 
		//Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
	} 
    else
    { 
		// Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
	} 
         
	//Return the instantiated object 
	return objXmlHttp; 
}  

function GetXmlHttpObject3() 
{ 
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
	if (is_ie)
	{ 
		//The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
             
        //Attempt to create the object 
        try
        { 
			objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = function() { localAuthoritySelectStateChangeHandler() };
		} 
		catch(e)
		{ 
            //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
         } 
	} 
    else if (is_opera)
    { 
		//Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
	} 
    else
    { 
		// Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
	} 
         
	//Return the instantiated object 
	return objXmlHttp; 
}  

function closeFacebox()
{
	jQuery(document).trigger('close.facebox')
}

function openFacebox(url)
{
	jQuery.facebox({ ajax : url });
}
		
function populateSelectFromString(str, whichBox)
{
	var elSel = document.getElementById("ddlMaterial" + whichBox);
	elSel.options.length = 0;

	var arr = str.split(",");
	for(i=0; i<arr.length; i++)
	{
		var selectArr = arr[i].split(":");
		appendOptionLast(selectArr[0],selectArr[1], whichBox);
	}

	
	if(elSel.length > 1)
	{
		$("#divMaterialLbl" + whichBox).css("display","inline");
		$("#divMaterial" + whichBox).css("display","inline");
	}
}
		
function appendOptionLast(value, text, whichBox)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = text;
	if(value == "0")
	{
		elOptNew.value = "";
	}
	else
	{
		elOptNew.value = value;
	}
	
	var elSel = document.getElementById("ddlMaterial" + whichBox);

	try 
	{
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) 
	{
		elSel.add(elOptNew); // IE only
	}
}

function get_material_additional_data(parentID)
{
	var url = "MaterialUnitGetter.aspx?mID=" + parentID; 
		 
	//Create the xmlHttp object to use in the request 
	xmlHttp2 = GetXmlHttpObject2(); 
		             
	//Send the xmlHttp get to the specified url 
	xmlHttp_Get(xmlHttp2, url); 
}
		
function get_material_data(parentID, whichBox)
{ 
	var url = "MaterialGetter.aspx?mID=" + parentID + "&level=" + whichBox; 
		 
	//Create the xmlHttp object to use in the request 
	xmlHttp = GetXmlHttpObject(whichBox); 
		             
	//Send the xmlHttp get to the specified url 
	xmlHttp_Get(xmlHttp, url); 
}

function get_localauthority_data(parentID)
{ 
	var url = "LocalAuthorityGetter.aspx?laID=" + parentID; 
		 
	//Create the xmlHttp object to use in the request 
	xmlHttp3 = GetXmlHttpObject3(); 
		             
	//Send the xmlHttp get to the specified url 
	xmlHttp_Get(xmlHttp3, url); 
}
		
function materialSelectStateChangeHandler(whichBox) 
{ 
	//readyState of 4 or 'complete' represents that data has been returned 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{ 
		//Gather the results from the callback 
		var str = xmlHttp.responseText;
		populateSelectFromString(str, whichBox);
	} 
}

function materialSelectStateChangeHandler2() 
{ 
	//readyState of 4 or 'complete' represents that data has been returned 
	if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == 'complete')
	{ 
		//Gather the results from the callback 
		var str = xmlHttp2.responseText;
		var arr = str.split(";");
		if(arr.length >= 2)
		{
			$("#helpTextSpan").attr('innerHTML', arr[0]);
			$("#unitSpan").attr('innerHTML', arr[1]);
		}
	} 
}

function localAuthoritySelectStateChangeHandler() 
{ 
	//readyState of 4 or 'complete' represents that data has been returned 
	if (xmlHttp3.readyState == 4 || xmlHttp3.readyState == 'complete')
	{ 
		//Gather the results from the callback 
		var str = xmlHttp3.responseText;
		populateSelectFromString(str);
	} 
}


function isNumberKey(evt)
{
//alert(event.keyCode); //45
	var charCode = (evt.which) ? evt.which : event.keyCode
	if ((charCode > 31 && (charCode < 48 || charCode > 57)) && charCode != 46 && charCode != 45)
		return false;
	return true;
}

function isNumberAndRestrictDecimalPlaces(evt, val, decimalsAllowed)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if ((charCode > 31 && (charCode < 48 || charCode > 57)) && charCode != 46)
		return false;
	
	var index = val.value.indexOf(".");
	if(index != -1)
	{
		var subStr = val.value.substring(index+1); 
		if(subStr.length >= decimalsAllowed)
		{
			return false;
		}
	}
	
	return true;
}

function ShowSplash()
{
	tb_init('a.thickbox, area.thickbox, input.thickbox');		
	imgLoader = new Image();		
	imgLoader.src = tb_pathToImage;		
	tb_show("", "SplashPopup.aspx?KeepThis=true&TB_iframe=true&height=510&width=600",false);
	Sys.Application.remove_load(ShowSplash); 
}

function RemoveSplash()
{
	self.parent.tb_remove();
}

function ShowSite(url, querystring, baseURL)
{
	tb_init('a.thickbox, area.thickbox, input.thickbox');		
	imgLoader = new Image();		
	imgLoader.src = baseURL + "images/loadingAnimation.gif";		
	tb_show("", url + "?" + querystring + "&TB_iframe=true&height=600&width=800", false);
}

  function toggleDiv(divid, imageid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
       document.getElementById(imageid).src = 'Images/Expanded.gif';
    }else{
      document.getElementById(divid).style.display = 'none';
      document.getElementById(imageid).src = 'Images/Collapsed.gif';
    }
  }
  
  function toggleDivOff(divid, imageid){
    if(document.getElementById(divid).style.display != 'none')
    {
		document.getElementById(divid).style.display = 'none';
		document.getElementById(imageid).src = 'Images/Collapsed.gif';
    }
  }
  
  function toggleDivOn(divid, imageid){
    if(document.getElementById(divid).style.display != 'block')
    {
	   document.getElementById(divid).style.display = 'block';
       document.getElementById(imageid).src = 'Images/Expanded.gif';
    }
  }
  
  	function OpenWindow(url)
	{
		window.open(url, '', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=1000,height=700');
	}
	
 function validate_form()
 {
	if(typeof validate != 'undefined')
	{
		return validate();
	}
	else
	{
		return true;
	}
 }
 
  function find_checked_button(group) 
 {
	buttonFound = false;
	
	for (i = 0; i < group.length; i++)
	{
		if (group[i].checked)
		{ 
			buttonFound = true;
		}
	}
	
	return buttonFound;
}

function check_textbox_length(textbox,minlength,maxlength)
 {
	fieldComplete = true;
	
	if(minlength != null)
	{
		if(textbox.length < minlength)
		{
			fieldComplete = false;
		}
	}
	
	return fieldComplete;
 }
