/****************************************************************
*				    Password Validation Obj   					*
*																*
*	Purpose:													*
*																*															 	*
*   Requires:	Must be instantiated into an object.			*
*	Returns:	Creates a new instance of an object				*
*	Author:		jjp 											*
*																*
*****************************************************************/	
function PasswordValidationObj()
{
	//----------------------------------------------------------//
	//					Properties & Methods					//
	//----------------------------------------------------------//	
	
	//Private Properties
	var	oXMLDom		= new ActiveXObject("Microsoft.XMLDOM");
	var src;
	
	//Public Methods pointers
	this.Init		= fnInit;
	this.Validate	= ValidateData;
	
	//----------------------------------------------------------//
	//					Initialization Functions				//
	//----------------------------------------------------------//	
	
	//XMLDOM: The XML that describes the validation rules
	//source: The calling source, used to determine paths and 
	//		  caller specfifc functions.
	function fnInit(XMLDOM, source){
		oXMLDom = XMLDOM;
		src = source;}
	
	//----------------------------------------------------------//
	//					Validation Functions					//
	//----------------------------------------------------------//	
	
	// This function returns an error if the information is 
	// not correct and empty string otherwise.
	function ValidateData(userName, oldPass, newPass, ReNewPass){
		var svMsg = "";
		if(userName == "")
			svMsg = "Please enter a User Name.";
		else if(oldPass == "" && src != "Login.asp")
			svMsg = "Please enter your previous password.";
		else if(newPass == "")
			svMsg = "Please enter a new password.";
		else if(ReNewPass == "")
			svMsg = "Please confirm your new password.";						
		else if(newPass.toUpperCase() != ReNewPass.toUpperCase()){
			svMsg = "The passwords you typed do not match. Please try again.";						
			}
		//else if(oldPass.toUpperCase() == newPass.toUpperCase())
		//	svMsg = "There is no new information to save.";						
		
		//Make sure there is something to test against.
		if (oXMLDom == null)
			svMsg = "Unable to save password. Validation object not initialized.";
			
		if (svMsg != "")
			return svMsg;
			
		//Custom Validation
			var validationNode = oXMLDom.selectSingleNode("//Validation");
			var valMsg = false;
			
			//Error message box
			var butArray=Array();
						butArray[0]="OK";
			var msg = "New password requirements: <br/>";
							
			var validationNodes = oXMLDom.selectNodes("//Validation/Test");
			for(var i=0; i<validationNodes.length; i++){
				var type = validationNodes[i].getAttribute("Type");
				var val = validationNodes[i].getAttribute("Value");
				var passMod = validationNodes[i].getAttribute("PassIfMatch");
				var failMsg = validationNodes[i].getAttribute("FailMessage");
				
				//Make the Match
				//alert("Name: " + val + "\nGetMatch():"+GetMatch(type, newPass, val)+"\nPassifMath:"+passMod+"\n!XOR:"+ !(GetMatch(type, newPass, val) ^ passMod));
				if(!(GetMatch(type, newPass, val) ^ passMod))
					msg += "<li style='text-align:left;'>"+failMsg+"</li>";
				else{
					msg += "<li style='text-align:left;color:#cc0033;'>"+failMsg+"</li>";
					valMsg = true;
					}
				}	
				
			if(valMsg){
				//Adjust the height so that all the rules can be displayed.
				var height = 100 + validationNodes.length*24;
				var alertResult = RulesAlert(msg,"!",butArray, height);			
				svMsg = "Please re-type a valid password.";
				}	
		return svMsg;
	}
	
	//--------------------------------------//
	//	ValidateData() Support Functions	//
	//--------------------------------------//	
	
	///Returns a match value based on the type based in
	function GetMatch(type, newPass, value){
		if(value == "")
			return true;
			
		var passVal = false;
		switch(type){
			case "RegEx":
				passVal = MatchRegEx(newPass, value);
				break;
			case "XPath":
				passVal = MatchXPath(newPass, value);
				break;	
			default:	
				passVal = false;
			}
		return passVal;	
		}

	/// Function that compares the regular expression to the newPass
	function MatchRegEx(newPass, regEx){
		if(newPass.search(regEx) == -1){ 
			return false; 
			}
		return true; 
		}

	/// Function that uses the xpath passed in to compare the oXMLDom
	/// to the new password and returns true if the newpass 
	/// CASE INSENSITIVTLY equals the xpath value
	function MatchXPath(newPass, xpath){

		var attributeIndex = xpath.lastIndexOf("@");
		if(attributeIndex == -1)
			return false;

		var attribute = xpath.substring(attributeIndex+1, xpath.length);
		var path = xpath.substring(0, attributeIndex-1);
		var node = oXMLDom.selectSingleNode(path);
		if(node == null)
			return false;
		var att = node.getAttribute(attribute);
		if(att.toLowerCase() == newPass.toLowerCase())
			return true;
		return false;
		}

	//Function to display the complete list of rules to the user.
	function RulesAlert(msg,img,buttons, height){
		var arrayInfo = new Array();
			arrayInfo[0]= msg;    
			arrayInfo[1]= new Array();
		for (var i=0; i<buttons.length; i++)	
			arrayInfo[1][i]=buttons[i];		
		
		arrayInfo[2]= img; 
		var sFeatures = "dialogHeight:"+height+"px;dialogWidth:440px;status:no;help:no";
		var sPage = "../../General/Alert.asp";
		switch(src){
			case "ModifyPassword.asp":
				sPage = "../../General/Alert.asp";
				break;
			case "Login.asp":
				sPage = "General/Alert.asp";
				break;
			default:
				return;
				break;
			}
		var waitRef = window.showModalDialog(sPage,arrayInfo, sFeatures);
		return 	waitRef;	
		}
}