//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|
// AUTHOR:                  Jeff Kody (kodyj@nlg.com)
// CREATED:                 10/14/2003
// MODIFICATION HISTORY:
// PURPOSE:                 this file contains a set of validation objects
//                          and a message buffer.  
//-----------------------------------------------------------------------------------------|
//-----------------------------------------------------------------------------------------|
// $Header: /WebSites/library/javascript/validation/messagingobjects.js 21    5/15/08 2:01p Ktowers $


//-----------------------------------------------------------------------------------------|
//---constants
var INDEX_CONDITIONS_REGULAR_EXPRESSION = 0;
var INDEX_CONDITIONS_MESSAGE = 1;
var INDEX_CONDITIONS_LEVEL = 2;

var ROWINDEX_POS_ALPHA_ONLY = 0;	//alpha only if exists
var ROWINDEX_POS_ZIPCODE = 1;
var ROWINDEX_POS_EMAIL = 2;
var ROWINDEX_POS_VALUEREQUIRED = 3;
var ROWINDEX_POS_VALIDDATE = 4;
var ROWINDEX_POS_ADDRESS = 5;
var ROWINDEX_POS_NUMERICONLY = 6;
var ROWINDEX_POS_CITY = 7;
var ROWINDEX_POS_THREEDIGITS = 8;
var ROWINDEX_POS_FOURDIGITS = 9;
var ROWINDEX_POS_VACNAME = 10;
var ROWINDEX_POS_CRUISENAME = 11;
var ROWINDEX_POS_NUMBERICONLY_IFEXISTS = 12;
var ROWINDEX_POS_VALID_BOOKNUM = 13;

var ROWINDEX_NEG_VALUEREQUIRED = 0;
var ROWINDEX_NEG_ALPHA_ONLY = 1;
var ROWINDEX_NEG_VALIDDATE = 2;
var ROWINDEX_NEG_ADDRESS = 3;
var ROWINDEX_NEG_INVALIDNUMBERS = 4;
var ROWINDEX_NEG_INVALIDZIPCODE = 5;
var ROWINDEX_NEG_INVALIDEMAIL = 6;
var ROWINDEX_NEG_INVALIDSPECIALCHARS = 7;
var ROWINDEX_NEG_PHONENONNUMERIC = 8;
var ROWINDEX_NEG_INVALIDGENERIC = 9;
var ROWINDEX_NEG_FIRSTCONTAINSHYPHEN = 10;
var ROWINDEX_NEG_LASTCONTAINSHYPHEN = 11;
var ROWINDEX_NEG_INVALID_BOOKNUM = 12;


var MESSAGE_LEVEL_ERROR = 0;
var MESSAGE_LEVEL_WARNING = 1;

// /^[\d]{3}\-[\d]{8}$/, Valid Booking Number pattern validates xxx-xxxxxxxx
// ^[\d]{8}$|^[\d]{1}\-[\d]{8}$|^[\d]{3}\-[\d]{8}$ validates xxxxxxxx OR x-xxxxxxxx OR xxx-xxxxxxxx

var positiveConditions = new Array(new Array(/^[a-zA-Z]*$/, "Please enter character values only.", MESSAGE_LEVEL_WARNING), 
                                    new Array(/^[0-9]{5}$/, "Zip Code", MESSAGE_LEVEL_ERROR), 
                                    new Array(/^[\w\-\.]+@[\w\-\.]+\.[\w\-\.]{2,4}$/, "Email Address", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[\w\-\.]+$/, "Any Value", MESSAGE_LEVEL_ERROR),
                                    new Array(/^((([0][1,3,5,7,8]|[1][0,2])[\/]([0-2][0-9]|[3][0-1]))|(([0][4,6,9]|[1][1])[\/]([0-2][0-9]|[3][0]))|([0][2][\/][0-2][0-9]))[\/](19|20)[0-9][0-9]$/, "Valid Date", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[ a-zA-Z0-9\.\-]+$/, "Valid Address", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[\d]+$/, "Valid Number", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[a-zA-Z\-\. ]+$/, "Valid City", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[\d]{3}$/, "Three Digits", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[\d]{4}$/, "Four Digits", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[a-zA-Z ]+$/, "Vacation Booking Name", MESSAGE_LEVEL_ERROR),
									new Array(/^[a-zA-Z ]+$/, "Cruise Booking Name", MESSAGE_LEVEL_ERROR),
									new Array(/^[\d]*$/, "Valid Number if Exists", MESSAGE_LEVEL_ERROR),
                                    new Array(/^[\d]{8}$|^[\d]{1}\-[\d]{8}$|^[\d]{3}\-[\d]{8}$/, "Valid Booking Number", MESSAGE_LEVEL_ERROR)
									       );
                              
//---this array is responsible for all messaging that is displayed
//-  to the user.  to adjust the messages, overwrite the value
//-  on the calling page.             
var negativeConditions = new Array(new Array("CHECKLENGTH", "-fl-", MESSAGE_LEVEL_ERROR), 
                                    new Array(/[^a-zA-Z ]+/, "-fl-", MESSAGE_LEVEL_ERROR),
                                    new Array(/.+/, "-fl-" , MESSAGE_LEVEL_ERROR),
                                    new Array(/.+/, "The address entered contains invalid characters.<br />" , MESSAGE_LEVEL_ERROR),
                                    new Array(/[\d]/, "Please enter a valid home city.<br />", MESSAGE_LEVEL_ERROR),
                                    new Array(/.+/, "Please enter a valid zip code.<br />", MESSAGE_LEVEL_ERROR),
                                    new Array(/.+/, "-fl-", MESSAGE_LEVEL_ERROR),
                                    new Array(/[^\d\w]+/, "City name field contains at least one invalid character.<br />", MESSAGE_LEVEL_ERROR),
                                    new Array(/[^\d]+/, "Please enter a valid daytime phone number.<br />", MESSAGE_LEVEL_ERROR),
                                    new Array(/.?/, "-fl-", MESSAGE_LEVEL_ERROR),
									new Array(/[-]/, "<img src=/images_unique/icon_error_blt.gif border=0 align=absmiddle />&#160;First name cannot contain a -. Please simply leave a space<br />", MESSAGE_LEVEL_ERROR),
									new Array(/[-]/, "<img src=/images_unique/icon_error_blt.gif border=0 align=absmiddle />&#160;Last name cannot contain a -. Please simply leave a space<br />", MESSAGE_LEVEL_ERROR),
									new Array(/^([\d]{1}-?|[\d]{2}-?)|[a-zA-Z]$/, "-fl-", MESSAGE_LEVEL_ERROR)
									       )
// ^([\d]{1}-?|[\d]{2}-?)|[\d]{8}|[a-zA-Z]$ - Will validate a xxx-xxxxxxxx number but VO can have xxxxxxxx or x-xxxxxxxx 
//---constants
//-----------------------------------------------------------------------------------------|



//-----------------------------------------------------------------------------------------|
//---FieldValidator class and associated members
function FieldValidator(as_FieldName, as_MessageField, as_pcIndexes, as_ncIndexes, as_LabelName, as_Icon, as_ImageSRC) {
    this.fieldName = as_FieldName;
    this.messageField = as_MessageField;
    this.labelName = as_LabelName;
    this.el = eval('document.' + this.fieldName);
    this.pass = true;
    this.pcIndexes = as_pcIndexes;
    this.ncIndexes = as_ncIndexes;
    this.validate = validate;
    this.icon = as_Icon;
    this.iconImageSRC = as_ImageSRC;
}

function validate(ab_positive) {
    var ls_indexes;
    var li_arrIndex;
    var lv_arrWork;
    var li_errorCount = 0;
    
    if(ab_positive) {
        ls_indexes = this.pcIndexes;
        lv_arrWork = positiveConditions;
    } else {
        ls_indexes = this.ncIndexes;
        lv_arrWork = negativeConditions;
    }
        
    li_arrIndex = ls_indexes.split(",");

                
    for(i = 0; i < li_arrIndex.length; i++) 
    {
        var re = lv_arrWork[parseInt(eval(li_arrIndex[i]))][INDEX_CONDITIONS_REGULAR_EXPRESSION];
        var msg = lv_arrWork[parseInt(eval(li_arrIndex[i]))][INDEX_CONDITIONS_MESSAGE];
        var level = lv_arrWork[parseInt(eval(li_arrIndex[i]))][INDEX_CONDITIONS_LEVEL]
        
        //---this routine analyzes the input and popultes the message buffer.
        //-  if it's looking at a positive condidion, it calls itself recursively
        //-  and applys the negative condition filters.        
        if((ab_positive) && (re.test(this.el.value + ""))) 
        {
            this.pass = true;                 
        } 
        else if((!ab_positive) && (re == "CHECKLENGTH"))  
        {  
            if(this.el.value.length > 0) 
            {
                this.pass = true;
            } 
            else 
            {
                this.pass = false;
                mb.addMessage(this.fieldName, this.messageField, level, msg, this.labelName, this.icon, this.iconImageSRC);
                li_errorCount++;
            }
        } 
        else 
        {             
            this.pass = false;
            if(ab_positive) 
            {
                this.validate(false);
            } 
            else 
            {            	
                if(re.test(this.el.value + "")) 
                {                    
                    mb.addMessage(this.fieldName, this.messageField, level, msg, this.labelName, this.icon, this.iconImageSRC);
                    li_errorCount++;
                }
            }
        }
        // Commented out this logic since it needs regression testing wherever this file is used
        //(leaving the old code above as is and instead made a change to ROWINDEX_POS_VACNAME above to solve the hyphen issue)
        /*      
        if(ab_positive)
        {
            //positive conditions
            if (!re.test(this.el.value + ""))
            {	
        	this.pass = false;
        	mb.addMessage(this.fieldName, this.messageField, level, msg, this.labelName, this.icon, this.iconImageSRC);
        	li_errorCount++;            
            }
        	
            //process negative conditions when we're done processing the positive ones
            if(i == li_arrIndex.length-1)
        	this.validate(false);        
        }
        else
        {
            //negative conditions
            if(re == "CHECKLENGTH")
            {
        	if (this.el.value.length == 0)
        	{
                    this.pass = false;
		    mb.addMessage(this.fieldName, this.messageField, level, msg, this.labelName, this.icon, this.iconImageSRC);
                    li_errorCount++; 
                }
            }
            else if (re.test(this.el.value + "")) 
            {
                this.pass = false;
	        mb.addMessage(this.fieldName, this.messageField, level, msg, this.labelName, this.icon, this.iconImageSRC);
                li_errorCount++;        	
            }             
        } 
        */
    }   
     
    return (li_errorCount == 0)
}
//---FieldValidator class and associated members
//-----------------------------------------------------------------------------------------|


//-----------------------------------------------------------------------------------------|
//---Message class and associated members
function Message(as_FieldName, as_MessageField, ai_Level, as_Description, as_FieldLabel, as_Icon, as_ImageSRC) {
    this.field = as_FieldName;
    this.fieldLabel = as_FieldLabel;
    this.messageField = as_MessageField;
    this.messageLevel = ai_Level;    
    this.description = as_Description;
    this.icon = as_Icon;
    this.iconImageSRC = as_ImageSRC;
    this.getDescription = getDescription;    
}

function getDescription() {
    return this.description.replace("-fl-", this.fieldLabel);
}
//---Message class and associated members
//-----------------------------------------------------------------------------------------|


//-----------------------------------------------------------------------------------------|
//---MessageBuffer class and associated members
function MessageBuffer() {
    this.count = 0;
    this.messages = new Array();
    this.addMessage = popMessage;
    this.init = killMessages;
}

function popMessage(as_FieldName, as_MessageField, ai_MessageLevel, as_Description, as_fieldLabel, as_Icon, as_ImageSRC) {
    this.messages[this.count] = new Message(as_FieldName, as_MessageField, ai_MessageLevel, as_Description, as_fieldLabel, as_Icon, as_ImageSRC);
    this.count++;
}

function killMessages() {
    this.message = new Array();
    this.count = 0;
}
//---MessageBuffer class and associated members
//-----------------------------------------------------------------------------------------|



//-----------------------------------------------------------------------------------------|
//---Group class and associated members
function Group(as_GroupName, as_GroupError) {
    this.groupName = as_GroupName;
    this.errorMsg = as_GroupError;
    this.fieldCount = 0;
    this.failCount = 0;
    this.successCount = 0;
    this.fields = new Array();
    this.addField = addField;
    this.clearFields = clearFields;
    this.validateGroup = validateGroup;
    this.resetLabels = resetLabels;
}

function addField(as_fName, as_MessageFieldName, as_PC, as_NC, as_lName, as_Icon, as_ImageSRC) {
    this.fields[this.fieldCount] = new FieldValidator(as_fName, as_MessageFieldName, as_PC, as_NC, as_lName, as_Icon, as_ImageSRC);
    this.fieldCount++;
}

function clearFields() {
    this.fields = new Array();
    this.fieldCount = 0;
}

function validateGroup() {
    for(x = 0; x < this.fieldCount; x++)
        this.fields[x].validate(true);
}

function resetLabels() {
    if(document.getElementById) {
        for(x = 0; x < this.fieldCount; x++) {
            document.getElementById(this.fields[x].messageField).innerHTML = "";
            if(document.getElementById(this.fields[x].icon))
                document.getElementById(this.fields[x].icon).src = "/images_unique/blank.gif";
        }
    }                
}
//---Group class and associated members
//-----------------------------------------------------------------------------------------|



//-----------------------------------------------------------------------------------------|
//---Static Inline messaging functions
function showInlineMessage(el, msg, append) {
    var element = getElement(el);   
    var spacerEl = document.images[el + "_spc"];

    if (document.layers) {
        //---we have a netscape 4.x browser--do the layer thing   
        element.visibility = "show";
    } else {
        //---we have a real browser--do the real browser thing
        if(append)
            element.innerHTML += msg + "<br />";
        else
            element.innerHTML = msg;
    }   
}

function hideInlineMessage(el) {
    var element = getElement(el); 
    var spacerEl = document.images[el + "_spc"];
    
    if (document.layers) {
        element.visibility = "hide";
    } else {
        element.innerHTML = "";
    }
}        

function getElement(id) {
    var lyr;

	lyr = (document.getElementById)?document.getElementById(id):(document.all)?document.all[id]:(document.layers)?getLayerElement(id, document): null;
    
	if (lyr) 
        return lyr;
    else
        return null;
}        

function getLayerElement(id, doc) {
    if (document.layers) {
		var theLyr;
        
		for (var i = 0; i < doc.layers.length; i++) {
	  	    theLyr = doc.layers[i];
            
			if (theLyr.name == id) {
                return theLyr;
            } else if (theLyr.document.layers.length > 0) {
    	    	if ((theLyr = getLayerElement(id, theLyr.document)) != null)
    					return theLyr;
            }
        }
    }
        return null;
}


function dispatchMessages(buffer) {
    var ls_msg = "";

    if(document.getElementById) {
        //---real browser in the hiz-ouse!!!!!!!!!
        for(z = 0; z < buffer.count; z++)  {
            showInlineMessage(buffer.messages[z].messageField, buffer.messages[z].getDescription(), false);
            if(document.getElementById(buffer.messages[z].icon))
                document.getElementById(buffer.messages[z].icon).src = buffer.messages[z].iconImageSRC;
        }
    } else {
        //---NS suck is what we're dealing with here.
        alert(buffer.messages[0].getDescription().replace("<br />", ""));
    }
}        
//---Static Inline messaging functions
//-----------------------------------------------------------------------------------------|



//-----------------------------------------------------------------------------------------|
//---Other validation routines
function isChild(adt_AgeDate, adt_DepartDate) {
    var ldt_Age = new Date(adt_AgeDate);
    var ldt_Depart = new Date(adt_DepartDate);
    
    var ldt_Two = DateAdd(ldt_Depart, 0, 0, -2);
    var ldt_Eighteen = DateAdd(ldt_Depart, 0, 0, -18);

    if((ldt_Age > ldt_Eighteen) && (ldt_Age <= ldt_Two))
        return true;
    else
        return false;
}

function isAdult(adt_AgeDate, adt_DepartDate) {
    //DOB for passenger
    var ldt_Age = new Date(adt_AgeDate);
    
    //depart date
    var ldt_Depart = new Date(adt_DepartDate);
    
    //DOB for passenger age to be valid
    var ldt_Eighteen = DateAdd(ldt_Depart, 0, 0, -18);    
    
    if(ldt_Age <= ldt_Eighteen)
        return true;    
    else
        return false;
}

function isEmailValid(as_Email) {
	var lo_re = /^[\w\-\.]+@[\w\-\.]+\.[\w\-\.]{2,4}$/
		
	return lo_re.test(as_Email);
}
//---Other validation routines
//-----------------------------------------------------------------------------------------|

