var validator_error_string = "";
var validator_error_fields = new Array();

function Validator(formname)
{
	// The following call to validator_reset() is commented out to prevent the errors from clearing in case of server side errors.
	//validator_reset();
	this.formobj = document.forms[formname];
	if (!this.formobj) { return; }
	
	/* redirect the onsubmit function 
	this.formobj.orig_onsubmit = null;
	if (this.formobj.onsubmit) { this.formobj.orig_onsubmit = this.formobj.onsubmit; }
	this.formobj.onsubmit = validator_submit;
	*/
	this.addValidation = validator_add;
}
function validator_test()
{
	return true;
}


function validator_add(itemname,continueErrors,validatorString,errorField,errorMsg)
{
	if (!this.formobj) { return; }

	var itemobj = this.formobj[itemname];
	if (!itemobj) { return;	}

	if (!itemobj.validations)	{ itemobj.validations = new Validations(itemobj); }
	itemobj.validations.add(validatorString,continueErrors,errorField,errorMsg);
}


function validator_submit()
{
	for(var i=0; i<this.elements.length; i++)
	{
		if (this.elements[i].validations && !this.elements[i].validations.validate())
		{
		  return false;
		}
	}
	return true;
}


function Validations(inputitem)
{
	this.vals     = new Array();
	this.itemobj  = inputitem;

	this.add      = validations_add;
	this.validate = validations_validate;
}

function validations_add(validatorString,continueErrors,errorField,errorMsg)
{
  this.vals[this.vals.length] = new Validation(this.itemobj,continueErrors,validatorString,errorField,errorMsg);
}


function validations_validate()
{
	var bReturn = true;
	for(var i=0;i<this.vals.length;i++)
	{
		if (bReturn == true || this.vals[i].continueErrors == true)
		{
			if (this.vals[i].validate() == false)
			{
				bReturn = false;
			}
		}
	}
	return bReturn;
}


function Validation(inputitem,continueErrors,validatorString,errorField,errorMsg)
{
	this.validatorString = validatorString;
	this.continueErrors  = continueErrors;
	this.errorMsg        = errorMsg;
	this.errorField		 = errorField;
	this.itemobj         = inputitem;
	this.validate = validation_validate;
}


function validation_validate()
{
	//radio buttons must be handled separately
	if(this.validatorString == "radio")
	{
		if(!validateData(this.validatorString,this.itemobj,this.errorField,this.errorMsg))
		{
			return false;
		}
		return true;
	}
	
	if(!validateData(this.validatorString,this.itemobj,this.errorField,this.errorMsg))
	{
		try{
		this.itemobj.focus();
		}catch(e){}
		return false;
	}
	return true;
	
}



function validateData(strValidateStr,objValue,strErrorField,strErrorMsg) 
{ 
	var validateParms = strValidateStr.split(",");
	var validateCmd = validateParms[0];
	
	var strFunction = "validate_" + validateCmd + "(objValue,strErrorField,strErrorMsg";
	for (var i=1; i<validateParms.length; i++)
	{
		strFunction += ',"' + validateParms[i] + '"';
	}
	strFunction += ")";
	return eval(strFunction);
}


// This function adds an error to the array of errors
function add_error(strErrorField,strErrorMsg)
{
	var element = document.getElementById(strErrorField);
	var fieldclass = "error";
	// Only for the BML Terms & Conditions checkbox, the id should set to 'field errorfield small'
	if(strErrorField == 'BMLTermsConditionsError' || strErrorField == 'BMLElectronicTermsError')
	{
		fieldclass += '_tc small';
	}
	element.className = fieldclass;
	validator_error_fields[validator_error_fields.length] = element;
	validator_error_string = validator_error_string + strErrorMsg;
}

// This function adds an error to the array of errors
function add_shipToStoreError(strErrorField,strErrorMsg)
{
	var element = document.getElementById(strErrorField);
	var fieldclass = "inset_field errorfield";
	
	element.className = fieldclass;
	validator_error_fields[validator_error_fields.length] = element;
	validator_error_string = validator_error_string + strErrorMsg;
}

// This function resets the field back to the original div class
function validator_reset()
{

	validator_error_string = "";
	$('topErrorMessages').hide();


	for (var i=0; i< validator_error_fields.length; i++)
	{	
		
			//alert("times through" + i);
			/*if(validator_error_fields[i].hasClassName('inset_field')){
			alert("postif" + i);
				validator_error_fields[i].className = "inset_field";
				}
			else{
			alert("post else");*/
				validator_error_fields[i].className = "field";	
			//	}
	}

	validator_error_fields = new Array();
	
}

// This function used by the Register form resets the field back to the original div class
function register_validator_reset()
{
//alert("in register_validator_reset() ...");	
	validator_error_string = "";
    var jObj = jQuery('#topErrorMessages');
	jObj.hide();

	for (var i=0; i< validator_error_fields.length; i++)
	{	
		validator_error_fields[i].className = "text";	
		//set class of the error message
		//$("span#errMsg").addClass("error");
		//hide error message
		errMessageId = "div#"+validator_error_fields[i].id+" span#errMsg";  //e.g.  "div#firstNameDiv span#errMsg"
		jObj = jQuery(errMessageId);
		jObj.hide();

	}

	validator_error_fields = new Array();

}

function validation_display_errors(stringTitle)
{
//alert("in validation_display_errors");
	var jObj = null;
	if((validator_error_string == "") || validator_error_string == null)
	{	
		return true;
	}
	else
	{

		var errorMessage = stringTitle + validator_error_string;
		$('topErrorMessages').update(errorMessage);
		$('topErrorMessages').className="error";		
		$('topErrorMessages').show();
		window.scrollTo(0,0);
		return false;
		
		//highlight each error field in red and show error message
		for (var i=0; i< validator_error_fields.length; i++)
		{	
			validator_error_fields[i].className = "field";	
		}
	}
}

//This function displays errors for the new Register form
//Expects each field to be inside a Div tag with id that ends in 'Div' e.g. 'firstNameDiv'
//Expects each field to have a corresponding Span tag with an error message,
//Span id should end in 'Err' e.g. 'firstNameErr'
function register_display_errors()
{
//alert("in NEW register_display_errors() ...");
	var jObj = null;
	if((validator_error_string == "") || validator_error_string == null)
	{	
		return true;
	}
	else
	{
		//show generic error message at top of the form
	    jObj = jQuery('#topErrorMessages');
	    jObj.show();

		//highlight each error field in red and show error message
		var errMessageId;
		for (var i=0; i< validator_error_fields.length; i++)
		{	
				//set class of the bad Div to "error"
				validator_error_fields[i].className = "error text";	
				//set class of the error message
				jObj = jQuery('span#errMsg');
				jObj.addClass("error");

				//set error message to show under the input field
				errMessageId = "div#"+validator_error_fields[i].id+" span#errMsg";  //e.g.  "div#firstNameDiv span#errMsg"
				jObj = jQuery(errMessageId);
				jObj.css('display', 'block');
		}
	}		
	return false;
}	

function render_validation_errors(stringTitle, objID) {
  var hasErrors = false;
  var jObj = null;

  if((validator_error_string != null) && (validator_error_string.length > 0)) {
    hasErrors = true;
    jObj = jQuery('#'.concat(objID));
    jObj.html(stringTitle.concat(validator_error_string));
    jObj.show();
    window.scrollTo(0,0);
  }
  return hasErrors;
}

// This function resets the field back to the original div class
function clear_validation_errors(objID) {
  var jObj = jQuery('#'.concat(objID));

  jObj.html("");
  jObj.hide();

  for (var index = 0; index < validator_error_fields.length; index++) {
    validator_error_fields[index].className = "field";
  }

  validator_error_string = "";
  validator_error_fields = new Array();
}

