var isIE  = (navigator.appVersion.toLowerCase().indexOf("msie") != -1) ? true : false;
var isMac = (navigator.appVersion.toLowerCase().indexOf("macintosh") != -1) ? true : false;
var isSafari  = (navigator.appVersion.toLowerCase().indexOf("safari") != -1) ? true : false;

var m_searchFocus = false;
var timeOuts = new Array();
//*******************************************************************************************
jQuery(function() {
	
	//document.getElementById("logoLink").onclick = function(){ document.location.href ="/" }
/*
	if(document.getElementById("menuTips")){
		document.getElementById("menuTips").onclick = function(){ jQuery.cookie('resourcesTab', 'tabTips') }
		document.getElementById("menuCaseStudies").onclick = function(){ jQuery.cookie('resourcesTab', 'tabCaseStudies') ;
	}
*/	
	jQuery("a.buyHolly").each(		
		function(i, elm){
			jQuery(elm).click(function(p_evt){
				document.location = "https://lumenvox.com/store/?pl=1728";
				p_evt.preventDefault();
			});
			jQuery(elm).css("cursor", "pointer");
		}
	)
	
	jQuery("a.buyAculab").each(		
		function(i, elm){
			jQuery(elm).click(function(p_evt){
				document.location = "https://lumenvox.com/store/?pl=1773";
				p_evt.preventDefault();
			});
			jQuery(elm).css("cursor", "pointer");	
		}
	)
	
	jQuery("a.buySupport").each(		
		function(i, elm){
			jQuery(elm).click(function(p_evt){
				document.location = "https://lumenvox.com/store/?pl=1774";
				p_evt.preventDefault();
			});
			jQuery(elm).css("cursor", "pointer");	
		}
	)
	
	jQuery("a.gotoCaseStudies").each(		
		function(i, elm){
			jQuery(elm).click(function(p_evt){
				p_evt.preventDefault();
				$.cookie('resourcesTab', 'tabCaseStudies');
				document.location = "/resources/"
			});
			jQuery(elm).css("cursor", "pointer");	
		}
	)
	
	jQuery("a.ml-support").each(
		function(i, elm){
			jQuery(elm).click(function(p_evt){
				p_evt.preventDefault();
				window.location.href='mailto:' + ml[0];
			});
			jQuery(elm).css("cursor", "pointer");	
		}
	)
});

function getQueryVariable(variable) 
{
	var query = document.referrer.substring(document.referrer.indexOf("?") + 1, document.referrer.length);
	var vars = query.split("&");
	
	for (var i=0;i<vars.length;i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable){ 
			return decodeURIComponent(pair[1]);
		} 
	} 
}

function URLDecode(url) //function decode URL
{

	var HEXCHARS = "0123456789ABCDEFabcdef"; 
	var encoded = url;
	var plaintext = "";
	var i = 0;

	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1  && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				//alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
}

return plaintext;
};

function hsF(p_inputBox){
	m_searchFocus = true;
	p_inputBox.className = 'headSearchFocus';
}
	
function hsB(p_inputBox){
	m_searchFocus = false;
	p_inputBox.className = 'headSearch';
}

function clearAllTimeouts(){
      for(key in timeOuts ){
            clearTimeout(timeOuts[Number(key)]);
      }
}

function hsE(e){
    var characterCode //literal character code will be stored in this variable

	if(!e) e=window.event;
	characterCode = e.keycode ? e.keycode : e.which;

    if(characterCode == 13 && m_searchFocus){ //if generated character code is equal to ascii 13 (if enter key)
        initSiteSearch();
        return false 
    }else{
        return true 
    }
}

function initSiteSearch(){
	if(m_searchFocus)
		document.location = "/search/index.aspx?q=" + document.getElementById("txtSiteSearch").value;
}

function isChildDiv(a, b){	
	// we climb through b parents till we find a or null
 	while(b && (a!=b) && (b!=null)){
		b = b.parentNode;
	}
	return a == b;
}

function getLastElementNode(node) {
    
    child =  node.lastChild;
    while (  child != null ) {
        if (child.nodeType == 1) return child    
        child = child.previousSibling;    
    }
    return null;    
}

function getCookie(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	
	if (document.cookie.match(re)){ //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	}
	return ""
}

function setCookie(name, value, days){
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}else{
		var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/"; //cookie value is domain wide (path=/)
}

function checkEmail(emailStr){
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) {
	   //alert("Email address seems incorrect (check @ and .'s)");
	   return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {
		   // user is not valid
		   //alert("The username doesn't seem to be valid.")
		   return false;
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		   // this is an IP address
	   for (var i=1;i<=4;i++) {
		  if (IPArray[i]>255) {
			 //alert("The email address Destination IP address is invalid!")
		 return false;
		  }
		   }
		return true;
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	   //alert("The email address domain name doesn't seem to be valid.")
		   return false;
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	
	if (domArr[domArr.length-1].length<2 ||
		   domArr[domArr.length-1].length>3) {
	   //alert("The email address must end in a three-letter domain, or two letter country.")
	   return false;
	}

	if (len<2) {
	   //alert("The email address is missing a hostname!")
	   return false;
	}
	return true;
}
