﻿function EnableRadioButtonCilcked(controls,rowindex,factor,maxRowVal,bigUserControlName)
{
    var found=false;
    var oldValue=document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value;
    if(controls!="")
    {
        var controlArr=controls.split(",");
        for(i=0;i<controlArr.length;i++)
        {
            document.getElementById(bigUserControlName+'_'+controlArr[i]).disabled =false;
            if((controlArr[i].indexOf('txtIntegerInput')>=0)||(controlArr[i].indexOf('txtFloatInput')>=0))
            {
                document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex).checked=true;
                if((document.getElementById(bigUserControlName+'_'+controlArr[i]).id.indexOf("txtIntegerInput")>=0)||(document.getElementById(bigUserControlName+'_'+controlArr[i]).id.indexOf("txtFloatInput")>=0))
                {
                    if(isInteger(document.getElementById(bigUserControlName+'_'+controlArr[i]).value))
                    {
                        
                        if(document.getElementById(bigUserControlName+'_'+controlArr[i]).value=="0")
                        {
                            document.getElementById(bigUserControlName+'_'+controlArr[i]).value="";
                            return;
                        }
                        document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value=Math.min(factor*document.getElementById(bigUserControlName+'_'+controlArr[i]).value,maxRowVal);
                    }
                    else
                    {
                        document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value="0";
                    }
                    
                    found=true;
                }
            }
        }
    }
    if(!found)
    {
        document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value=factor;
    }
    CalculateTabTotal(bigUserControlName);
}

function CalculateTabTotal(bigUserControlName)
{

    var obj=null;
    var mandatoryRows=document.getElementById(bigUserControlName+"_txtMandatoryRows").value;
    var mandatoryArr=mandatoryRows.split(",");
    var maxRowId=0;
    
    for(i=1;i<mandatoryArr.length;i++)
    {
        if(parseInt( mandatoryArr[i],10)>parseInt( maxRowId,10))
            maxRowId=parseInt(mandatoryArr[i],10); 
        obj=document.getElementById(bigUserControlName+"_txtTotal"+mandatoryArr[i]);
        if(obj)
        {
            if((obj.value=='0')||(obj.value==''))
            {
                if(document.getElementById(bigUserControlName+"_txtTabTotal"))
                {
                   document.getElementById(bigUserControlName+"_txtTabTotal").value='0';
                    SetSoilEngineeringTotal();
                    SetSpatialSets();
                    return;
                }
            }
        }
    }
    
    obj=document.getElementById(bigUserControlName+"_txtTabTotal");
    if(obj)
    {
        var i=0;
        var totalText=null;
        var val=0;
        while(true)
        {
            totalText=document.getElementById(bigUserControlName+"_txtTotal"+i)
            if(totalText)
            {
                if(totalText.value!='')
                    val=parseFloat(val)+parseFloat(totalText.value);
            }
            else if(i>maxRowId)
                break;
            i++;
        }
        //here we see if the bigusercontrol is in soil activity so that we round the value
        if(bigUserControlName.indexOf("SoilEngineeringSetsEval")>0)
            {
                val = Math.round(val *100)/100;
                obj.value=val;
            }
        else
        {
            val=Math.round(val*100)/100;
            obj.value=Math.min(val,mandatoryArr[0]);
        }
        
        
    }
  
    SetSoilEngineeringTotal();
    SetSpatialSets();
    
}

function OnBlurTextBox(controls,rowindex,factor,maxRowVal,bigUserControlName)
{
    if(document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex))
    {
        if(document.getElementById(bigUserControlName+"_txtString"+rowindex).value.replace(/(\s+$)|(^\s+)/g, '')!="")
        {
            document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex).checked=true;
            EnableRadioButtonCilcked(controls,rowindex,factor,maxRowVal,bigUserControlName);
        }
    }    
    if(document.getElementById(bigUserControlName+"_txtString"+rowindex).value.replace(/(\s+$)|(^\s+)/g, '')=="")
    {
        document.getElementById(bigUserControlName+"_txtString"+rowindex).value="";
    }
        
}
function OnChangeComboBox(rowIndex,parentControlName,factor)
{
    if(document.getElementById(parentControlName+"_cmb"+rowIndex).value!="")
        document.getElementById(parentControlName+'_'+"txtTotal"+rowIndex).value=parseFloat(document.getElementById(parentControlName+"_cmb"+rowIndex).value)*parseFloat(factor);
    else
        document.getElementById(parentControlName+'_'+"txtTotal"+rowIndex).value="0";
    CalculateTabTotal(parentControlName);
}

function OnBlurIntegerInput(controls,rowindex,factor,maxRowVal,bigUserControlName,MinValue)
{
   
    var found=false;
//    var oldValue=document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value;
    if(controls!="")
    {
        var controlArr=controls.split(",");
        for(i=0;i<controlArr.length;i++)
        {
            document.getElementById(bigUserControlName+'_'+controlArr[i]).disabled =false;
            if((controlArr[i].indexOf('txtIntegerInput')>=0)||(controlArr[i].indexOf('txtFloatInput')>=0))
            {
                if((document.getElementById(bigUserControlName+'_'+controlArr[i]).id.indexOf("txtIntegerInput")>=0)||(document.getElementById(bigUserControlName+'_'+controlArr[i]).id.indexOf("txtFloatInput")>=0))
                {   
                    var validNumber;
                    if((controlArr[i].indexOf('txtIntegerInput')>=0)&&isInteger(document.getElementById(bigUserControlName+'_'+controlArr[i]).value))
                        validNumber = true;
                    else if((controlArr[i].indexOf('txtFloatInput')>=0)&&!isNaN(parseFloat(document.getElementById(bigUserControlName+'_'+controlArr[i]).value)))
                             validNumber = true;
                    if( validNumber)
                    {
                       
                        if(document.getElementById(bigUserControlName+'_'+controlArr[i]).value=="0")
                        {
                            DisableRadioButtonCilcked(controls,rowindex,bigUserControlName);
                            if(document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex))
                                document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex).checked=true;
                            return;
                        }
                        else if(document.getElementById(bigUserControlName+'_'+controlArr[i]).value=="")
                        {
                            if(document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex))
                                document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex).checked=false;
                            if(document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex))
                                document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex).checked=false;
                            return;
                        }
                        if(document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex))
                        {
                            var currentValue = document.getElementById(bigUserControlName+'_'+controlArr[i]).value;
                            
                            if(parseInt(currentValue,10)>=parseInt(MinValue,10))
                            
                                document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value=Math.min(factor*currentValue,maxRowVal);
                             else
                             {
                             alert(".المساحة أقل من الحد الأدنى المطلوب (12). لن يتم احتساب أي نقاط عنها");
                              document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value = "0"
                              }
                         }
                        if(document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex))
                            document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex).checked=true;
                    }
                    else
                    {
                        if(document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex))
                                document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex).checked=false;
                        if(document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex))
                            document.getElementById(bigUserControlName+'_'+"radioEnable"+rowindex).checked=false;
                        if(document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex))
                            document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value="0";
                        document.getElementById(bigUserControlName+'_'+controlArr[i]).value="";
                    }
                    
                    found=true;
                }
            }
        }
    }
    if(!found)
    {
        document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value=factor;
    }
    if(document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex))
        CalculateTabTotal(bigUserControlName);
}
function DisableRadioButtonCilcked(controls,rowindex,bigUserControlName)
{
    if(document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex))
        document.getElementById(bigUserControlName+'_'+"txtTotal"+rowindex).value=0;
    if(document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex))
        document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex).checked=true;
    if(controls!="")
    {
        var controlArr=controls.split(",");
        for(i=0;i<controlArr.length;i++)
        {
            if((document.getElementById(bigUserControlName+'_'+controlArr[i]).id.indexOf("txtIntegerInput")>=0)||(document.getElementById(bigUserControlName+'_'+controlArr[i]).id.indexOf("txtFloatInput")>=0))
            {
                    document.getElementById(bigUserControlName+'_'+controlArr[i]).value ="0";
                    //if(document.getElementById(bigUserControlName+'_'+"radioDisable"+rowindex))
                        //document.getElementById(bigUserControlName+'_'+controlArr[i]).disabled=true;
            }
            else 
            {
                document.getElementById(bigUserControlName+'_'+controlArr[i]).disabled =true;
                ShowFileUploadControl(rowindex,bigUserControlName);
            }
        }
    }
    CalculateTabTotal(bigUserControlName);
}
function isInteger (s)
{
  var i;
  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);
     if (!isDigit(c)) return false;
  }
  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function setTextvalueWithRadioValue(rowIndex,value,bigUserControlName)
{
    document.getElementById(bigUserControlName+'_'+"txtTotal"+rowIndex).value=value;
    CalculateTabTotal(bigUserControlName);
}

function ShowFileUploadControl(rowIndex,bigUserControlName)
{
    document.getElementById(bigUserControlName+"_"+"fileUpload"+rowIndex).style.display="block";
    //document.getElementById(bigUserControlName+"_"+"lblAddAttachment"+rowIndex).style.display="block";
    if(document.getElementById(bigUserControlName+"_"+"ltrChange"+rowIndex))
    {
        document.getElementById(bigUserControlName+"_"+"ltrChange"+rowIndex).style.display="none";
        
    }
}
function SetSoilEngineeringTotal()
 {
var total =0;
for ( i = 0; i < document.forms[0].length; i++)
    {
        if (document.forms[0][i].id != null && document.forms[0][i].id != '' )
        {        
            if (document.forms[0][i].id.indexOf('SoilEngineeringSetsEval') > 0 )
            if (document.forms[0][i].id.indexOf('txtTabTotal') > 0 )
            {            
                var newVal;
                
                newVal = parseFloat(document.forms[0][i].value,10);
//                newVal = Math.round(newVal);
//                document.forms[0][i].value = newVal;
                
                if(!isNaN(newVal))
                {
              
                    if(newVal==0)
                    {
                        total = 0;
                        break;
                     }
                    else
                        total+=newVal;
                }
            }
            
            
        }
    }
    
    var controlId="ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SoilEngineeringSetsEval_ctl03_txtTotalFooter";
     
    var textTotal =  document.getElementById(controlId);
    if(textTotal)
    {
         
        textTotal.value = total;
    }

}

function SetSpatialSets()
{

if(!document.getElementById("ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SpatialEngineerSetEval_Bigusercontrol1_txtTotal0"))
    return;
    

var total = 0;

var totalStation = document.getElementById("ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SpatialEngineerSetEval_Bigusercontrol1_txtTotal0");
var weight = document.getElementById("ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SpatialEngineerSetEval_Bigusercontrol1_txtTotal1");
var tudolet = document.getElementById("ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SpatialEngineerSetEval_Bigusercontrol1_txtTotal2");
var gps = document.getElementById("ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SpatialEngineerSetEval_Bigusercontrol1_txtTotal3");


var totalTxt = document.getElementById("ctl00_c_SelfAssessmentMainTab_ActivityAssessment_SpatialEngineerSetEval_Bigusercontrol1_txtTabTotal");
 
if(totalStation.value =='' || totalStation.value=='0')
    {
     if( (weight.value=='0' || weight .value=='') || (tudolet.value=='0' || tudolet .value==''))
        {
            totalTxt .value = '0';
            return;
        }
        else
        {
            totalTxt .value = parseFloat(weight .value)+parseFloat(tudolet .value);
        }
    }
    
 if( (weight.value=='0' || weight .value=='') || (tudolet.value=='0' || tudolet .value==''))
 {
    if(totalStation.value =='' || totalStation.value=='0')
    {
        totalTxt .value = '0';
        return;
    }
    else
    {
        totalTxt .value = parseFloat(totalTxt .value);
    }
 }
 
 if(gps.value=='0' || gps.value=='')
 {
   totalTxt .value = '0';
    return;
 }
}

function SetConsultantExpYears(totalObj,EngineeringObj,ConsultantObj,ConsultantTotalObj)
{
    
    if((isInteger(document.getElementById(totalObj).value))&&(isInteger(document.getElementById(EngineeringObj).value)))
    {
        if(parseInt(document.getElementById(totalObj).value,10)<parseInt(document.getElementById(EngineeringObj).value,10))
        {
            alert(" عدد سنوات العمل الكلية للمكتب يجب أن تكون أكبر من عدد سنوات عمل المكتب كمكتب هندسي ");
            document.getElementById(EngineeringObj).value =0;
            document.getElementById("ctl00_c_SelfAssessment_OSBigusercontrol7_txtTotal1").value =0;
            return;
        }
        document.getElementById(ConsultantObj).value=parseInt(document.getElementById(totalObj).value,10)-parseInt(document.getElementById(EngineeringObj).value,10);
    }
    else if(isInteger(document.getElementById(totalObj).value))
        document.getElementById(ConsultantObj).value=parseInt(document.getElementById(totalObj).value,10);
    else
        document.getElementById(ConsultantObj).value='0';
    
    document.getElementById(ConsultantTotalObj).value=document.getElementById(ConsultantObj).value;
    //CalculateTabTotal(totalObj.replace("_txtIntegerInput10",""));
    var mandatoryRows=document.getElementById(totalObj.replace("_txtIntegerInput10","")+"_txtMandatoryRows").value;
    var mandatoryArr=mandatoryRows.split(",");
    document.getElementById(totalObj.replace("_txtIntegerInput10","")+"_txtTabTotal").value=
        Math.min( parseInt( document.getElementById(totalObj.replace("_txtIntegerInput10","")+"_txtTotal1").value,10)+
            parseInt(document.getElementById(totalObj.replace("_txtIntegerInput10","")+"_txtTotal2").value,10),mandatoryArr[0]);
}

 function ShowHideButtons(chk,all)
 {
 var selected = 0;
 var total=0;
    for ( i = 0; i < document.forms[0].length; i++)
    {
        if (document.forms[0][i].id != null && document.forms[0][i].id != '' )
        {        
            if (document.forms[0][i].id.indexOf(chk) > 0 )
            {   
                total++;
                if (document.forms[0][i].checked)
                {
                    selected ++;
                }
            }
        }
    }
    //var checkBox = document.getElementById(all);
    var checkBox;
    for ( j = 0; j < document.forms[0].length; j++)
    {
        if (document.forms[0][j].id != null && document.forms[0][j].id != '' )
        {        
            if (document.forms[0][j].id.indexOf(all) > 0 )
            {   
                checkBox = document.forms[0][j];
            }
        }
    }
    
    if(selected==total)
    {
     
     if(checkBox)
        checkBox.checked = true;
               
    }else
    {
     if(checkBox)
        checkBox.checked = false;
    }
    
    
    
 }
 
 
 function OpenNewWindow(url,windowName)
{
window.open(url, windowName, 'width=790, height=570, scrollbars=yes');
}

function SelectAllCheckBoxes(SingleCheckId, CheckAllControlId) 
{ 
    //var checkBox = document.getElementById(CheckAllControlId);
    
    var checkBox;
    for ( j = 0; j < document.forms[0].length; j++)
    {
        if (document.forms[0][j].id != null && document.forms[0][j].id != '' )
        {        
            if (document.forms[0][j].id.indexOf(CheckAllControlId) > 0 )
            {   
                checkBox = document.forms[0][j];
            }
        }
    }

    for ( i = 0; i < document.forms[0].length; i++)
    {
        if (document.forms[0][i].id != null && document.forms[0][i].id != '' )
        {        
            if (document.forms[0][i].id.indexOf(SingleCheckId) > 0 )
            {   
                document.forms[0][i].checked = checkBox.checked;                        
            }
        }
    }
 }
 
function EnsureSelectedCheckBoxes(confirmationMessage, nonSelectedConfirmationMessage, SingleCheckId )
{
    var selected = 0;
    
    // ugly function for debugging..   
    for ( i = 0; i < document.forms[0].length; i++)
    {
        if (document.forms[0][i].id != null && document.forms[0][i].id != '' )
        {        
            if (document.forms[0][i].id.indexOf(SingleCheckId) > 0 )
            {            
                if (document.forms[0][i].checked)
                {
                    selected  ++;
                }
            }
        }
    }
    
    if (selected > 0)
    {
    
        if(confirmationMessage == '')
        {
            
            return true;
        }
        else
        {
            return confirm(confirmationMessage);
        }
    }
    else
    {
        alert(nonSelectedConfirmationMessage);
        return false;
    }
}

