function getDestinationName(prefix) {
   var host = location.host;
   var portalName = prefix + "tehamacorporate.com";

   if (host.match("^localhost")=="localhost") //startsWith
      portalName = prefix=="portal."?("localhost" + (location.port==90?":80":":81")):location.host;
   else if (host.match("tehamaonline.com$")=="tehamaonline.com") //endsWith
      portalName = prefix + "tehamaonline.com";
   else if (host.match("haveanimpact.com$")=="haveanimpact.com") //endsWith
      portalName = prefix + "haveanimpact.com";

   return portalName;
}
function getPortalName() {
   return getDestinationName("portal.");
}
function getDynamicName() {
   return getDestinationName("login.");
}
function getStaticName() {
   return getDestinationName("www.");
}

function setFormFocus() {
   if (document.loginForm) {
      document.loginForm.j_username.focus();
      document.loginForm.j_username.select();
   }
}
function check(extrapath) {
   var f = document.loginForm;
   if (f.j_username.value == "") {
      alert("Please enter user name!");
      setFormFocus();
      return false;
   }
   if (f.j_savedata.checked) {
      var exp = new Date(); 
      var expDays = 30;
      exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
      setCookie("implogin", f.j_username.value, exp);
      setCookie("imppassword", f.j_password.value, exp);
   } else {
      setCookie("implogin", "");
      setCookie("imppassword", "");
   }
   var portalName = getPortalName();
   f.action = "http://" + portalName + (extrapath==null?"":extrapath);

   return true;
}
function setCookie(name, value, expires, path, domain, secure) {
   var curCookie = name + "=" + escape(value) +
     ((expires) ? "; expires=" + expires.toGMTString() : "") +
     ((path) ? "; path=" + path : "") +
     ((domain) ? "; domain=" + domain : "") +
     ((secure) ? "; secure" : "");
   document.cookie = curCookie;
}
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
} else
     begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
    end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
function init() {
   var implogin = getCookie("implogin");
   implogin = implogin==null?"":implogin;
   var imppassword = getCookie("imppassword");
   imppassword = imppassword==null?"":imppassword;
   if (document.loginForm) {
      var f = document.loginForm;
      f.j_username.value = implogin;
      f.j_password.value = imppassword;
      if (implogin!='' || imppassword!='')
         f.j_savedata.checked = true;
   }
   setFormFocus();
}
function search() {
   var f = document.searchForm;
   f.action = "http://" + getDynamicName() + "/search.html";
   f.submit();
}
function register() {
   document.location.href = "http://" + getDynamicName() + "/registration-form.html";
}
function needAssistance() {
   document.location.href = "http://" + getDynamicName() + "/assistance-form.html";
}
function requestDistributors() {
   document.location.href = "http://" + getDynamicName() + "/find-distributors-form.html";
}


function backToHome() {
   document.location.href = "http://" + getStaticName() + "/";
}