
var validator_error_string = "";
var validator_error_fields = new Array();
var formname = "undefined";

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.formname = formname;
	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;
	this.removeValidation = validator_remove;
}

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;	}
	errorMsg = addLinkToErrorMessage(itemname, this.formname, errorMsg);
	if (!itemobj.validations)	{ itemobj.validations = new Validations(itemobj); }
	itemobj.validations.add(validatorString,continueErrors,errorField,errorMsg);
}

function validator_remove(itemname) {
	if (!this.formobj) { return; }
	var itemobj = this.formobj[itemname];
	if (!itemobj) { return;	}
	if (itemobj.validations) {
		itemobj.validations = new Validations(itemobj);
	}
}

/**
 * Function builds a an <a href=javascript link around a form objects errror text.<b>
 */
function addLinkToErrorMessage(fieldName, formName, errorMsg){
   //Create a link that will take focus to the error box. 
   return "<a href='javascript:linkToError(\""+fieldName+"\",\""+formName+"\")'>"+errorMsg+"</a>";
}

/**
 * Applies formating to error message
 */
function addFormatToErrorMessage(errorMsg){
   return "<em>"+errorMsg+"</em>"
}

/** 
 * Function moves focus to the specified field.
 * fieldName - String value, the name of the field to go to.
 * formName - String value, the name of the form the field is on. 
 */
function linkToError(fieldName,formName){
   var form = document.forms[formName];
   var field = form[fieldName];
   field.focus();
}


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(strErrorField,strErrorMsg)
{
	var fieldclass = "errortxt";
	if(strErrorField != null)
	{
		var element = document.getElementById(strErrorField);
	
		// 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;
		validator_error_fields[validator_error_fields.length] = element;
	}
	strErrorMsg = addFormatToErrorMessage(strErrorMsg);
	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;
	strErrorMsg = addFormatToErrorMessage(strErrorMsg);
	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_donot_scroll(stringTitle, errorDivId)
{
	var browser = navigator.appName;
	var platform= navigator.platform;
	var x=window.pageXOffset; //document.body.scrollLeft; 
	var y=window.pageYOffset; //document.body.scrollTop; 
	
	if(	browser == "Microsoft Internet Explorer" && platform == "Win32" )
	   { 
	       x = document.documentElement.scrollLeft;
	       y = document.documentElement.scrollTop;
	  }
	  // alert("x="+x+" y="+y+navigator.platform);
	if((validator_error_string == "") || validator_error_string == null)
	{	
		return true;
	}
	else
	{
		var errorMessage = stringTitle + validator_error_string;
		
		if (errorDivId == null)
		{
			errorDivId = "topErrorMessages";
		} 
		
		$(errorDivId).update(errorMessage);
		$(errorDivId).className="error";		
		$(errorDivId).show();
		window.scrollTo(x,y);
		return false;
	}
}

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";
		} 
		
		$(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]/);
}
