userAgent = window.navigator.userAgent;
browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);

function setCookie(cookieName,cookieValue,nDays) {
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0) nDays=1;
  expire.setTime(today.getTime() + 3600000*24*nDays);
  document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
}

function newImage(arg) {
  if (document.images) {
    rslt = new Image();
    rslt.src = arg;
    return rslt;
  }
}

function findElement(n,ly) {
  if (browserVers < 4)		return document[n];
  var curDoc = ly ? ly.document : document;
  var elem = curDoc[n];
  if (!elem) {
    for (var i=0;i<curDoc.layers.length;i++) {
      elem = findElement(n,curDoc.layers[i]);
      if (elem) return elem;
    }
  }
  return elem;
}

function clearDefault(el) {
  el.value = "";
}

function loadEmail() {
  var u = 'https://services.myngp.com/NGPOnlineServices/EmailSignup.aspx?X=vymPqF7Xjbg=';
  var e = document.getElementById("email");
  if (!(e==null)){
  	var ev = e.value;
  	if (!(ev=="") && (echeck(ev))){
  	  u=u+"&e="+ev;
    }
  }
  var z = document.getElementById("zipcode");
  if (!(z==null)){
  	var zv = z.value;
  	if (!(zv=="") && (validateZIP(zv))){
      u=u+"&z="+zv;
    }
  }
  setCookie('splash_skip','TRUE',365);
  document.location.href=u;
}

function loadContrib() {
  var u = 'https://services.myngp.com/NGPOnlineServices/contribution.aspx?X=Ye7d1HUofGe8BLm6EqtmlBwOtFUFRbzU';
  var f = document.getElementById("amount");
  if (!(f==null)){
    var fg = f.value;
    if (!(isNaN(fg)) && !(fg=="")){
      u=u+"&a="+fg;
    }
  }
  setCookie('splash_skip','TRUE',30);
  document.location.href=u;  
}

function loadVolunteer() {
  var u = 'https://services.myngp.com/NGPOnlineServices/volunteer.aspx?X=Ye7d1HUofGfD+uhzMnzk/g==';
  setCookie('splash_skip','TRUE',120);
  document.location.href=u;  
}

function validateZIP(field) {
  var valid = "0123456789";
  var hyphencount = 0;
  if (field.length!=5) {
    return false;
  }
  for (var i=0; i < field.length; i++) {
    temp = "" + field.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") {
      return false;
    }
  }
  return true;
}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	  return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	  return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	  return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
	  return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	  return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
	  return false;
	}
	if (str.indexOf(" ")!=-1){
	  return false;
	}
 	return true;					
}
function IEHoverPseudo() {
  var ulNodes = getElementsByClass("nice-menu");

  var j = 0;
  var liNodes = null;

  for(var i = 0; i < ulNodes.length; i++) { 
    liNodes = ulNodes[i].getElementsByTagName("li");
    for(j = 0; j < liNodes.length; j++) {
      if(liNodes[j].className == "menuparent") {
        liNodes[j].onmouseover=function() { this.className += " active"; }
        liNodes[j].onmouseout=function() { this.className = "menuparent"; }
      } else if(liNodes[j].className == "menuparent first") {
        liNodes[j].onmouseover=function() { this.className += " active"; }
        liNodes[j].onmouseout=function() { this.className = "menuparent first"; }
      }
    }
  }
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(IEHoverPseudo);

// place in tinyMCE.init:
// setupcontent_callback : "plugin_regexrep_setup",
//
// tinyMCE regexp fix for Drupal

function plugin_regexrep_setup(editor_id, node) {
  node.innerHTML = plugin_regexrep_replace(node.innerHTML);
}

function plugin_regexrep_replace(content) {
  if (value.indexOf('\n')) {
    content = value += "\n\n"; // just to make things a little easier, pad the end
    content = value.replace('|<br />\s*<br />|', "\n\n");
    content = value.replace('!(<(?:table|ul|ol|li|pre|form|blockquote|h[1-6])[^>]*>)!', "\n$1"); // Space things out a little
    content = value.replace('!(</(?:table|ul|ol|li|pre|form|blockquote|h[1-6])>)!', "$1\n"); // Space things out a little
    content = value.replace("/\n\n+/", "\n\n"); // take care of duplicates
    content = value.replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n"); // make paragraphs, including one at the end
    content = value.replace('|<p>\s*?</p>|', ''); // under certain strange conditions it could create a P of entirely whitespace
    content = value.replace("|<p>(<li.+?)</p>|", "$1"); // problem with nested lists
    content = value.replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>");
    content = value.replace('</blockquote></p>', '</p></blockquote>');
    content = value.replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1");
    content = value.replace('!(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>!', "$1");
    content = value.replace('|(?<!<br />)\s*\n|', "<br />\n"); // make line breaks
    content = value.replace('!(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*<br />!', "$1");
    content = value.replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1');
    content = value.replace('/&([^#])(?![A-Za-z0-9]{1,8};)/', '&amp;$1');
  }
  return content;
}