
///////////////////////////////////////////////////////////////////////////
// make me an array of error messages
// heading
// message
// width of box
///////////////////////////////////////////////////////////////////////////
var errorMessage = new Array(11) // 2 total counting 0
						// error heading, error message, width of errorbox
errorMessage[0]= ['An Error Has Occured','Please correct the problem in the area below.','200'];
errorMessage[1]= ['Invalid Email Address','Please enter a valid email address.','200'];
errorMessage[2]= ['Invalid Password','Please enter a valid password.',''];
errorMessage[3]= ['Name Required','Please enter your name.','200'];
errorMessage[4]= ['Address Required','Please enter street address.',''];
errorMessage[5]= ['City Required','Please enter your city.',''];
errorMessage[6]= ['Zip Code Required','Please enter your zip code.',''];
errorMessage[7]= ['Account Name Required','Please enter your full name as you wouldl like it to appear on your account.',''];
errorMessage[8]= ['Social Security Number Invalid','Please enter a valid Social Security Number.',''];
errorMessage[9]= ['Invalid Date','Please enter the date as mm/dd/yyyy.',''];
errorMessage[10]= ['Invalid Number','Please enter digits only.',''];
errorMessage[11]= ['First Name Required','Please enter your first name.',''];
errorMessage[12]= ['Last Name Required','Please enter your last name.',''];
errorMessage[13]= ['Invalid Phone Number','Please enter a valid phone number.',''];



///////////////////////////////////////////////////////////////////////////
// autoValidateMe function - the lazyman's validator
///////////////////////////////////////////////////////////////////////////
function autoValidateMe(form) {
turnOffErrorFloat();
var formName = form.getAttribute('id');
var formObj = document.getElementById(formName);
var Error;
if (formObj) { inputs = formObj.getElementsByTagName("input"); }
for (var i = 0; i < inputs.length; i++) {
// reset all red inputboxes
if (inputs[i].getAttribute('type') == 'text') {
	var origClass = inputs[i].getAttribute('origClass');
		if (origClass) { inputs[i].className = origClass; }
		else { 
			if (inputs[i].getAttribute('class')) { 
			inputs[i].className = inputs[i].getAttribute('class');
			inputs[i].setAttribute("origClass",inputs[i].getAttribute('class'));
			}
	
		}
	}
}

for (var i = 0; i < inputs.length; i++) {
	if ((inputs[i].type == 'text') || (inputs[i].type == 'password')) {
	//alert(inputs[i].getAttribute('name'));
	var valAttribute = inputs[i].getAttribute('validate');
		if (valAttribute) {
			var b = inputs[i].getAttribute('m');
			if (!b) { b = 0; } //
			Error = validateMe(inputs[i].getAttribute('name'),formName,inputs[i].getAttribute('id'));
			if (Error) { break; }
		}
	}
	}
if (Error) { return false; }
else { return true; }
}


function makeObj(v) {
return eval('(' + v + ')'); 
}

///////////////////////////////////////////////////////////////////////////
// validateMe function - the master validator
///////////////////////////////////////////////////////////////////////////
function validateMe(name, formName, theId) {
//alert(name + ' - ' + theId);
// get the attribute that says what to validate
var outcome = false;
var vtype = document.getElementById(theId).getAttribute('validate');
var options = makeObj(vtype);

if (typeof(options.m) == 'object') {
//, head, message, width - errorMessage[b][0],errorMessage[b][1],errorMessage[b][2]
var head = options.m.heading;
var message = options.m.body;
var width = options.m.width;
options.m
} 
else {
//, head, message, width - errorMessage[b][0],errorMessage[b][1],errorMessage[b][2]
var head = errorMessage[options.m][0];
var message = errorMessage[options.m][1];
var width = errorMessage[options.m][2];
}
//digit length stuff
if (options.len) {
	var can_be_blank = options.len.blank;
	var min = options.len.min;
	var max = options.len.max;
	if (!max) { max = min; }
}

	// is it an email address
		if (options.type == 'email') {
			outcome = isEmailAddr(document[formName][name].value);
			}
	// is it empty
		else if (options.type == 'blank') {
			if (document[formName][name].value == '') { outcome = true; }
		}
	// is a zip code
		else if (options.type == 'zipCode') {
			outcome = checkzip(document[formName][name]);
		}
	// is a digitLength
		else if (options.type == 'digitLength') {
			outcome = checklength(document[formName][name],min,max,can_be_blank);
		}
	// is a phone number
		else if (options.type == 'phoneNumber') {
			outcome = checkphone(document[formName][name]);
		}
	// is a social security number
		else if (options.type == 'social') {
			outcome = checkSocial(document[formName][name]);
		}
	// is a date
		else if (options.type == 'date') {
			outcome = checkDate(document[formName][name]);
		}
	// is all digits
		else if (options.type == 'digit') {
			outcome = digitsonly(document[formName][name].value);
		}
	
	// if there was an error, show the box	
	if (outcome) {
		displayErrorFloat(theId,formName,head,message,width,options.align);
	}
return outcome;
}



//////////////////////////////////////////////////////////////////////
// make the current error message disappear
//////////////////////////////////////////////////////////////////////

function turnOffErrorFloat() {
	document.getElementById('errorContainer').style.display = 'none';
	document.getElementById('errorBottomFloat').style.display = 'none';
}


//////////////////////////////////////////////////////////////////////
// display floating error message
//////////////////////////////////////////////////////////////////////
function displayErrorFloat(obj,formName,heading,body,w,alignment) {
// turn off any existing error boxes
turnOffErrorFloat();

// assign header and body
document.getElementById('errorHeadingFloat').innerHTML = heading;
document.getElementById('errorBodyFloat').innerHTML = body;

// find positions of form element
var inputX = findPosX(document.getElementById(obj));
var inputY = findPosY(document.getElementById(obj));
var inputW = document.getElementById(obj).offsetWidth;

// display the errorContainer
document.getElementById('errorContainer').style.display = "block";

// if width param was passed, the assign it to box
if (w) { document.getElementById('errorContainer').style.width = w + 'px'; }

// find the width and height of the errorContainer
var errorContainerW = document.getElementById('errorContainer').offsetWidth;
var errorContainerH = document.getElementById('errorContainer').offsetHeight;

//alert(alignment);
//var alignObj = makeObj(alignment);

if (typeof(alignment) == 'object') { 
 document.getElementById('errorContainer').style.left = (inputX + parseInt(alignment.left));
 document.getElementById('errorContainer').style.top = (inputY + parseInt(alignment.top));
}

else {
// left alignment
if (alignment == 'right') { document.getElementById('errorContainer').style.left = ((inputX + inputW) - 40); }
else if (alignment == 'center') { document.getElementById('errorContainer').style.left = ((inputX + (inputW/2)) - (errorContainerW/2)); }
else { document.getElementById('errorContainer').style.left = (inputX - 10); }

// top alignment
inputY = inputY - (errorContainerH + 20);
document.getElementById('errorContainer').style.top = inputY + "px";
}



errorContainerX = findPosX(document.getElementById('errorContainer'));
errorContainerY = findPosY(document.getElementById('errorContainer'));

if (alignment == 'right') { document.getElementById('errorBottomFloat').style.left = (errorContainerX + 20); }
else if (alignment == 'center') { document.getElementById('errorBottomFloat').style.left = ((errorContainerX + (errorContainerW/2) - 12)); }
else { document.getElementById('errorBottomFloat').style.left = (errorContainerX + 20); }
document.getElementById('errorBottomFloat').style.top = (errorContainerY + (errorContainerH - 3));
document.getElementById('errorBottomFloat').style.display = "block";


// make the input box turn red and add focus to it
document[formName][obj].className = "inputError";
//document[formName][obj].style.backgroundColor = "#F8E0DE";
document[formName][obj].focus();

new Effect.ScrollTo('errorContainer', {offset: -30, duration: .2, queue: 'end'});
}














function isEmailAddr(email) {  
var result = true; 
var theStr = new String(email); 
var index = theStr.indexOf("@"); 
if (email == '') { return true; } 
else if (index > 0) { var pindex = theStr.indexOf(".",index);
if ((pindex > index+1) && (theStr.length > pindex+1)) result = false; }
return result; 
}

function checkzip(obj) {
var zip = getNumber(obj.value);
obj.value = zip;
if (zip.length < 5) { return true; }
else { return false; }
}

function checkphone(obj) {
var phone = getNumber(obj.value);
obj.value = phone;
if (phone.length < 10) { return true; }
else { return false; }
}

function checklength(obj,min,max,can_be_blank) {
var oblen = getNumber(obj.value);
var origvalue = obj.value;
obj.value = oblen;
//alert(oblen + '<' + min + ' - ' + oblen + '>' + max);

//alert('min:' + (oblen >= parseInt(min)));
//alert('max:' + (oblen <= parseInt(max)));

// can be blank
if ((oblen.length >= parseInt(min)) && (oblen.length <= parseInt(max))) { return false; }
else { 
		if (can_be_blank == 'yes') { 
			if (origvalue == '') { return false; }
			else { return true; }
		}
		else {
			return true;
		}
 }

}

function checkSocial(obj) {
var soc = getNumber(obj.value);
soc = formatSocial(soc);
if (soc != "--") { obj.value = soc; }
if (soc.length != 11) { return true; }
else { return false; }
}

function checkDate(obj) {
var d = getNumber(obj.value);
d = formatDate(d);
if (d != "//") { obj.value = d; }
if (d.length != 10) { return true; }
else { return false; }
}

function digitsonly(wee) {
zee = getNumber(wee);
if ((wee.length != zee.length) || (wee.length == 0) || (wee == '')) {
return true;
}
}

////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////

function checkblank(n,c,t) {
	if (document.tf[n].value == ""){
	  alert(c);
	  if (t != '1') { document.tf[n].focus(); }
	return false;
	}
	else { return true; }
}

function checkemail(n,c) {
if ((document.tf[n].value == "") || (!isEmailAddr(document.tf[n].value))) {
        alert(c);
	document.tf[n].focus();
	return false;
	}
else { return true; }
  }


function checkpayment() {
	if (document.tf.paymenttype.value == ""){
	  alert('Please select a payment type.');
	  document.tf.paymenttype.focus();
	return false;
	}
	else if (document.tf.paymenttype.value != "Check") { 
		if (document.tf.nameoncard.value == "") { alert('Please enter the name as it appears on the credit card.'); document.tf.nameoncard.focus(); return false; }
		var cc = getNumber(document.tf.ccnumber.value);
		document.tf.ccnumber.value = cc;
		if (!cc > 0) { alert('Please enter a valid Credit Card Number.'); document.tf.ccnumber.focus(); return false; }
		else { return true; }
	}
	else { return true; }
}





function formatNumber(tobject)
	{
		var unformatted = stringReplace(tobject.value, "-", "")
		var formatted = unformatted.substring(0,3) + "-" + unformatted.substring(3,6) + "-" + unformatted.substring(6,10)
		if (unformatted.length > 10)
		{
			tobject.value = formatted + "-" + unformatted.substring(10,15)
		} else {
			tobject.value = formatted
		}
	}
	
function formatSocial(tobject)
	{
		var unformatted = stringReplace(tobject, "-", "");
		var formatted = unformatted.substring(0,3) + "-" + unformatted.substring(3,5) + "-" + unformatted.substring(5,9);
		return formatted;
	}	
	
function formatDate(tobject)
	{
		var unformatted = stringReplace(tobject, "/", "");
		var formatted = unformatted.substring(0,2) + "/" + unformatted.substring(2,4) + "/" + unformatted.substring(4,8);
		return formatted;
	}	
	
function stringReplace(originalString, find, replace)
	{
		var pos=0, preString, postString
		pos = originalString.indexOf(find)
		while (pos != -1)
		{
			preString = originalString.substring(0,pos)
			postString = originalString.substring(pos+1, originalString.length)
			originalString = preString + replace + postString
			pos = originalString.indexOf(find)
		}
		return originalString;
	}




function getNumber(sNumber)
{
	var valid = "0123456789";
	var temp;
	var sNewNumber = '';
	for (var i=0; i < sNumber.length; i++) 
	{
		temp = '' + sNumber.substring(i, i+1);
		if (valid.indexOf(temp) != "-1") 
		{
			sNewNumber = sNewNumber + temp;
		}
	}
	return sNewNumber;
}




