var htmlNode = document.createElement("div");
// Allow only numbers to be input
function allowNumbersOnly(e) {                              
  var code;                                                 
  if (window.event)                                         
    code = window.event.keyCode;                            
  else if (e)                                               
    code = e.which;                                         
  else                                                      
    return true;                                            
                                                            
  // Set they key
  var keyChar = String.fromCharCode(code);                  
  // Control keys
    if ((code == null) || (code == 0) || (code == 8) ||     
        (code == 9) || (code == 13) || (code == 27) )       
       return true;                                         
                                                            
    return !isNaN(Number(keyChar));                         
}                                                         
  
/**
* This function will load an XML document
*/
function loadXMLDoc(xmlFile) {  
    // Internet Explorer  
    try {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    } catch(e) {
        // Firefox, Mozilla, Opera, etc.
        try {
            xmlDoc = document.implementation.createDocument("","",null);
        } catch(e) {
            alert(e.message);
            return;
        }
    }
    try {
        xmlDoc.async = false;
        xmlDoc.load(xmlFile);
        return xmlDoc;
    } catch (err) {
        alert(err.message);
    }
    return null;
}

/*
 * Return the value of a query parameter in the address bar
 */
function getQueryParameter(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pairs = vars[i].split("=");
        if (pairs[0] == variable) {
            return pairs[1];
        }
    }
    return null;
}

// Acquire AJAX
function getXMLHttpObject() {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser does not support AJAX!");
        return null;
      }
    }
  }
  return xmlHttp;
}

/**
* Send AJAX request
*/
function requestData(xmlHttp, url, data, method, callback) {
	if (xmlHttp) {
		if (method.toUpperCase() == "GET") {
			if (data) {
				xmlHttp.open("GET", url + "?" + data, true);
			} else {
				xmlHttp.open("GET", url, true);
			}
			xmlHttp.onreadystatechange = callback;
			xmlHttp.send("");
		} else {
			xmlHttp.open("POST", url, true);
			xmlHttp.onreadystatechange = callback;
			xmlHttp.send(data);
		}
	}
}

/**
* This function will refresh the parent window
*/
function refreshParent() {                 
  try {                                    
    window.opener.location.reload(true);  
    return true; 
  } catch(e) {                             
  }    
  return false;                                    
}                                          

/**
* This function will show a layer
*/
function showLayer(layerID) {
	var layer = document.getElementById(layerID);
	if (!layer) {
		return true;
	}
	layer.style.zIndex = 2;
	layer.style.visibility = "visible";
	return false;
}

/**
* This function will hide a layer
*/
function hideLayer(layerID) {
	var layer = document.getElementById(layerID);
	if (!layer) {
		return true;
	}
	layer.style.zIndex = 0;
	layer.style.visibility = "hidden";
	return false;
}

/*
* Unescape HTML
*/
function unescapeHTML(html) {
    htmlNode.innerHTML = html;
    if(htmlNode.innerText)
        return htmlNode.innerText; // IE

    return htmlNode.textContent; // FF
}

function newEventListener(id, eventType, callback) {
	var element = $(id);
	if (element) {
		if (element.addEventListener) {
			element.addEventListener(eventType, callback, false);
		} else if (element.attachEvent) {
			element.attachEvent("on" + eventType, callback);
		}
	}
}	


/**
* Limit the input characters
*/
function limitInput(textElem, counterElem, maxLen) {
	var success = false;
	$(document).ready(function() {
		// Make sure the elements exist
		if ($(textElem).length == 0) {
			return;
		}
		if ($(counterElem).length == 0) {
			return;
		}
			
		success = true;			
		var text = $(textElem).val();
		if (text.length > maxLen) {
			text = text.substring(0, maxLen);
			$(textElem).val(text);
			success = false;
		}
		$(counterElem).text(text.length + " of " + maxLen);
	});
	return success;
}
/*
* Clear the contents of a drop down element
*/
function clearDropDown(dropDownElem) {
	for (var i = dropDownElem.options.length-1; i >=0; i--) {
		dropDownElem.remove(i);
	}
}

/*
* Show processing box
*/
function showProcessing() {
	scroll(0,0);
	$(document).ready(function() {		
		if ($("#procBox").length == 0) {
			var div = $(document.createElement("div"));
			div.css("width", $(document).width()+'px');
			div.css("height", $(document).height()+'px');
			$(window).resize(function() {
				div.css("width", $(document).width()+'px');
				div.css("height", $(document).height()+'px');
			});
			div.attr("id", "procBox");
			var divInner = $(document.createElement("div"));
			divInner.attr("id", "innerBox");
			divInner.text("Processing");
			div.append(divInner);
			$("body").append(div);
		} else {
			$("#procBox").show();
		}
	});
}

/*
* Hid processing box
*/
function hideProcessing() {
	$(document).ready(function() {
		$("#procBox").hide();
	});
}

/*
* Show loading image for AJAX
*/
function showAJAXLoading() {
	$(document).ready(function() {		
		if ($("#loadingBox").length == 0) {
			var div = $(document.createElement("div"));
			div.css("width", $(document).width()+'px');
			div.css("height", $(document).height()+'px');
			$(window).resize(function() {
				div.css("width", $(document).width()+'px');
				div.css("height", $(document).height()+'px');
			});
			div.attr("id", "loadingBox");
			$("body").append(div);
		} else {
			$("#loadingBox").show();
		}
	});
}

/*
* Hide loading image for AJAX
*/
function hideAJAXLoading() {
	$(document).ready(function() {
		$("#loadingBox").hide();
	});
} 

/**
 * Opens a window with
 * @param url
 * @param name
 * @return
 */
function openWindow(url, name, options) {                                
  var w = $(window).width();                           
  w = w - (w * .1);                                    
  var h = $(window).height();                          
  h = h - (h * .05);                                   
  h = h - (h * .05); 
  var x = (($(window).width() - w) / 2);
  var y = (($(window).height() - h) / 2);
  var props = "height=" + h + ",width=" + w + ",top=" + x + ",left=" + y + ",scrollbars=yes,resizable=yes";
  if (options)
    props = options;
  win = window.open(url, name, props);       
}     

/**
 * Validate the email address
 * @param address
 * @return
 */
function isValidEmailAddress(address) {    
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/);
    return pattern.test(address);
}
