var badSIDchars = new String(" \"/\\[]:;|=,+*?<>()"); // same as "nogood" array in CGI code.
var bademchars = new String(" \"/\\[]:;|=,+*?<>()");
var badsSIDchars = new String(" \"/\\[]:;|=,+*?<>()@");
var badFieldchars = new String("\"\\");
var alphabet = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); // same as decrypt.c but no '*'

function blankStatus()
	{
	window.status="";
	}

function doLogin(formname) 
{
	doTallerLogin(1,formname);
}

function doTallerLogin(extra,formname ) {
	
	if (!checkCookies())
	{
		window.open("dpec/nocookies.htm");
		return;
	}
	
	currentform = document.getElementById(formname);
	
	if (checkFields(formname))
	{
		
		
		if (extra > 0) setTimeout( "clearPasswordField()", 3000 );
		
			currentform.ac.value="login";
			currentform.action = currentform.page.value;
			//currentform.action = "index.php";
			currentform.submit();
		
	} else {
		currentform.action = "";
		return;
	}
}

// Check for the ENTER key
function loginKeyPress(evt) {
	// be browser friendly
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		// more browser friendliness
		var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
		if (charCode == 13)
			doLogin();
	}
}

function checkCookies()
{

	setCookie("test","123");
	if (getCookie("test") != "123")
		return false;
	setCookie("test","",-1000);

	setCookie("test","123",1000);
	if (getCookie("test") != "123")
		return false;
	setCookie("test","",-1000);

	return true;
}

// allow for ASP/JSP flexibility
function fixExtension( url )
{
	var result = new String(url);
	var pos = result.lastIndexOf(".");
	if (pos != -1)
	{
		result = result.substr(0, pos + 1);
		if (mlomTech == "VB")
			result += "asp";
		else
			result += "jsp";
	}
	return result;
}

function clearPasswordField() {
	// Netscape 4.x needs this to be separated from the submit()
	document.form1.password.value = "";
}

function checkFields(formname) { 

currentform = document.getElementById(formname);

	if(EmptyField("ID", currentform.userid.value) || !ValidField("ID", currentform.userid.value, 30, badSIDchars)) {
		currentform.userid.focus();
		currentform.userid.select();
		return false;
	}

	if(currentform.userid.value != "") {
		if(!ValidPassword(currentform.password.value)) {
			currentform.password.focus();
			currentform.password.select();
			return false;
		}
	}
	return true;
}

function validatehFields() {
	if (checkfFields())
	{
		
		document.hover.action = "demodetails1.php";
		
		var d = new Date();
			d.setFullYear(d.getFullYear() + 2);
			document.cookie = "popDropin=" + document.lastModified +
					"; expires=" + d.toGMTString();
			SetCookie("ELCCUSTOMER","true",d);
			
		document.hover.submit;
	}
	return false;
}

function checkfFields() { 

	if(EmptyField("First Name", document.hover.firstname.value) || !ValidField("First Name", document.hover.firstname.value, 30, badsSIDchars)) {
		document.hover.firstname.focus();
		document.hover.firstname.select();
		return false;
	}
	
	if(EmptyField("Email ", document.hover.email.value) || !ValidField("Email", document.hover.email.value, 20, bademchars)) {
		document.hover.email.focus();
		document.hover.email.select();
		return false;
	}
	
	
		
	var i = document.hover.email.value.indexOf('@');
		if (-1 == i || 0 == i || i == (document.hover.email.value.length - 1)) {
			alert("Invalid e-mail address.");
			document.hover.email.focus();
			document.hover.email.select();
			return false;
		}
	
	return true;
}


// COMMON HTML PAGE FUNCTIONS >>>>>>>>>>>>>>>>>> 

function ValidField(fieldName, fieldValue, maxLength, badChars) {
	if(ValidLength(fieldName, fieldValue.length, maxLength))
		if(ValidChars(fieldName, fieldValue, badChars))
			return true;
	return false;
}

function ValidLength(fieldName, fieldLength, maxLength) {
	if(fieldLength <= maxLength)
		return true;
	else {
		alert(fieldName.toUpperCase() + " exceeds " + maxLength + " characters");
		return false;
	}
}

function ValidChars(fieldName, fieldValue, badChars) {
	for (var i=0; i<badChars.length; i++) {
		if (-1 != (fieldValue.indexOf(badChars.charAt(i)))) {
			alert("Bad character in " + fieldName.toUpperCase() + "\nThe following characters are not valid:\n" + FormatBadChars(badChars));
			return false;
		}
	}
	return true;
}

function FormatBadChars(badChars) {
	var fmtStr = new String();
	for (var i=0,j=0; i<badChars.length; i++) {
			fmtStr+= badChars.charAt(i);
			fmtStr+= " "; 
	}
	return fmtStr;
}

function EmptyField(fieldName, fieldValue) {
	if(fieldValue == "") {
		alert("Please enter " + fieldName.toUpperCase());
		return true;
	}
	else
		return false;
}

function ValidPassword(passWord) {

	if(ValidLength("Password", passWord.length, 11)) {
		for (var i=0; i<passWord.length; i++) {
			if (-1 == (alphabet.indexOf(passWord.charAt(i)))) {
				alert("Bad Password: Only alphanumerics are valid.");
				return false;
			}
		}
		return true;
	}
	return false
}

function setCookie(name, value, seconds)
	{
		var today = new Date();
		var expires = null;
		if (typeof(seconds) != "undefined" && seconds != "session")
		{
			expires = new Date();
			expires.setTime(today.getTime() + (seconds * 1000));
		}
		thisCookie = name + "=" + escape(value) + "; path=/"
			+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
		document.cookie = thisCookie;
	}
	
function SetpopCookie(name,value)
{
	var argv=SetpopCookie.arguments;
	var argc=SetpopCookie.arguments.length
	var expires=(argc>2) ? argv[2] : null
	var path=(argc>3) ? argv[3] : null
	var domain=(argc>4) ? argv[4] : null
	var secure=(argc>5) ? argv[5] : false
	document.cookie=name + "=" +escape(value) + 
	((expires==null) ? "" :( ";expires=" + expires.toGMTString())) +
	((path==null) ? "" :( ";path=" + path)) +
	((domain==null) ? "" :( ";domain=" + domain)) +
	((secure==true) ? "; secure " : "")
	
}


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 ContinueShopping(page) {
window.location=page;

}

function CheckCoupon(){
	CouponCode = document.formcheckout.couponcode;
	
	if (CouponCode.value!=''){
	
		document.formcheckout.action='cart.php'
	}else{
		document.formcheckout.action='cart/checkout.php'
	}
	
	document.formcheckout.submit;
}


function validateEnquiry() { 

	if(EmptyField("Question ", document.enquiry.question.value)) {
		document.enquiry.question.focus();
		document.enquiry.question.select();
		return false;
	}
	
	if(EmptyField("Email ", document.enquiry.email.value) || !ValidField("Email", document.enquiry.email.value, 30, bademchars)) {
		document.enquiry.email.focus();
		document.enquiry.email.select();
		return false;
	}
	
	if(EmptyField("Security Code ", document.enquiry.security_code.value)) {
		document.enquiry.security_code.focus();
		document.enquiry.security_code.select();
		return false;
	}
	
	
		
	var i = document.enquiry.email.value.indexOf('@');
		if (-1 == i || 0 == i || i == (document.enquiry.email.value.length - 1)) {
			alert("Invalid e-mail address.");
			document.enquiry.email.focus();
			document.enquiry.email.select();
			return false;
		}
	
	return true;
}

function validate(){
	if (validateEnquiry())
	{
		document.enquiry.action = 'enquiries.php';
		document.enquiry.submit();
		
	} else {
		currentform.action = "";
		return;
	}
}
