function checkUnitSelected(unit)
{
    var unitDropDown = document.getElementById(unit);    
    if(unitDropDown.options[unitDropDown.selectedIndex].text == "All")
    {
        alert("To proceed to  add unit contacts you must select Owner Name/Unit from owner name dropdown!!!");
        //window.event.returnValue = false;
        return false;
    }
    
}
function ResetDdnWidth(object)
{
    if(object.originalWidth!="")    {
        object.style.position = "";
        object.style.width = object.originalWidth;
        object.originalWidth = "";
    }
}

function RearrangeDdnWidth(object)
{
    var objectPosition = fGetXY(object);
    if(object.originalWidth=="")    {
		object.style.position = "absolute";
		object.style.top = objectPosition[1];
		object.style.left = objectPosition[0];
		object.originalWidth = object.style.width;
		object.style.width = "";
		object.focus();
	}	
}

function fGetXY(aTag){
  var p=[0,0];
  while(aTag!=null){
  	p[0]+=aTag.offsetLeft;
  	p[1]+=aTag.offsetTop;
  	aTag=aTag.offsetParent;
  }
  return p;
}

function CheckForTerms(terms)
{    
    var chkTerms = eval('document.form1.' + terms);
    //alert(e.keyCode);
    if(!chkTerms.checked)
    {
        alert("Please accept terms and conditions to proceed.");
        //window.event.returnValue = false;
        return false;
        //window.event.returnValue = false;
    }
}

function CheckSelectedVendors(selectedgridname,selectiongridname,combo,sessionvalue)
{       
    /*if(sessionvalue >= 3)
    {
        alert('You can select only three from the search panel...');
        window.event.returnValue = false;     
    }*/    
    if(!combo)  
    {       
        SelectOnlyThree(selectiongridname);        
    }
    var n = document.getElementById(selectedgridname).rows.length;	    
    if(document.getElementById(selectiongridname) != null)
        var m = document.getElementById(selectiongridname).rows.length;      
    
    
    var cnt = 0;    
    var totcnt = 0;
   
    if(!combo)
    {
        for(i=2;i<=m;i++)
        {
            if(i<10)
            {
              if(document.getElementById(selectiongridname +"_ctl0"+i+"_HOACheckBox").checked==true)
		      {
		        cnt++;
		      }  		      
            }
            else
            {
                if(document.getElementById(selectiongridname+"_ctl"+i+"_HOACheckBox").checked==true)
			    {
			        cnt++;
			    }
			}
        }       
        totcnt = eval(cnt+(n-1));  
        //alert(totcnt + " " + n + "  " + cnt)  ;
        if(totcnt > 4)    
        {
            alert("Already " + eval(n-1) + " vendor(s) have been selected, you can select only four vendors for requesting bid!!!");            
            for(i=2;i<=m;i++)
            {            
                 if(document.getElementById(selectiongridname +"_ctl0"+i+"_HOACheckBox") != null)
                    document.getElementById(selectiongridname +"_ctl0"+i+"_HOACheckBox").checked = false;
                 if(document.getElementById(selectiongridname +"_ctl"+i+"_HOACheckBox") != null)
		            document.getElementById(selectiongridname +"_ctl"+i+"_HOACheckBox").checked = false;		        
            }            
            //return false;                
            window.event.returnValue = false;        
            
        }
    }
    else
    { 
       totcnt = eval(1+(n-1));         
        if(totcnt > 4)    
        {
            alert("Already " + eval(n-1) + " vendor(s) have been selected, you can select only four vendors for requesting bid!!!");
            //return false;            
            window.event.returnValue = false;        
            
        }
    }  
    
        
}
function VendorRequestedBid(requested,chk)
{
    if(requested == 'true')
    {
        alert("Selected vendor already requested a bid!!!");
        chk.checked = false;
        return false;
        window.event.returnValue = false;
    }
}

function SelectOnlyOne(gridname)
{
    //alert(gridname)
	var n = document.getElementById(gridname).rows.length;	
	var i; 
	var j=0; 
	for(i=2;i<=n;i++)
	{
		if(i<10)
		{	
			if(document.getElementById(gridname +"_ctl0"+i+"_HOACheckBox").checked==true)
			{	
				if(j>0)
				{	
					alert('You can select only one...');
					document.getElementById(gridname +"_ctl0"+i+"_HOACheckBox").checked=false;
				}
				else		
				{	
					j++;
				}
			}
		}
		else
		{
			if(document.getElementById(gridname+"_ctl0"+i+"_HOACheckBox").checked==true)
			{
				if(j>0)
				{
					alert('You can select only one...');
					document.getElementById(gridname+"_ctl0"+i+"_HOACheckBox").checked=false;
				}
				else
				{
					j++;
				}
			}
		}
	}
}

function SelectOnlyThree(gridname)
{   
	var n = document.getElementById(gridname).rows.length;
	var i; 
	var j=0; 
	for(i=2;i<n;i++)
	{
		if(i<10)
		{
			//alert(i) 
			if(document.getElementById(gridname +"_ctl0"+i+"_HOACheckBox").checked==true)
			{				    		    
				if(j>2)
				{	
					alert('You can select only three...');
					document.getElementById(gridname +"_ctl0"+i+"_HOACheckBox").checked=false;
				}
				else		
				{	
					j++;
				}
			}
		}
		else
		{ 
			if(document.getElementById(gridname+"_ctl"+i+"_HOACheckBox").checked==true)
			{
				if(j>2)
				{
					alert('You can select only three...');
					document.getElementById(gridname+"_ctl"+i+"_HOACheckBox").checked=false;
				}
				else
				{
					j++;
				}
			}
		}
	}
}

// JavaScript Document
function CheckforBlank()
{
//    alert("hi");
}

function checkSelection()
{
    var cnt = 0;
    flag = "false"
    for(i=0;i< document.Form1.elements.length;i++)
	{	
		if(document.Form1.elements(i).type=='checkbox' && document.Form1.elements(i).checked == true)
		{
		    cnt++		
		}
		if(cnt > 3)
		{
		    flag = "true"	    
		}
	}
	
	if(flag == "true")
	{
	    alert("Select upto 3 vendors!!!");
	    return false;
	}

}

function EnableContractorNo(dropdownlist,txtbox,validator)
{
    /*var dropdown = eval("document.all."+ dropdownlist);
    var textbox = eval("document.all."+ txtbox);
    var validate = eval("document.all."+ validator);*/
    var dropdown = document.getElementById(dropdownlist);
    var textbox = document.getElementById(txtbox);
    var validate = document.getElementById(validator);
    
    
    if(dropdown.options[dropdown.selectedIndex].text == "Yes")
    {
        textbox.disabled = false;
        validate.enabled = true;
        textbox.focus();
    }
    else
    {
        textbox.disabled = true;
        textbox.value = "";
        validate.enabled = false;
    }
}
function EnableLocation(dropdownlist,txtbox)
{
    var dropdown = document.getElementById(dropdownlist);
    var textbox = document.getElementById(txtbox);
    
    if(dropdown.options[dropdown.selectedIndex].text == "Common Area")
    {
        textbox.disabled = false;        
        textbox.focus();
    }
    else
    {
        textbox.disabled = true;
        textbox.value = "";        
    }

}
function changeBoxStatus() {

   var boxes = new Array(); 
   for (var i = 0; i <= 7; i++) 
   { 
      boxes[i] = document.getElementById("box"+i); 
      
      if (boxes[i].checked) 
	  { document.f.submitbutton.disabled = false; break; } 
	  else
	  { document.f.submitbutton.disabled = true;} 
   } 
   
}
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function showSelects()
{    
    //if(!document.all) 
    //    return; //not IE
    var oSelects=document.getElementsByTagName("select"); 
    for(var i=0;i<oSelects.length;i++)
        oSelects[i].style.visibility="visible";
}
/*function loopSelects()
{
    if(navigator.appName == "Microsoft Internet Explorer")
    {        
        var ua = window.navigator.userAgent
	    var msie = ua.indexOf ( "MSIE 6.0" )
	    if(msie > 0)
        {    
            for(var i = 0; i < document.forms[0].elements.length; i++)
            {
                if(document.forms[0].elements[i].type == "select-one")
                {
                    if(document.forms[0].elements[i].id.indexOf("Service") != -1)
                    {
                        document.forms[0].elements[i].style.visibility = "visible"
                    }
                    else
                    {
                        document.forms[0].elements[i].style.visibility = "hidden"
                    }
                }
                    //alert(document.forms[0].elements[i].type)
            }
        }
    }
}*/

function hideSelects()
{    
    //if(!document.all) 
    //    return; //not IE    
    if(navigator.appName == "Microsoft Internet Explorer")
    {        
        var ua = window.navigator.userAgent
	    var msie = ua.indexOf ( "MSIE 6.0" )
	    if(msie > 0)
        {        
            var oSelects=document.getElementsByTagName("select"); 
            //alert(oSelects)
            for(var i=0;i<oSelects.length;i++)
            {        //alert(oSelects[i].id);
                if(oSelects[i].id == 'year')        
                    oSelects[i].style.visibility="visible";        
                else if(oSelects[i].id == 'month')
                    oSelects[i].style.visibility="visible"; 
                else if(oSelects[i].id == 'Select1')
                    oSelects[i].style.visibility="visible"; 
                else if(oSelects[i].id == 'Select2')
                    oSelects[i].style.visibility="visible"; 
                else if(oSelects[i].id.indexOf('Property') != -1)       
                    oSelects[i].style.visibility="visible";                 
                else
                {
                    //alert("hidden");
                    oSelects[i].style.visibility="hidden";        
                }
            }
        }
     }
}

/*function hideSelects()
{
     if(!document.all )
     {
      selectControls[0] = null;
      return; //not IE
     }
     var b_version=navigator.appVersion  // start testing for IE version 7 or higher
     var verels = b_version.split(';');
     for( var i = 0; i < verels.length; i++ )
      if( verels[i].indexOf("MSIE") != -1 )
       break;
     if( i != verels.length )
     {
      verels = verels[i].split(" ");
      if( parseFloat(verels[2]) >= 7.0 )  // select problem with redering over top fix in IE 7 so return
       return;
     }
     var oSelects=document.getElementsByTagName("select");
     var count = 0;
     b1t = parseInt(Cal.elem.style.top);
     b1h = parseInt(Cal.elem.offsetHeight);
     b1l = parseInt(Cal.elem.style.left)
     b1w = parseInt(Cal.elem.offsetWidth);
     
     for(var i=0; i < oSelects.length; i++ )
     {
      b2t = parseInt(fDomOffset(oSelects[i], 'offsetTop'));
      b2h = parseInt(oSelects[i].offsetHeight);
      b2l = parseInt(fDomOffset(oSelects[i], 'offsetLeft'));
      b2w = parseInt(oSelects[i].offsetWidth);
      if( b1t <= b2t && (b1t + b1h) >= b2t && b1l <= (b2l + b2w) && (b1l + b1w) >= b2l ) 
      { 
       oSelects[i].style.visibility="hidden";
       selectControls[count++] = oSelects[i]; 
      }
      else
       selectControls[count] = null;
     }
}*/

fDomOffset = function( oObj, sProp )
{ 
 var iVal = 0; 
 while (oObj && oObj.tagName != 'BODY') 
 { 
  eval('iVal += oObj.' + sProp + ';');
  oObj = oObj.offsetParent; 
 } 
 return iVal; 
} 


function validateCheckbox(FormName, CheckAll, ObjectName){
		var bStatus = eval('window.document.' + FormName + '.' + CheckAll + '.checked');
		if(bStatus){
			for(index=0; index<4; index++){
				eval('window.document.' + FormName + '.' + ObjectName + "[" + index  + '].checked=true');
			}
		}
		else{
			for(index=0; index<4; index++){
				eval('window.document.' + FormName + '.' + ObjectName + "[" + index  + '].checked=false');
			}
		}		
	}
	function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function hilite(obj)
{
obj.style.border='1px  solid ';	
obj.style.borderColor='#666666';	
obj.style.backgroundColor='#EDEFF0';
}
function delite(obj)
{
obj.style.border='1px solid ';	
obj.style.borderColor='#999999';	
obj.style.backgroundColor='#ffffff';
}



function openDir(form) { 

	var newIndex = form.fieldname.selectedIndex; 
	if ( newIndex == 0 ) { 
		 
	} else { 
		cururl = form.fieldname.options[ newIndex ].value; 
		document.getElementById("day").style.display="none";
		document.getElementById("month").style.display="none"; 
		document.getElementById("week").style.display="none";		
        document.getElementById(cururl).style.display="block";
	} 

} 


function openfilter(form) { 

	var newIndex1 = form.filtername.selectedIndex; 
	
	if ( newIndex1 == 0 ) { 
	    document.getElementById("Vendor").style.display="none";
		document.getElementById("Priority").style.display="none"; 
		document.getElementById("Warranty").style.display="none";	
	} else { 
		cururl1 = form.filtername.options[ newIndex1].value; 
		document.getElementById("Vendor").style.display="none";
		document.getElementById("Priority").style.display="none"; 
		document.getElementById("Warranty").style.display="none";		
        document.getElementById(cururl1).style.display="block";
	} 

} 

function openindexfilter( form ) { 

	var newIndex = form.fieldname1.selectedIndex; 

	if ( newIndex == 0 ) {  
		document.getElementById("activity").style.display="none"; 
		document.getElementById("status").style.display="none";
		document.getElementById("properties").style.display="none";
		} 
	else { 

		cururl = form.fieldname1.options[ newIndex ].value; 
		document.getElementById("activity").style.display="none"; 
		document.getElementById("status").style.display="none";		
		document.getElementById("properties").style.display="none";	
        document.getElementById(cururl).style.display="block";
	} 

} 



function ChangeCheckBoxState(id, checkState)
{
    var cb = document.getElementById(id);
    if (cb != null)
        cb.checked = checkState;
}

function ChangeAllCheckBoxStates(arrayIDs, checkState)
{
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter    
    if (arrayIDs != null)
    {
        for (var i = 0; i < arrayIDs.length; i++)
        ChangeCheckBoxState(arrayIDs[i], checkState);
    }
}

function chkChilds(chkbox,ids)
{      
    var clientIDs = ids.split(",");
    for(i=0;i<clientIDs.length;i++)
    {
        ChangeCheckBoxState(clientIDs[i],chkbox);
    }
}

function ChangeHeaderAsNeeded(arrayIDs)
{
    // Whenever a checkbox in the GridView is toggled, we need to
    // check the Header checkbox if ALL of the GridView checkboxes are
    // checked, and uncheck it otherwise
    if (arrayIDs != null)
    {
        // check to see if all other checkboxes are checked
        for (var i = 1; i < arrayIDs.length; i++)
        {
            var cb = document.getElementById(arrayIDs[i]);
            if (!cb.checked)
            {
                // Whoops, there is an unchecked checkbox, make sure
                // that the header checkbox is unchecked
                ChangeCheckBoxState(arrayIDs[0], false);
                return;
            }
        }
        
        // If we reach here, ALL GridView checkboxes are checked
        ChangeCheckBoxState(arrayIDs[0], true);
    }
}
function checkGridCheckBoxs()
{
    var flg = "false";
    if (CheckBoxIDs != null)
    {
        // check to see if all other checkboxes are checked
        for (var i = 1; i < CheckBoxIDs.length; i++)
        {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if (cb.checked)
            {
                flg = "true";
            }
        }
        if(flg == "false")
        {
            alert('Select at least one item to delete');
            window.event.returnValue = false;
            return false;
        }
    }
    else
        window.event.returnValue = false;

}

function reName(lstBox,txtBox)
{
        var myList = document.getElementById(lstBox);		
		var myTxtBox = document.getElementById(txtBox);				
		myTxtBox.value = myList.options[myList.selectedIndex].text;				
}

function EvImageOverChange(name, direction,imgName)
{
    switch(direction)
    {
        case 'in':
            name.src = "../resources/images/tabs/" + imgName;
            break;
        case 'out':
            name.src = "../resources/images/tabs/" + imgName;    
            break;       
    }
}

function multilineMaxLength(textbox)
{
    if (textbox.value.length >= 255)
    {        
        textbox.value = textbox.value.substring(0,254);    
    }
    //return true;
    
}

function multilineMaxLengthNotes(textbox)
{
    if (textbox.value.length >= 500)
    {        
        textbox.value = textbox.value.substring(0,499);    
    }
    //return true;
    
}

function doKeypress(control){
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength && value.length > maxLength-1){
          event.returnValue = false;
          maxLength = parseInt(maxLength);
     }
}
// Cancel default behavior

function doBeforePaste(control){
    maxLength = control.attributes["maxLength"].value;
     if(maxLength)
     {
          event.returnValue = false;
     }
}
// Cancel default behavior and create a new paste routine

function doPaste(control){
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength){
          event.returnValue = false;
          maxLength = parseInt(maxLength);
          var oTR = control.document.selection.createRange();
          var iInsertLength = maxLength - value.length + oTR.text.length;
          var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
          oTR.text = sData;
     }
}


function DeleteConfirmation()
{
//    check if atleast one check box is selected
//    if (checkGridCheckBoxs()==false)
//    {
//        //no checkbox selected
//        return false;
//    }

        var result= confirm("Are you sure you want to delete?");
        if (result == true)
        {
           return true;
        }
        else
        {
            return false;
        }  

}

function DeleteConfirmationForUnit()
{
//    check if atleast one check box is selected
//    if (checkGridCheckBoxs()==false)
//    {
//        //no checkbox selected
//        return false;
//    }

        var result= confirm("Are you sure you want to delete?\nDeleting this unit will result in the deletion of all activity records, all assets, and all photos.\nClick 'Ok' if you wish to continue with deletion");
        if (result == true)
        {
           return true;
        }
        else
        {
            return false;
        }  

}

function DeleteConfirmationForUnitContact()
{
//    check if atleast one check box is selected
//    if (checkGridCheckBoxs()==false)
//    {
//        //no checkbox selected
//        return false;
//    }

        var result= confirm("Are you sure you want to delete?");
        if (result == true)
        {
           return true;
        }
        else
        {
            return false;
        }  

}

//format numbers with comma 
function CommaFormatted(amount)
{
    var delimiter = ","; // replace comma if desired
	if (amount.toString().indexOf('.')==-1)
	{
	    amount = amount + '.00';
	}
	var a = amount.split('.',2);
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}
// end of function CommaFormatted()