
var hostName = document.location.host;

function selectImg( obj, i ){
obj.src=document.preloadArray[i].src;
return true;
}

function openWindow(url)  {
    popupWin = window.open(url, 'popupWin', 'scrollbars,resizable,dependent,width=619,height=500,left=yes,top=yes');
}

function setfocus() {
   var userNameFromCookie = getUserNameFromCookie();
   if( userNameFromCookie != null && userNameFromCookie.length > 0 && document.Login.username.value.length == 0 ) {
      document.Login.username.value = userNameFromCookie;
      document.Login.rem_un.checked = true;
      document.Login.password.focus();
   } else if (document.Login.username.value.length > 0){
      document.Login.password.focus();
   } else {
      document.Login.username.focus();
   }
}


function submitLoginForm(form) {

		var browser = navigator.appName;
		document.Login.browser.value = browser;
		var ver = navigator.appVersion;
		ver = ver.slice(0,4);
		document.Login.version.value = ver;

		if(sniffBrowser()) {
        if( form.rem_un.checked == true ) {
          var today = new Date();
          var expires = new Date();
          expires.setTime(today.getTime() + 1000*60*60*24*365*100);
          document.cookie = "username=" + escape(form.username.value) + "; path=/; expires=" + expires.toGMTString();
        } else {
          document.cookie = "username=";
        }
			form.action='https:\/\/' + hostName +'\/ClientHomePage/home.jsp';
			form.submit();
         return false;
		}
		else {
			alert("Your Browser is not supported. CreditEdge Plus requires\nInternet Explorer 5.0 or higher, or Netscape 7.0 or higher\n(both with SSL enabled).");
			return false;
		}
	}

function getUserNameFromCookie() {
  var search = "username=";
  if (document.cookie != null && document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(";", offset)
      if (end == -1) {
        end = document.cookie.length;
      }
      return unescape(document.cookie.substring(offset, end));
    }
  }
}

function clearForm(formName) {
	var theform = document.forms[formName];
	theform.First_Name.value = "";
	theform.Last_Name.value = "";
	theform.Company.value = "";
	theform.Title.value = "";
	theform.Address1.value = "";
	theform.Address2.value = "";
	theform.City.value = "";
	theform.State.value = "";
	theform.Zip_Code.value = "";
	theform.Country.value = "";
	theform.Phone_Number.value = "";
	theform.FAX_Number.value = "";
	theform.email.value = "";
	theform.Comments.value = "";
	theform.Company_URL.value = "http://";
	// document.contactus.reset();
	// return false;
}

function validateForm(theform) {

	if(theform.First_Name.value == "") {
		alert("First Name required.");
		theform.First_Name.focus();
		return false;
	}

	if(theform.Last_Name.value == "") {
		alert("Last Name required.");
		theform.Last_Name.focus();
		return false;
	}

	if(theform.Company.value == "") {
		alert("Company required");
		theform.Company.focus();
		return false;
	}

	if(theform.City.value == "") {
		alert("City required.");
		theform.City.focus();
		return false;
	}

	if(theform.Country.value == "") {
		alert("Country required.");
		theform.Country.focus();
		return false;
	}

	if(theform.Phone_Number.value == "") {
		alert("Phone Number Required");
		theform.Phone_Number.focus();
		return false;
	}

	var numericOnly = new RegExp(/^\d+$/);
	if(!numericOnly.test(theform.Phone_Number.value)) {
		alert("Phone Number can only accept numeric digits (Ex.5552323).");
		theform.Phone_Number.value = "";
		theform.Phone_Number.focus();
		return false;
	}


	if(theform.FAX_Number.value!="") {
		if(!numericOnly.test(theform.FAX_Number.value)) {
			alert("Fax Numbers can only accept numeric digits (Ex.5552323).");
			theform.FAX_Number.value = "";
			theform.FAX_Number.focus();
			return false;
		}
	}

	var email = theform.email;
	return validateEmail(email);

}

var chkDot = true;
var usEmail = true;

function validateEmail(theEmail) {
    var email = theEmail.value;
     var lenSuffix = (usEmail) ? 4 : 3;
      var ndxAt = ndxDot =  0;
      var ndxDot2 = 0
      ndxAt  = email.indexOf("@");
      ndxDot = email.indexOf(".") ;
      ndxDot2 = email.lastIndexOf(".") ;

      if ((ndxDot < 0) || (ndxAt < 0)) {
         alert("ERROR:Your email address lacks '@' or '.'.\n\n The format is 'you@domain.sfx'");
		 theEmail.focus();
		 return false;
      }
	  //else if (chkDot && (ndxDot < ndxAt) ) {
      //   chkDot = !(
       //     confirm("ERROR:You entered a 'dot' before the '@'\n Are you sure that is right?") );
		//	return false;
	 // }
      else if ( (ndxDot2 - 1) <= ndxAt) {
         alert("You may be missing your domain name.\n\nThe format is 'you@domain.sfx'");
		 theEmail.focus();
		 return false;
      }

	  else if (email.length < ndxDot2 + (lenSuffix -1)) {
	    alert("ERROR:Your email suffix has less than 2 characters.\n\n The format is 'you@domain.sfx'");
		theEmail.focus();
        //usEmail = !(confirm("You have fewer than 3 characters as a domain suffix.\nClick 'OK' if that is correct.")) ;
		return false;
      }

 }

function validateFriendForm(theform) {
	var email = theform.friendEmail;
	if(theform.friendName.value == "") {
		alert("ERROR: please specify your name in the input form. Thank you.");
		theform.friendName.focus();
		return false;
	}

	return validateEmail(email);
}
