function m_addLoadEvent(func) {
  var oldOnload = window.onload;
  if (typeof window.onload != "function") {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldOnload();
      func();
    }
  }
}

function m_externalLinks() {
  if (typeof document.getElementsByTagName == "undefined") {
    return;
  }
  var anchors = document.getElementsByTagName("a");
  var mesg = "(this link opens in a new window)";
  for (var i = 0; i < anchors.length; i++) {
    var anchor = anchors[i];
    var rel = "" + anchor.getAttribute("rel");
     if(anchor.href == "" || rel.indexOf("lightbox")>=0) {
	     continue;
    }
    if (
      typeof anchor.hostname != "undefined"
      && typeof anchor.protocol != "undefined"
      && anchor.protocol.indexOf("javascript") < 0
      && (
        (
          rel.length > 0
          && rel.indexOf("blank") >= 0
        )
        || (
          (
            rel.length == 0
            || rel.indexOf("self") < 0
          )
          && anchor.hostname != document.location.hostname
        )
      )
      && anchor != 'http://elysiants.luxehomeswap.com/'
    ) {
      anchor.target = "_blank";
           
      if (typeof anchor.title != "undefined") {
        anchor.title = (anchor.title ? anchor.title + " " : "") + mesg;
      }
      else {
        anchor.title = mesg;
      }
    }
  }
}

/**
 * Returns a new xml http request object
 */
function getXmlHttpRequest() {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    alert("Your browser does not support XMLHTTP!");
  }
  return xmlhttp;
}

function divPopup(nid) {
	if (nid == 'close') {
		document.getElementById('popup-overlay').style.display = 'none';
		document.getElementById('popup').style.display = 'none';
	}else {
		document.getElementById('popup-overlay').style.display = 'block';
		document.getElementById('popup').style.display = 'block';
		
		//Build Ajax request
		  var ajaxRequest = getXmlHttpRequest();
		  
		  ajaxRequest.onreadystatechange = function() {
		    if (ajaxRequest.readyState == 4) {
		      //Update status message
		      var popupDiv = document.getElementById('popup-content');
       		  popupDiv.innerHTML = ajaxRequest.responseText;
		    }
		  }
		  
		  if(window.location.hostname == 'localhost'){
		  	ajaxRequest.open("GET", "getNode-ajax/" + nid, true);
		  }else{
		  	ajaxRequest.open("GET", 'http://' + window.location.hostname + "/getNode-ajax/" + nid, true); 
		  }
		  ajaxRequest.send(null);			
	}
}

function el_thumbs_up(e) {
	jQuery.getJSON($(e).attr('href'), function(json){
		if (json.error) {
			alert(json.error_message);
		} else {
			$(e).parent().html(json.result);
		}
	});
}

m_addLoadEvent(m_externalLinks);

function CheckMinChars(e){
	if (e.val().length >= 3) return true;
	else {
		alert('Search criteria should be at least 3 characters.');
		return false;
	}
}
