window.onerror = HandleError
function HandleError(){
	return true;
}
var CellSplit;
var LastCellId
var LastClassName
function Displayformula(CellId,CurrentValue,DbName){
   var LastCellId=document.getElementById('LastCellClicked').value;
   if(LastCellId){
	   document.getElementById(LastCellId).className='interblockField';
   }
   LastClassName=document.getElementById(CellId).className
   document.getElementById('LastCellClicked').value=CellId;
   var xmlHttp;
   try 
    {   
	xmlHttp=new XMLHttpRequest();    
	}
    catch (e) 
    {   
	try 
	{      
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
	}
    catch (e)
    {      
	try
    {        
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
	}
    catch (e)
    {        
	alert("Your browser does not support AJAX!");        
	return false;       
	}      
	}    
	}
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
	ONUM=xmlHttp.responseText;
	if(!ONUM){
		document.getElementById('Formulae').value=("CONSTANT: "+CurrentValue);
		document.getElementById('FormulaFollowTxt').className="hidden";
		document.getElementById('FormulaFollowButtonContainer').className="hidden";
		ResetCells(CellId);
        document.getElementById(CellId).className="SelectedinterblockField";
    }else{
		document.getElementById('FormulaFollowTxt').className="hidden";
		document.getElementById('FormulaFollowButtonContainer').className="hidden";
		PreviewFormula(ONUM,CellId);
        document.getElementById(CellId).className="SelectedinterblockField";
	}
	}
	}
	var url="../Admin/RetreiveFormula.php?CellId="+CellId+"&CurrentValue="+CurrentValue+"&DbName="+DbName;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
}
function PreviewFormula(InStr,CellId){
	if(CellSplit){
		ResetCells(CellId);
	}
	var ValuesSplit=InStr.split("|");
	var Alias=ValuesSplit[0];
	var Calculation=ValuesSplit[1];
	var PageLink=ValuesSplit[2];
	var CellTargets=ValuesSplit[3];
	if(CellTargets){
		HighlightCells(CellTargets);
	}
	document.getElementById('Formulae').value=(Alias+"="+Calculation);
	if(PageLink){
		document.getElementById('FormulaTrace').value=PageLink;
		document.getElementById('FormulaFollowTxt').className="display";
		document.getElementById('FormulaFollowButtonContainer').className="display";
	}
}
function FollowFormula(){
	var PageLink=document.getElementById('FormulaTrace').value
	MM_goToURL('parent',PageLink);
	return document.MM_returnValue;
}
function HighlightCells(CellTargets){
	CellSplit=CellTargets.split(",");
	for (var i=0;i<CellSplit.length;i++) {
		var TargetInput=CellSplit[i];
		FieldCheck=document.getElementById(TargetInput);
		if(FieldCheck){
		document.getElementById(TargetInput).className="interblockFieldHighlighted";
		}
	}
}
function ResetCells(CellId){
	if(!CellSplit){
		return false;
	}
	for (var i=0;i<CellSplit.length;i++) {
		var TargetInput=CellSplit[i];
		FieldCheck=document.getElementById(TargetInput);
		if(FieldCheck){
		document.getElementById(TargetInput).className="interblockField";
		}
	}
	
	CellSplit.length=0;
	CellSplit="";
}
function CalculateChkBoxFormula(FormulaName,CurrentValue,Currentfield,obj){
	var SlopeSelected=document.getElementById('SlopeSelect').value
	var EqChk=document.getElementById('EqLoad');
	if((SlopeSelected==20)&&(EqChk.checked==1)){
		alert('Earthquake load combined with steep slopes requires a specific engineered design');
		obj.checked=0;
	}
	if(obj.checked==1){
		CalulateFormula(FormulaName,CurrentValue,Currentfield,'Custom');
	}else{
		CalulateFormula(FormulaName,'zero',Currentfield,'Custom');
	}
}
function CalculateVoidsOfBlocksFormula(FormulaName,CurrentValue,Currentfield,obj){
	if(obj.checked==1){
		CalulateFormula(FormulaName,CurrentValue,Currentfield,'Custom');
	}else{
		CalulateFormula(FormulaName,'zero',Currentfield,'Custom');
	}
}
var Custom;
function CalulateFormula(FormulaName,CurrentValue,Currentfield,Custom){
   if(!CurrentValue){
	   return false;
   }
   if(!Custom){
	   Custom="No";
   }
   var EqChk=document.getElementById('EqLoad');
   if((Currentfield=='calb19')&&(EqChk.checked==1)&&(CurrentValue==20)){
	   alert('Earthquake load combined with steep slopes requires a specific engineered design');
	   EqChk.checked=0;
   }
   var xmlHttp;
   try 
    {   
	xmlHttp=new XMLHttpRequest();    
	}
    catch (e) 
    {   
	try 
	{      
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
	}
    catch (e)
    {      
	try
    {        
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
	}
    catch (e)
    {        
	alert("Your browser does not support AJAX!");        
	return false;       
	}      
	}    
	}
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
	ONUM=xmlHttp.responseText;
	//alert(ONUM);return false;
	ReturnArray=ONUM.split("|");
	UpdateCells(ReturnArray);
	
	}
	}
	var url="../Admin/UpdateCalculations.php?Formula="+FormulaName+"&CurrentValue="+CurrentValue+"&Currentfield="+Currentfield+"&Custom="+Custom;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
}
function UpdateCells(ReturnArray){
	for (var i=0;i<ReturnArray.length;i++) {
		var TargetInput=ReturnArray[i+1];
		FieldCheck=document.getElementById(TargetInput);
		FieldValue=ReturnArray[i];
		if(FieldCheck){
			document.getElementById(TargetInput).value=ReturnArray[i];
		}
		var CautionField=in_array (TargetInput);
		if((CautionField==true)&&(ReturnArray[i]<1)){
			document.getElementById(TargetInput).className="InterBlockDisplayRedField";
		}
		else if((CautionField==true)&&(ReturnArray[i]>=1)){
			document.getElementById(TargetInput).className="InterBlockDisplayField";
		}
	}
}
function in_array (needle, argStrict) {
	haystack=['calz63', 'calz64', 'calz65', 'calz66', 'calz67', 'calac63', 'calac64', 'calac65', 'calac66', 'calac67', 'calac68'];
    var key = '', strict = !!argStrict; 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
			}
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) { 
			return true;
            }
        }
    }
     return false;
}
function LoadProject(UserId){
   var xmlHttp;
   try 
    {   
	xmlHttp=new XMLHttpRequest();    
	}
    catch (e) 
    {   
	try 
	{      
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
	}
    catch (e)
    {      
	try
    {        
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
	}
    catch (e)
    {        
	alert("Your browser does not support AJAX!");        
	return false;       
	}      
	}    
	}
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
	ONUM=xmlHttp.responseText;
	MM_goToURL('parent','Project.php');
	return document.MM_returnValue
	}
	}
	var url="LoadProject.php?UserId="+UserId;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
}
function ReLoadDefaultLoads(UserId){
   var xmlHttp;
   try 
    {   
	xmlHttp=new XMLHttpRequest();    
	}
    catch (e) 
    {   
	try 
	{      
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
	}
    catch (e)
    {      
	try
    {        
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
	}
    catch (e)
    {        
	alert("Your browser does not support AJAX!");        
	return false;       
	}      
	}    
	}
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
	ONUM=xmlHttp.responseText;
	ReturnArray=ONUM.split("|");
	UpdateCells(ReturnArray);
	}
	}
	var url="LoadDefaultLoads.php?UserId="+UserId;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
}
function FilterUserProjects(obj){
   var xmlHttp;
   try 
    {   
	xmlHttp=new XMLHttpRequest();    
	}
    catch (e) 
    {   
	try 
	{      
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
	}
    catch (e)
    {      
	try
    {        
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
	}
    catch (e)
    {        
	alert("Your browser does not support AJAX!");        
	return false;       
	}      
	}    
	}
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
	ONUM=xmlHttp.responseText;
	var curloc="Projects.php"
    MM_goToURL('parent',curloc); 
	return document.MM_returnValue;
	}
	}
	if(obj.checked==1){
	FilterState="on";
	}else{
	FilterState="off";
	}
	var url="FilterProjects.php?FilterState="+FilterState;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
}
function ShowOnlyResult(obj){
	var DivArray=new Array('DensitySoilVoids','DensitySoilBetweenBlocks','VerticalBlockCaption','CoefficientFriction','SlopeDegrees','WallFrictionAngle','SoilDensity');
	if(obj.checked==1){
		CurrentKAETop=document.getElementById('KAEResults').style.top;
		CurrentResultsTop=document.getElementById('ProjectResults').style.top;
		CurrentAllowHeightTop=document.getElementById('AllowableWallHeight').style.top;
		CurrentConcreteSelectorTop=document.getElementById('ConcreteSelector').style.top;
		CurrentConcreteSelectorLeft=document.getElementById('ConcreteSelector').style.left;
		CurrentBottomBlockWidthTop=document.getElementById('BottomBlockWidth').style.top;
		CurrentBottomBlockWidthLeft=document.getElementById('BottomBlockWidth').style.left;
		CurrentBottomBlockDepthTop=document.getElementById('BottomBlockDepth').style.top;
		CurrentProjectTitleTop=document.getElementById('ProjectTitle').style.top;
		CurrentBottomBlockDepthLeft=document.getElementById('BottomBlockDepth').style.left;
		CurrentConcreteSelectorHeight=document.getElementById('ConcreteSelector').style.height;
		CurrentCornerCellShroudTop=document.getElementById('CornerCellShroud').style.top;
		
		document.getElementById('ProjectResults').style.top=-200+"px";
		document.getElementById('KAEResults').style.top=-200+"px";
		document.getElementById('AllowableWallHeight').style.top=-130+"px";
		document.getElementById('ConcreteSelector').style.top=80+"px";
		document.getElementById('ConcreteSelector').style.height=40+"px";
		document.getElementById('ConcreteSelector').style.left=-485+"px";
		document.getElementById('BottomBlockWidth').style.top=10+"px";
		document.getElementById('BottomBlockWidth').style.left=320+"px";
		document.getElementById('BottomBlockDepth').style.top=40+"px";
		document.getElementById('BottomBlockDepth').style.left=-130+"px";
		document.getElementById('ProjectTitle').style.top=-630+"px";
		document.getElementById('ProjectResults').style.top=-200+"px";
		document.getElementById('CornerCellShroud').style.top=-370+"px";
		
		DisplayMode="hidden";
		document.getElementById('HorizontalBlockCaption').className="display";
		document.getElementById('InterBlockContainer').style.backgroundImage="url(../images/null.gif)";
	}else{
		DisplayMode="visible";
		document.getElementById('InterBlockContainer').style.backgroundImage="url(images/ProjectImage.gif)";
		document.getElementById('ProjectResults').style.top=CurrentResultsTop;
		document.getElementById('KAEResults').style.top=CurrentKAETop;
		document.getElementById('AllowableWallHeight').style.top=CurrentAllowHeightTop;
		document.getElementById('ConcreteSelector').style.top=CurrentConcreteSelectorTop;
		document.getElementById('ConcreteSelector').style.left=CurrentConcreteSelectorLeft;
		document.getElementById('BottomBlockWidth').style.top=CurrentBottomBlockWidthTop;
		document.getElementById('BottomBlockWidth').style.left=CurrentBottomBlockWidthLeft
		document.getElementById('BottomBlockDepth').style.top=CurrentBottomBlockWidthTop;
		document.getElementById('BottomBlockDepth').style.left=CurrentBottomBlockWidthLeft;
		document.getElementById('ConcreteSelector').style.height=CurrentConcreteSelectorHeight;
		document.getElementById('ProjectTitle').style.top=CurrentProjectTitleTop;
		document.getElementById('CornerCellShroud').style.top=CurrentCornerCellShroudTop;
		document.getElementById('HorizontalBlockCaption').className="hidden";
	}
	for (var i=0;i<DivArray.length;i++) {
		TargetDiv=DivArray[i];
		document.getElementById(TargetDiv).style.visibility=DisplayMode;
	}
}
function ShowSpecs(ConId){
	var CurrentState=document.getElementById(ConId).className;
	if(CurrentState=="hidden"){
		document.getElementById(ConId).className="display";
	}else{
		document.getElementById(ConId).className="hidden";
	}
}
function ResetRegisterWindow(){
	 resizeTo(900,600)
}
function SetOpenerSpecs(){
	 w=getWidth();
	 h=getHeight();
     var left = 0;
     var top = 0;
	 moveTo(left, top)
	 //resizeTo(w,h);
}
function maximizeWindow() {
 if (parseInt(navigator.appVersion)>3) {
  if (navigator.appName=="Netscape") {
   if (top.screenX>0 || top.screenY>0) top.moveTo(0,0);
   if (top.outerWidth < screen.availWidth)
      top.outerWidth=screen.availWidth;
   if (top.outerHeight < screen.availHeight) 
      top.outerHeight=screen.availHeight;
  }
  else {
   top.moveTo(-4,-4);
   top.resizeTo(screen.availWidth+8,screen.availHeight+18);
  }
 }
}


function ResetOperationalWindow(){
	 w=900;
	 h=780;
	 resizeTo(w,h);
	 width=1259;
	 height=797;
     var left = Math.round(width/2-w/2);
     var top = Math.round(height/2-h/2);
	 moveTo(left, top)
}
function BeginProjectEdit(str,width,height){
	 w=1003;
	 h=850;
	 resizeTo(w,h);
     var left = Math.round(width/2-w/2);
     var top = Math.round(height/2-h/2);
	 moveTo(left, top)
	 document.location.href=str
}
function IndicateTerms(obj){
	if(obj.checked==1){
		document.getElementById('ContinueButton').disabled=false;
	}
}
function AcceptTerms(obj){
	if(obj.checked==1){
		document.getElementById('ContinueButton').disabled=false;
	}
}
function checkAccessRequestFields(){
	var requiredCount=0;
	var requiredFields=new Array('login','password','First_Name','Second_Name','Email','Company','Terms','Region');
	var requiredRow=new Array('loginRow','passwordRow','First_NameRow','Second_NameRow','EmailRow','CompanyRow','TermsRow','RegionRow');
	NumofRequired=requiredFields.length;
	for (var i=0;i<NumofRequired;i++) {
		var fieldName=document.getElementById(requiredFields[i])
		if((fieldName)&&(!fieldName.value)){
			document.getElementById(requiredRow[i]).style.backgroundColor="#FD7302";
			requiredCount++;
		}else{
			document.getElementById(requiredRow[i]).style.backgroundColor="#EBEDEE";
		}
	}
	if(requiredCount>0){
		alert('The Following Field(s) are Required');
	}else{
		tmt_formManager('former','submit()');
	}
}
function AutoLogin(){
	alert('Thanks for registering. A copy of your access details has been sent to the email address supplied.\nYou will now be automatically logged into the system.');
	tmt_formManager('former','submit()');
}
function ShowAnchorBloc(){
	navigate_external('http://www.humes.co.nz/Subdivision_view.php?id=1112049904&origin=subdivision');
}
function GlobalDelete(str){
	confirmation=confirm('As a primary administrator you may completely remove this project from the system.\nClick OK to proceed or Cancel to leave the project in the system but not viewable.');
	if(confirmation==true){
		MM_goToURL('parent',str);
		return document.MM_returnValue
	}
}
