
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.validator_submit = 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()
{
	var validated = true;
	for(var i=0; i<this.formobj.elements.length; i++)
	{
		if (this.formobj.elements[i].validations && !this.formobj.elements[i].validations.validate())
		{
		  validated = false;
		}
	}
	return validated;
}


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()
{
	if(!validateData(this.validatorString,this.itemobj,this.errorField,this.errorMsg))
	{
		this.itemobj.focus();
		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(elementId,strErrorField,strErrorMsg)
{
	var element = document.getElementById(strErrorField);
	var fieldclass = "errortxt";
	
	// The nickname field on Address Add/Edit page has one more css class nickname which needs to be appended to the error class.
	if(strErrorField == 'nickNameError')
	{
		fieldclass += ' nickname';
	}

	element.className = fieldclass;
	// check needed for confirm password equality, it displays a dot if it allowed to enter this condition logic
	if(strErrorField != 'conf_new_pw_label')
	{
		addErrorByID(strErrorMsg,strErrorField);
		showErrorSmartPanel();
		var strErrorId = '#' + elementId;
		displayAlert($(elementId));
	}
//	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(errorDivId)
{
	if (errorDivId == null)
	{
		errorDivId = "topErrorMessages";
	}

	validator_error_string = "";
//	$(errorDivId).hide();
	for (var i=0; i< validator_error_fields.length; i++)
	{
			var error_field = validator_error_fields[i];

			if (error_field)
			{
				if(error_field.className == 'errtxt small')
				{
					error_field.className = "small";
				}
				else
				{
					error_field.className = "";
				}
			}
			
	}
	validator_error_fields = new Array();
}


function validation_display_errors(stringTitle, errorDivId)
{
	if((validator_error_string == "") || validator_error_string == null)
	{	
		return true;
	}
	else
	{
		var errorMessage = stringTitle + validator_error_string;
		
		if (errorDivId == null)
		{
			errorDivId = "topErrorMessages";
		} 
		//addError(errorMessage);
		//$(errorDivId).update(errorMessage);
		//$(errorDivId).className="error";		
		//$(errorDivId).show();
		//window.scrollTo(0,0);
		//return false;
	}
}
//////////////////////////////////////////////////////////
// This function will validate a positive integer quantity
//
// arg1 = the object you want info on...
// Return true is this input arg is a valid positive int,
// otherwise return false...
//////////////////////////////////////////////////////////
function wc_validateInt(intStr) {
    var validChars = "-0123456789";

    // if the string is empty it is not a valid integer
    if (isEmpty(intStr)) return false;

    // look for non numeric characters in the input string
    for (var i=0; i<intStr.length; i++) {
      if (validChars.indexOf(intStr.substring(i, i+1)) == "-1") {
        return false;
      }
    }
    // look for bad leading zeroes in the input string
    if (intStr.length > 1 && intStr.substring(0,1) == "0") {
       return false;
    }

    // "-" cannot only be at the very beginning
    if (intStr.lastIndexOf("-") >= 0) {
       return false;
    }

    var iValue = parseInt(intStr);
    if (isNaN(iValue) || iValue < -2147483648 || iValue > 2147483647) {
        return false;
    }
        
    return true;
}

function isEmpty(inputStr) {
   if (inputStr == null)
       return true;
   else    
       return !inputStr.match(/[^\s]/);
}

