// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// (c) cubic international b.v. - 2000 - C.Brookes, M.Wingfield
//
//  A basic validation routine.
//  This file contains all the core functionality of the validation routines.
//  It was written by Colin in August 2000, and should not be changed
//  on pain of death...
//
// -----------------------------------------------------------------------

// -----------------------------------------------------------------------
// the global array storage
// -----------------------------------------------------------------------
var validateArray = new Array();
var validateFlag  = true;

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
function arrayItem(itmDescription, itmName, itmField, itmFunc, itmValue, itmDepends) {

	this.itmDescription		= itmDescription;	// what to display on error
	this.itmName	    	= itmName;			// what is the items name
	this.itmField       	= itmField;			// the form field name
	this.itmFunc	    	= itmFunc;			// the validate function name
	this.itmValue       	= itmValue;			// validate value for this item
	this.itmDepends     	= itmDepends;		// which items it depends on (an Array object)

	// additional flags for this object
	this.mustValidate 	= true;			// do we have to validate?
	this.returnValue  	= false;		// what is the return value?
	this.xdone	  		= false;		// have we run this one yet?
	this.ignore     	= false;		// do we need to final verify?

}

// -----------------------------------------------------------------------
// this routine adds the fields to the array - any number of fields.
// -----------------------------------------------------------------------
function addField() {
	var argv 		= addField.arguments;
	var argc 		= addField.arguments.length;
	var newType 	= false;

	// error checking
	if(argc < 3) {
		alert("invalid number of arguments in addField()");
		return false;
	}
	if(argc==4 || (argc!=3&&argc%2==1)) {
		xtmpitmName = argv[1] + argv[2] + argv[3];			// create the hashcode/name
		tmpitmDepends = dependsNameArray(addField.arguments, 4);		// build the depends array names
		newType = true;
	} else {
		xtmpitmName = argv[0] + argv[1] + argv[2];			// create the hashcode/name
		tmpitmDepends = dependsNameArray(addField.arguments, 3);		// build the depends array names
	}
	

	var foundFlag = false;
	// does the item already exist?
	for(i=0; i<validateArray.length; i++) {
		if(validateArray[i].itmName == xtmpitmName) {
			// yes it does
			validateArray[i].mustValidate = true;
			validateArray[i].itmDepends   = tmpitmDepends;
			foundFlag = true;
		}
	}
	
	// did we find it?
	if(foundFlag==false) {
		// no we didnt - so add a new one!
		if(newType == true) {
			validateArray[validateArray.length] = new arrayItem(argv[0], xtmpitmName, argv[1], argv[2], argv[3], tmpitmDepends);
			addAllDepends(addField.arguments, 4);				// add all the additional validations
		} else {
			validateArray[validateArray.length] = new arrayItem(argv[0], xtmpitmName, argv[0], argv[1], argv[2], tmpitmDepends);
			addAllDepends(addField.arguments, 3);				// add all the additional validations
		}
	}

}

// -----------------------------------------------------------------------
// create the dependencies string with conjunctions to an array!
// -----------------------------------------------------------------------
function dependsNameArray(args, countStart) {
	depArray = new Array();

	for(i=countStart; i<args.length; i+=4) {
		tmpitmName = args[i] + args[i+1] + args[i+2];		// create the hashcode/name
		depArray[depArray.length] = tmpitmName;			// add the hashcode/name
		if((i+countStart) < args.length) {
			depArray[depArray.length] = args[i+3];		// add the conjunction if it exists
		}
	}

	// return the array
	return depArray;
}

// -----------------------------------------------------------------------
// we check all the dependencies and add them if they dont exist!
// -----------------------------------------------------------------------
function addAllDepends(args, startCount) {

	for(i=startCount; i<args.length; i=i+4) {
		tmpitmName = args[i] + args[i+1] + args[i+2];		// create the hashcode/name
		for(ii=0; ii<validateArray.length; ii++) {
			// find out if it exists in the array already
			if(validateArray[ii].itmName == tmpitmName) {
				// yep, it does!
				return;
			}
		}

		// we got here, so it doesnt exist already
		itmCurrent = validateArray.length;
		validateArray[itmCurrent]              = new arrayItem(tmpitmName, args[i], args[i+1], args[i+2], null);
		validateArray[itmCurrent].mustValidate = false;		// not a real validation - dependency only
	}

}

// -----------------------------------------------------------------------
// check to see if everything is done yet.
// -----------------------------------------------------------------------
function allDone() {

	for(i=0; i<validateArray.length; i++) {
		if(validateArray[i].xdone == false) {
			return false;
		}
	}
	return true;
}

// -----------------------------------------------------------------------
// validateNow()
// This routine is specifically for having an 'onBlur' validation or an
// 'onChange' validation at the field level instead of the form level.
// You add the validation logic in the normal way, but set the validation
// test at the field level with this function.
// -----------------------------------------------------------------------
function validateNow(theField, itmFunc, itmValue) {

	validateType = eval("VALID" + itmFunc);
	returnValue = validateType(theField, itmValue);

	if(returnValue==false) {
		if(transInstalled==true) {
			alert(transGet("validateNow", "en"));
		} else {
			alert("invalid value in " + theField.name);
		}
	}
	return returnValue;

}

// -----------------------------------------------------------------------
// this is the routine which actually performs the validation
// We use the global array to determine the validation procedure.
// -----------------------------------------------------------------------
function validate(theForm, checkType) {
alert("wobbly");
	// --------------------------------------------------------------
	// make sure everything is initialised before running.
	// --------------------------------------------------------------
	for(notdone=0; notdone<validateArray.length; notdone++) {
		validateArray[notdone].xdone        = false;
		validateArray[notdone].returnValue = false;
		validateArray[notdone].ignore      = false;
	}

	// --------------------------------------------------------------
	// first we validate everything
	// --------------------------------------------------------------
	for(colini=0; colini<validateArray.length; colini++) {
		if(validateArray[colini].xdone!=true) {
			// we must run this one.
//	alert("before error");
	//alert(validateArray[colini]);
	//alert(validateArray[colini].itmFunc);
			this.validateType = eval("VALID" + validateArray[colini].itmFunc);
	//alert("after error");
			field = eval(theForm.name + "." + validateArray[colini].itmField);
		    validateArray[colini].returnValue = this.validateType(field, validateArray[colini].itmValue)
			validateArray[colini].xdone = true;
		}
	}

	// --------------------------------------------------------------
	// now we check the logic for each item to determine if we should
	// be influenced by the validation.
	// --------------------------------------------------------------
	for(x=0; x<validateArray.length; x++) {
		// for each item we check the itmDepends stuff
		if(validateArray[x].itmDepends!=null && validateArray[x].itmDepends.length > 0) {
			curPos = 0;
			curFlag = getReturnValue(validateArray[x].itmDepends[0]);
			while(curPos < (validateArray[x].itmDepends.length-2)) {
				con       = validateArray[x].itmDepends[curPos+1];
				rightSide = getReturnValue(validateArray[x].itmDepends[curPos+2]);

				// check the AND condition
				if(con == "and") {
					if(curFlag==true && rightSide==true) {
						curFlag=true;
					} else {
						curFlag=false;
					}
				}
				// check the OR condition
				if(con == "or") {
					if(curFlag==true || rightSide==true) {
						curFlag=true;
					} else {
						curFlag=false;
					}
				}
				curPos = curPos + 2;
			}
			// essentially we ignore the item if we end with a false result.
			if(curFlag==false) {
				validateArray[x].ignore = true;
			}
		}
	}

	// --------------------------------------------------------------
	// now we need to verify the logic for each of the primary fields
	// to determine the overall form boolean state.
	// --------------------------------------------------------------
	var validateFlag = true;

	for(cur=0; cur<validateArray.length; cur++) {

		if(validateArray[cur].mustValidate==false) continue;
		if(validateArray[cur].ignore      ==true)  continue;

		if(validateFlag!=false) {
			validateFlag = validateArray[cur].returnValue;
		}
	}
	
	// --------------------------------------------------------------
	// we need to display (possibly) the alert box with junk in it
	// --------------------------------------------------------------
	if(validateFlag == false) {
		errorMessage = "The following fields contain invalid data:-";

		for(bd=0; bd<validateArray.length; bd++) {
			if(validateArray[bd].returnValue==false && validateArray[bd].mustValidate==true &&validateArray[bd].ignore==false) {
				var obj = validateArray[bd];
				if (typeof obj.itmDescription != "string") {
					errorMessage = errorMessage + '\r' + obj.itmDescription.value;
				} else {
					errorMessage = errorMessage + '\r' + obj.itmDescription;
				}
				if(checkType=="StopOnFirst") {
					alert(errorMessage);
					return false;
				}
			}
		}
		alert(errorMessage);
		return false;
	}
	return true;
}

// -----------------------------------------------------------------------
// finds the item in the list and returns the return value
// -----------------------------------------------------------------------
function getReturnValue(itmName) {
	for(i=0; i<validateArray.length; i++) {
		if(itmName == validateArray[i].itmName) {
			return validateArray[i].returnValue;
		}
	}

	// how can the item be missing? - stupid user error.... (is the error or the user stupid?)
	return false;
}
//-------------------------------------------------
   //To choose the image for Product and Services
//-------------------------------------------------

function changeSelection(field) {
		Item = field.selectedIndex;
		// we need to update the preview image
		newImageFilename = '/images/resellertoolkit/templates/preview/' + field.options[Item].text + '.gif';
            document.Preview.src = newImageFilename;
		document.Preview.alt = field.options[Item].text;
	}


//modified 
function validate(theForm, checkType, msg) {

	// --------------------------------------------------------------
	// make sure everything is initialised before running.
	// --------------------------------------------------------------
	for(notdone=0; notdone<validateArray.length; notdone++) {
		validateArray[notdone].xdone        = false;
		validateArray[notdone].returnValue = false;
		validateArray[notdone].ignore      = false;
	}

	// --------------------------------------------------------------
	// first we validate everything
	// --------------------------------------------------------------
	for(colini=0; colini<validateArray.length; colini++) {
		if(validateArray[colini].xdone!=true) {
			// we must run this one.
//	alert("before error");
	//alert(validateArray[colini]);
	//alert(validateArray[colini].itmFunc);
			this.validateType = eval("VALID" + validateArray[colini].itmFunc);
	//alert("after error");
			field = eval("document."+theForm.name + "." + validateArray[colini].itmField);
		    validateArray[colini].returnValue = this.validateType(field, validateArray[colini].itmValue)
			validateArray[colini].xdone = true;
		}
	}

	// --------------------------------------------------------------
	// now we check the logic for each item to determine if we should
	// be influenced by the validation.
	// --------------------------------------------------------------
	for(x=0; x<validateArray.length; x++) {
		// for each item we check the itmDepends stuff
		if(validateArray[x].itmDepends!=null && validateArray[x].itmDepends.length > 0) {
			curPos = 0;
			curFlag = getReturnValue(validateArray[x].itmDepends[0]);
			while(curPos < (validateArray[x].itmDepends.length-2)) {
				con       = validateArray[x].itmDepends[curPos+1];
				rightSide = getReturnValue(validateArray[x].itmDepends[curPos+2]);

				// check the AND condition
				if(con == "and") {
					if(curFlag==true && rightSide==true) {
						curFlag=true;
					} else {
						curFlag=false;
					}
				}
				// check the OR condition
				if(con == "or") {
					if(curFlag==true || rightSide==true) {
						curFlag=true;
					} else {
						curFlag=false;
					}
				}
				curPos = curPos + 2;
			}
			// essentially we ignore the item if we end with a false result.
			if(curFlag==false) {
				validateArray[x].ignore = true;
			}
		}
	}

	// --------------------------------------------------------------
	// now we need to verify the logic for each of the primary fields
	// to determine the overall form boolean state.
	// --------------------------------------------------------------
	var validateFlag = true;

	for(cur=0; cur<validateArray.length; cur++) {

		if(validateArray[cur].mustValidate==false) continue;
		if(validateArray[cur].ignore      ==true)  continue;

		if(validateFlag!=false) {
			validateFlag = validateArray[cur].returnValue;
		}
	}
	
	// --------------------------------------------------------------
	// we need to display (possibly) the alert box with junk in it
	// --------------------------------------------------------------
	if(validateFlag == false) {
		//errorMessage = "The following fields contain invalid data:-";
		errorMessage = msg;

		for(bd=0; bd<validateArray.length; bd++) {
			if(validateArray[bd].returnValue==false && validateArray[bd].mustValidate==true &&validateArray[bd].ignore==false) {
				var obj = validateArray[bd];
				if (typeof obj.itmDescription != "string") {
					errorMessage = errorMessage + '\r\n' + obj.itmDescription.value;
				} else {
					errorMessage = errorMessage + '\r\n' + obj.itmDescription;
				}
				if(checkType=="StopOnFirst") {
					alert(errorMessage);
					return false;
				}
			}
		}
		alert(errorMessage);
		return false;
	}
	return true;
}





// EOF


