//Buildium.js
// Create an object to serve as the namespace for buildium javascript code.
var buildium = buildium || {};

function ToggleEnterKey(id, ev) 
{    
 if (id == null)
    document.getElementById('_ctl8_btnSearch').click();
    
 var evt = ev ? ev : window.event;
      
 if (evt.keyCode == 13){
    evt.cancelBubble = true;
    evt.returnValue = false;
    __doPostBack(id,'');
        
    return false;
 } 
}

function ValidateForHTML(txtbox){
    
    if (txtbox.value.indexOf('<',0) >= 0)
    {
        
        txtbox.value = txtbox.value.replace(/\</g,'');
    }
}

function getElementsByNameMatch(node,searchNameMatch,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchNameMatch+"\\b");
	
	for (i = 0, j = 0; i < elsLen; i++) {
				
		if ( pattern.test(els[i].id) ) {
	
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function toggleAllModules(srcElement, srcArrow){
	 
	 var src, className;
	 var elems = getElementsByNameMatch(document,'^' + srcElement + '\\d+','tr');
	 
	 var src = document.getElementById(srcArrow + '0').src;
	 
	 if (src.substr(src.lastIndexOf("_")) == '_collapse.gif'){
		src = '../images/ico_action_expand.gif';
		className = 'gridBodyHidden';
	 } else{
		src = '../images/ico_action_collapse.gif'; 
		className = 'gridBodyChild';		
	 }	 
		
	 document.getElementById(srcArrow + '0').src = src;
		 
	 for (var i=1;i <= elems.length;i++) 
	 {
		document.getElementById(srcElement + i).className = className; 
		document.getElementById(srcArrow + i).src = src; 
	 }
}

function toggleModule(srcElement, srcArrow) 
{
	if((document.getElementById(srcElement).className=='gridBodyHidden')) 
	{ 
		document.getElementById(srcElement).className = 'gridBodyChild'; 
		document.getElementById(srcArrow).src = '../Images/ico_action_collapse.gif'; 
	}else { 
		document.getElementById(srcElement).className='gridBodyHidden'; 
		document.getElementById(srcArrow).src = '../Images/ico_action_expand.gif';
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked)
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}


String.prototype.trim = function() {
	var str = this.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
};

buildium.email = buildium.email || {};
buildium.email.ajaxEmailValidation = function(clientId, appPath)
{

  var email = $('#'+clientId).val();  
  if($('#img'+clientId) != null)
    $('#img'+clientId).remove();
         
  $.ajax({ 
    type: "POST", 
    url: appPath + '/PublicPages/AJAX/GetEmailValidationStatus.ashx', 
    data: "email="+ email, 
    success: function(data)
    { 
      if(data == "Invalid")
      { 
           if($('#img'+clientId).length == 0)
           {
             var img = $(document.createElement("img")).attr({ src: appPath + '/images/ico_email_invalid.gif', alt: 'invalid email ', id:'img' + clientId });
              $('#'+clientId).after(img);
              $('#img'+clientId).addClass("InvalidEmailImg");
           }
       }     
     } 
}); 
                    
}

//----------------------------------- report control scripts start------------------------------------------------
buildium.reportControlScripts = buildium.reportControlScripts || {};

buildium.reportControlScripts.hideOrShowParentRow = function(elemToFindText, textToSearch, elemToDisable)
{

  var txtVal = $('select[id$=_reports_' + elemToFindText + ']').val();
  if(txtVal != textToSearch) {
    buildium.reportControlScripts.hideParentRow(elemToDisable);
   }
   else {
    buildium.reportControlScripts.displayParentRow(elemToDisable);
   }
}

// function hide parent row of given element in report
buildium.reportControlScripts.hideParentRow = function(elem)
{
    $('input[id$=_reports_' + elem + ']').parent().parent().hide();
}

// funciton display parent row of given element in report
buildium.reportControlScripts.displayParentRow = function(elem)
{
    $('input[id$=_reports_' + elem + ']').parent().parent().show();
}

//----------------------------------- report control scripts end ------------------------------------------------
