/* 
CALL THE FUNCTION THIS WAY 
return crs_validInput('form number=form field name^field name to display in error alert^type of alert|form field name^field name to display in error alert^type of alert');

NOTE: form number is a reference to the specific form in the forms[] array. If the first form on the page needs to be validated, the correct value is 0.

TYPES OF ALERTS (SO FAR):
text = field cannot be 0 length
number = must be a numeric value
email = regex that searches for the '@', '.' and stuff on both sides
alpha = numbers and letters, both upper and lower case
menu = selected index cannot be -1 -- the first item in a select drop down

ADDED FOR MAGIC RACK
numberMinimum = must be a numeric value that's not lower than the minimum value of the minimum form field

*/
/*
==================
OH Yeah, I spent many hours building this thing. If you're reading this, you've stolen my code. That's OK, I can't stop you. Just send me an email and let me know what you think of this lovely little function. 

Jer@jshdesigns.com
==================
*/

/* This constructor holds information on the individual field that's being validated - It's called in crs_validInput() */ 
function fieldToValidate(field,description,type) { 
	this.field = field;
	this.description = description;
	this.type = type;
 }////////////////////////////////////////////////////////////////////////////////////////////////


/* This function validates fields of type "text" */
function crs_validText(theForm,field,description) {

	var errorText = '> ' + description.toUpperCase() + ' must be completed.\n';	
	var okText = '';

	if (document.forms[theForm][field].value.length < 1) {	
	return errorText;
	}
	else { return okText; }
}////////////////////////////////////////////////////////////////////////////////////////////////


/* This function validates fields of type "numberMinimum" */
function crs_validNumberMinimum(theForm,field,description) {
	var errorText = '> ' + description.toUpperCase() + ' must be a numeric value.\n';
	var errorText2 = '> There is a ' + document.forms[theForm].elements['qty_min'].value + ' piece minimum order.\n';	
	var userQty = document.forms[theForm][field].value;
	var minQty = document.forms[theForm].elements['qty_min'].value;
	var okText = '';
	
	//alert('qty = ' + document.forms[theForm][field].value + '\n' + 'minimum = ' + document.forms[theForm].elements['qty_min'].value);
	
	if (isNaN(document.forms[theForm][field].value) || document.forms[theForm][field].value.length < 1) {	
	return errorText;
	}
	
	else if (userQty - minQty < 0) {
	return errorText2;
	}
	else { return okText; }
}////////////////////////////////////////////////////////////////////////////////////////////////



/* This function validates fields of type "number */
function crs_validNumber(theForm,field,description) {
	var errorText = '> ' + description.toUpperCase() + ' must be a numeric value.\n';	
	var okText = '';

	if (isNaN(document.forms[theForm][field].value) || document.forms[theForm][field].value.length < 1) {	
	//alert(document.forms[0][field].value.length);
	return errorText;
	}
	else { return okText; }
}////////////////////////////////////////////////////////////////////////////////////////////////


/* This function validates fields of type "email"*/
function crs_validEmail(theForm,field,description) {
	var strng = document.forms[theForm][field].value;
	var emailFilter=/^.+@.+\..{2,3}$/;
	var errorText = '> ' + description.toUpperCase() + ' must be a valid address\n';	
	var okText = '';


	if (!emailFilter.test(strng)) {	
	return errorText;
	}
	else { return okText; }
}////////////////////////////////////////////////////////////////////////////////////////////////

/* This function validates fields of type "menu" */
function crs_validMenu(theForm,field,description) {
	var errorText = '> Make a selection from the ' + description.toUpperCase() + ' menu.\n';	
	var okText = '';
	
	if (document.forms[theForm][field].selectedIndex <= 0) {	
	return errorText;
	}
	else { return okText; }
}////////////////////////////////////////////////////////////////////////////////////////////////



/* This function validates fields of type "alpha" == only alpha-numeric values*/
function crs_validAlpha(theForm,field,description) {
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = document.forms[theForm][field].value;
	var emailFilter=/^.+@.+\..{2,3}$/;
	var errorText = '> ' + description.toUpperCase() + ' can only contain numbers and letters.\n';
	var blankText = '> ' + description.toUpperCase() + ' must be provided.\n';
	var okText = '';
	var allValid = true;

if (checkStr.length > 0) {
	 for (t=0;t<checkStr.length;t++) {

		currentChar = checkStr.charAt(t);
		currentValid = checkOK.indexOf(currentChar);
		
		if (currentValid == -1) {
		allValid = false;
		}
	}

	if (!allValid) {return errorText;}
	else { return okText; }

}
else { return blankText; }
}////////////////////////////////////////////////////////////////////////////////////////////////




/*===================== 
MASTER crs_validInput() FUNCTION 
=====================*/

function crs_validInput(fields) { // fields format = form number=field1 name^field1 name to be displayed in the alert^type of validation|field2 name^field2 name to be displayed in the alert^type of validation

var theError = ' ';

var whichForm = fields.split('=');

var theForm = whichForm[0];
var theString = whichForm[1];
var splitString = theString.split('|');

theError = '';


// Create new main array. 
var individualField = new Array();

	for (i=0;i<splitString.length;i++) {
		tempString = splitString[i].split('^');
		individualField[i] = new fieldToValidate(tempString[0],tempString[1],tempString[2]); 

			// Field type "text" validation -- calls crs_validText()
			if (individualField[i].type == 'text') { 
				var addError = crs_validText(theForm,individualField[i].field,individualField[i].description);
				theError += addError;	 
			}

			// Field type "number" validation -- calls crs_validNumber()
			if (individualField[i].type == 'number') { 
				var addError = crs_validNumber(theForm,individualField[i].field,individualField[i].description);
				theError += addError;	 
			}
			
			// Field type "email" validation -- calls crs_validEmail()
			if (individualField[i].type == 'email') { 
				var addError = crs_validEmail(theForm,individualField[i].field,individualField[i].description);
				theError += addError;	 
			}

			// Field type "alpha" validation -- calls crs_validAlpha()
			if (individualField[i].type == 'alpha') { 
				var addError = crs_validAlpha(theForm,individualField[i].field,individualField[i].description);
				theError += addError;	 
			}
			// Field type "menu" validation -- calls crs_validMenu()
			if (individualField[i].type == 'menu') { 
				var addError = crs_validMenu(theForm,individualField[i].field,individualField[i].description);
				theError += addError;	 
			}
			
			// Field type "numberMinimum" -- calls crs_validNumberMinimum()
			if (individualField[i].type == 'numberMinimum') {
				var addError = crs_validNumberMinimum(theForm,individualField[i].field,individualField[i].description);
				theError += addError;
			}
	}



//Display the alert box if there's an error - if not submit the form
if (theError != '') {
		//Set the position of the alert box
		alertPosition();
	
		if (navigator.platform.indexOf('Win') > -1) {
				toggleDiv_pc(true);
			}
		else {
				toggleDiv_mac(true);
			}

		document.errorForm.error.value = theError;
		//alert('no good\n' + document.forms[0].elements['option-1'].selectedIndex);
		return false;
		}
else {
	return true;
	}
}/////////////////////////////////// END MASTER crs_validInput() function

/* MAC SHOW HIDE ALERT FUNCTION*/
function toggleDiv_mac(state) {
	var id = 'alertBox';
	if (state){
	if (document.layers) document.layers[''+id+''].visibility = "show"
	else if (document.all) document.all[''+id+''].style.visibility = "visible"
	else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
	}
	
	else
	if (!state){
	if (document.layers) document.layers[''+id+''].visibility = "hide"
	else if (document.all) document.all[''+id+''].style.visibility = "hidden"
	else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
	}
}


/* PC SHOW DIV/IFRAME FUNCTION == ALLOWS THE ALERT BOX TO DISPLAY ON TOP OF SELECT BOXES*/
function toggleDiv_pc(state)
  {
   var DivRef = document.getElementById('alertBox');
   var IfrRef = document.getElementById('alertFrame');
   if(state)
   {
    DivRef.style.display = "block";
    IfrRef.style.width = DivRef.offsetWidth;
    IfrRef.style.height = DivRef.offsetHeight;
    IfrRef.style.top = DivRef.style.top;
    IfrRef.style.left = DivRef.style.left;
    IfrRef.style.zIndex = DivRef.style.zIndex - 1;
    IfrRef.style.display = "block";
   }
   else
   {
    DivRef.style.display = "none";
    IfrRef.style.display = "none";
   }


}/////////////////////////////////////////////////////



/* BEGIN IMAGE PRELOAD FUNCTION */

if (document.images) {
	close1 = new Image;
	close2 = new Image;
 	close3 = new Image;	
	 
	close1.src = 'js/close_up.gif';
	close2.src = 'js/close_over.gif';
	close3.src = 'js/close_down.gif';
 }
/////////////////////////////////////////////////////


/* CREATE THE ALERT BOX */

if (navigator.platform.indexOf('Win') > -1) {
document.write('<IFRAME id="alertFrame" style="DISPLAY: none; LEFT: 0px; POSITION: absolute; TOP: 0px" src="javascript:false;" frameBorder="0" scrolling="no"></IFRAME>');
document.write('<div id="alertBox" style="display:none;position:absolute;top:20px;left:100px;z-index:1000;width:400px;height:230px;background-color:#dcdcdc;border-left:1pt solid black;border-top:1pt solid black;border-right:3pt solid black;border-bottom:2pt solid black;font-family:verdana,arial,sans-serif;font-size:10px">');
document.write('<img src="js/closeicon.gif" border="0"><br><br>');
document.write('<form name="errorForm" style="display:inline;padding-left:5px"><textarea name="error" onFocus="this.blur();" style="width:380px;height:150px;background-color:#f5f5f5;font-family:verdana,arial,sans-serif;font-size:10px;border:1pt solid #808080"></textarea></form>');
document.write('<img src="js/close_up.gif" align="right" name="close" border="0" vspace="8" onClick="toggleDiv_pc(false);" onMouseOver="document.images.close.src=close2.src" onMouseDown="document.images.close.src=close3.src" onMouseOut="document.images.close.src=close1.src">');
document.write('</div>');
}
else {
document.write('<div name="alertBox" id="alertBox" style="visibility:hidden;position:absolute;top:20px;left:150px;z-index:1000;width:400px;height:230px;background-color:#dcdcdc;border-left:1pt solid black;border-top:1pt solid black;border-right:3pt solid black;border-bottom:2pt solid black;font-family:verdana,arial,sans-serif;font-size:10px">');
document.write('<img src="js/closeicon.gif" border="0"><br><br>');
document.write('<form name="errorForm" style="display:inline;padding-left:5px"><textarea name="error" onFocus="this.blur();" style="width:380px;height:150px;background-color:#f5f5f5;font-family:verdana,arial,sans-serif;font-size:10px;border:1pt solid #808080"></textarea></form>');
document.write('<img src="js/close_up.gif" align="right" name="close" border="0" vspace="8" onClick="toggleDiv_mac(false);" onMouseOver="document.images.close.src=close2.src" onMouseDown="document.images.close.src=close3.src" onMouseOut="document.images.close.src=close1.src">');
document.write('</div>');
}




/* Position the alert box to appear at the same place, regardless of whether or not the page has been scrolled */
function alertPosition() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
	var theBox = document.getElementById('alertBox');
	
	theBox.style.top = (scrOfY) + 20;
	theBox.style.left = (screen.width - 400) / 2;
	//theBox.style.left = (scrOfX) + 100;
}



/*function alertPosition() {
var myWidth = 0;
var myHeight = 0;

if(typeof( window.innerWidth ) == 'number') {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}

	var theBox = document.getElementById('alertBox');
	

	theBox.style.left = (myWidth/2) - (theBox.style.width.replace('px','')/2);

	if (myHeight < theBox.style.height.replace('px','')) {
	theBox.style.top = (myHeight/2) + (theBox.style.height.replace('px','')/2);
	} else { theBox.style.top = (myHeight/2) - (theBox.style.height.replace('px','')/2); }


}*/











