function WizardNextValidateRequired(hdnMoveNextID)
{
	var inputs = document.getElementsByTagName("input");
	var failed = false;

	for (var i=0; i < inputs.length; i++) 
	{	
		if (inputs[i].getAttribute("type") == "hidden") {
			if (inputs[i].name.indexOf(hdnMoveNextID) > -1){
				if (inputs[i].value.length == 0) {			
					inputs[i].value = '1';
					break;
				}
			}
		}
	}
	return validateRequired('wizard next button error', 'wizard next button error');
}

/**
 * Wrapper function for all validations
 */
function validateRequiredFields(MsgIncorrectDataType, MsgRequiredFields)
{
	var failOccurred1 = false;
	var failOccurred2 = false;
	failOccurred1 = validateCheckboxRadioButtons();
	failOccurred2 = validateRequiredFieldsGeneric();
	if (!failOccurred1 && !failOccurred2)
	{
		if (! validateFieldDataTypes())
		{
			alert(MsgIncorrectDataType);
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		alert(MsgRequiredFields);
		return false;
	}
}
/**
 * generic required fields function (uses the "required" attribute)
 */
function validateRequiredFieldsGeneric(MsgRequiredFieldEnterValue) {
	var tagNames = new Array("input", "select", "textarea");
	var failOccurred = false;
	
	for (var i=0; i<tagNames.length; i++) {
		var objects = document.getElementsByTagName(tagNames[i]);
		for (var k=0; k<objects.length; k++) {
			var pass = true;
			var obj  = objects[k];

            var requiredAttribute = false;
            requiredAttribute = obj.getAttribute("required");
            
            if(requiredAttribute == null)
                requiredAttribute = 'false';
            
			if (requiredAttribute == 'true')
			{
			    var tab;
			    if (obj.tagName.toLowerCase() == "textarea" || obj.getAttribute("type") == "text" || obj.getAttribute("type") == "password" || obj.getAttribute("type") == "file") {
				    // text and password
				    if (obj.value == ""){
					    pass = false;
				    }
			    } else if ((obj.tagName.toLowerCase() == "select") && ((obj.getAttribute("multiple") != null) & (obj.getAttribute("multiple") != false) ) ) {
			        if(obj.getAttribute("multiple").toString() == "true" || obj.getAttribute("multiple").toString() == "multiple")
			        {
					    
					    //multi-select
					    var options = obj.options;
				        if (options.length == 0) {
					        pass = false;
				        }
				    }
			    } else if ((obj.tagName.toLowerCase() == "select") && ((obj.getAttribute("multiple") == null) || (obj.getAttribute("multiple") == false))) {
			        var options = obj.options;
			        // rls control check - BEGIN
			        if ((obj.name.indexOf("drpVisibleType") > 0)|| (obj.name.indexOf("drpModifyType") > 0))
			        {
						if (options[obj.selectedIndex].value == 2) 
						{
							var selects = document.getElementsByTagName("select");
							for (var indRls = 0; indRls < selects.length; indRls++)
							{
								var dependant = selects[indRls];
								if (((obj.name.indexOf("drpVisibleType") > 0) && (dependant.name.indexOf("drpVisibleGroup") > 0)) ||
									((obj.name.indexOf("drpModifyType") > 0) && (dependant.name.indexOf("drpModifyGroup") > 0)))
								{
									if (dependant.selectedIndex <= 0)
									{
											dependant.className = "yellow";
											if(dependant.parentNode != null)
											{
												dependant.parentNode.oldClassName = dependant.parentNode.className;
												dependant.parentNode.className = dependant.parentNode.className + "yellow";
											}
											dependant.title = MsgRequiredFieldEnterValue;
											failOccurred = true;
									}
									else{
											// if pass, clear background color and title
											if (dependant.className == "yellow")
											{
												dependant.className = "";
												if(dependant.parentNode != null)
												{
													dependant.parentNode.className = dependant.parentNode.oldClassName;
												}
												
											}
											obj.title = "";
									}
								}
							}
						}
			        }
			        // rls control check - END
			        if (obj.selectedIndex == -1) {
				        pass = false;
			        } else if (options[obj.selectedIndex].text == "") {
				        pass = false;
			        }
			    } else if ((obj.getAttribute("type") == "checkbox")) {
                    if(!obj.checked)
                    {
                        pass = false;
                    }
			    }
			    if (! pass) {
				    // if fail, color in yellow and give a title
				    //If Safari and the object is a select then set the parent class to be yellow.  Otherwise set the class to be yellow.
					if(obj.parentNode != null && obj.tagName.toLowerCase() == "select" && BrowserDetect.browser.toLowerCase() == "safari")
					{
						if(obj.parentNode.getAttribute("class") != null && obj.parentNode.getAttribute("class").indexOf("yellow") == -1)
						{
							obj.parentNode.setAttribute("oldClassName", obj.parentNode.getAttribute("class"));
						}
						obj.parentNode.setAttribute("class", "yellow");
					}
					else if(BrowserDetect.browser.toLowerCase() == "safari")
					{
						obj.setAttribute("class", "yellow");
					}
					else
					{
						obj.style.backgroundColor = "yellow";
					}
				    
				    obj.title = MsgRequiredFieldEnterValue;
				    failOccurred = true;
			    } else {
    			
				    // if pass, clear background color and title
				    if(obj.parentNode != null && obj.parentNode.className == "yellow" && obj.tagName.toLowerCase() == "select" && BrowserDetect.browser.toLowerCase() == "safari")
					{
						obj.parentNode.className = obj.parentNode.getAttribute("oldClassName");
					}
				    else if(obj.className == "yellow" && BrowserDetect.browser.toLowerCase() == "safari")
					{
						obj.className = "";
					}
					else if (obj.style.backgroundColor == "yellow")
				    {
					    obj.style.backgroundColor = "";
				    }

				    obj.title = "";
			    }
			}
		}
	}	
	
	return failOccurred;	
}

/*
 * check that fields are of the correct datatypes based on the datatype attribute) 
 */
function validateFieldDataTypes(MsgDecimalNumber, MsgIntegerNumber, MsgDateFormat, MsgTime, MsgMinute, MsgHour) {
	var inputs = document.getElementsByTagName("input");
	var failed = false;
	for (var i=0; i < inputs.length; i++) {
	
		if (inputs[i].className == "red") {
			inputs[i].className = "";
			inputs[i].title = "";
		}
	
		if (inputs[i].value.length == 0) {			
			continue;
		}
		if (inputs[i].getAttribute("type") != "text" && inputs[i].getAttribute("type") != "password") {
			continue;
		}
		switch (inputs[i].getAttribute("datatype"))
		{
			case "phone":
				if (! validatePhone(inputs[i])) failed = true;					
				break;
			case "decimal":
				if (isNaN(parseFloat(inputs[i].value)))
				{						
					inputs[i].className = "red";
					inputs[i].title = MsgDecimalNumber;
					failed = true;
				} else {
					// ensure field value is an int
					inputs[i].value = parseFloat(inputs[i].value);
				}
				break;
			case "int":
				if (isNaN(parseInt(inputs[i].value)))
				{						
					inputs[i].className = "red";
					inputs[i].title = MsgIntegerNumber;
					failed = true;
				} else {
					// ensure field value is an int
					inputs[i].value = parseInt(inputs[i].value);
				}
				break;

			case "date":
				var dateValue = null;
				dateValue = new Date(inputs[i].value);
				//alert(dateValue);
				if (!isDate((dateValue.getMonth() + 1) + "/" + dateValue.getDate() + "/" + dateValue.getFullYear()))
				{
					inputs[i].className = "red";
					inputs[i].title = MsgDateFormat;
					failed = true;
				}
				break;
			case "minute":
				var minuteInput = inputs[i].value;
				if (minuteInput < 0 || minuteInput > 59)
				{
					inputs[i].className = "red";
					inputs[i].title = MsgMinute;
					failed = true;
				}
				break;
			case "hour":
				var hourInput = inputs[i].value;
				if (hourInput < 1 || hourInput > 23) {
					inputs[i].className = "red";
					inputs[i].title = MsgHour;
					failed = true;
				}
				break;
		}		
	}
	
	if (failed) {		
		return false;
	}
	return true;
}
function validateCheckboxRadioButtons(msg_reuired_field)
{
	var failOccurred = false;
	var finishedItems = false;
	var counter = 0;
	var itemChecked = false;
	if(window.radioCheckboxButtonsRequired != null && window.radioCheckboxButtonsRequired != "undefined")
	{
		if(radioCheckboxButtonsRequired.length != null && radioCheckboxButtonsRequired.length != "undefined")
		{
			for(var i = 0; i < radioCheckboxButtonsRequired.length; i++)
			{
				finishedItems = false;
				counter = 0;
				itemChecked = false;
				chkContainerObject = document.getElementById(radioCheckboxButtonsRequired[i]);
				do
				{
					var chkObject = document.getElementById(radioCheckboxButtonsRequired[i] + "_" + counter);
					if(chkObject != null)
					{
						if(chkObject.checked == true)
						{
							itemChecked = true;
						}
					}
					else //we reached the end of the radio/checkbox items
					{
						if(itemChecked == false)
						{
							failOccurred = true;
							chkContainerObject.className = "yellow";
							chkContainerObject.title = msg_reuired_field;
						} 
						else
						{
							// if pass, clear background color and title
							if (chkContainerObject.className == "yellow")
							{
								chkContainerObject.className = "";
							}
							chkContainerObject.title = "";
						}
						finishedItems = true;
					}
					counter++;
				}while(finishedItems == false)
			}
		}
	}
	return failOccurred;
}

// ******* DATE VALIDATION ***********************

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) 
{
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	//alert(strDay);
	if (strDay.charAt(0)=="0" && strDay.length > 1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length > 1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
    //alert("Month: " + month + " | Day: " + day + " | Year: " + year);

	if (pos1==-1 || pos2==-1)
	{
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		return false
	}
	return true
}
//*************** DATE VALIDATION END **************************

//********************** PHONE VALIDATION START**********************
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 23;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object)
{
	phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object)
{
	phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e)
{
	if(e)
	{
		e = e
	}
	else
	{
		e = window.event
	}
	if(e.which)
	{
		var keycode = e.which
	}
	else
	{
		var keycode = e.keyCode
	}
	
	ParseForNumber1(object)
	if(keycode > 48)
	{
		ValidatePhone(object)
	}
}

function backspacerDOWN(object,e)
{
	if(e)
	{
		e = e
	}
	else
	{
		e = window.event
	}
	if(e.which)
	{
		var keycode = e.which
	}
	else
	{
		var keycode = e.keyCode
	}
	
	ParseForNumber2(object)
}

function GetCursorPosition(){

	var t1 = phonevalue1;
	var t2 = phonevalue2;
	var bool = false
	for (i=0; i<t1.length; i++)
	{
		if (t1.substring(i,1) != t2.substring(i,1))
		{
			if(!bool)
			{
				cursorposition = i
				bool = true
			}
		}
	}
}
function SetCursorPosition(pos)
{
	var obj = document.activeElement;
	if (obj)
	{
		var tr = obj.createTextRange();
		if (obj && tr)
		{
			tr.moveStart("character", pos);
			tr.collapse();
			tr.select();
			return true;
		}
	}
	return false;
}
function ValidatePhone(object)
{
	var p = phonevalue1

	p = p.replace(/[^\d]*/gi,"")

	if (p.length < 3)
	{
		object.value=p
	}
	else if(p.length==3)
	{
		pp=p;
		d4=p.indexOf('(')
		d5=p.indexOf(')')
		if(d4==-1)
		{
			pp="("+pp;
		}
		if(d5==-1){
			pp=pp+") ";
		}
		object.value = pp;
	}
	else if(p.length>3 && p.length < 7)
	{
		p ="(" + p;
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+") "

		p31=p.substring(4,l30);
		pp=p30+p31;

		object.value = pp;

	}
	else if(p.length >= 7 && p.length < 10)
	{
		p ="(" + p;
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+") "

		p31=p.substring(4,l30);
		pp=p30+p31;

		l40 = pp.length;
		p40 = pp.substring(0,9);
		p40 = p40 + phoneThreeFourSeperator

		p41 = pp.substring(9,l40);
		ppp = p40 + p41;

		object.value = ppp.substring(0, maxphonelength);
	}
	else if(p.length >= 11)
	{
		p ="(" + p;
		l30 = p.length;
		p30 = p.substring(0,4);
		p30 = p30+") "

		p31 = p.substring(4,l30);
		pp = p30+p31;

		l40 = pp.length;
		p40 = pp.substring(0,9);
		p40 = p40 + phoneThreeFourSeperator
		p41 = pp.substring(9,l40);
		ppp = p40 + p41;

		l50 = ppp.length;
		p50 = ppp.substring(0,14);
		p50 = p50 + " x"
		p51 = xreplace(xreplace(xreplace(ppp.substring(14,l50), " x", ""), " x", ""), "x", "");
		pppp = p50 + p51;

		object.value = pppp.substring(0, maxphonelength);
	}

	GetCursorPosition()

	if(cursorposition >= 0)
	{
		if (cursorposition == 0)
		{
			cursorposition = 2
		}
		else if (cursorposition <= 2)
		{
			cursorposition = cursorposition + 1
		}
		else if (cursorposition <= 5)
		{
			cursorposition = cursorposition + 2
		}
		else if (cursorposition == 6)
		{
			cursorposition = cursorposition + 2
		}
		else if (cursorposition == 7)
		{
			cursorposition = cursorposition + 4
			e1 = object.value.indexOf(')')
			e2 = object.value.indexOf(phoneThreeFourSeperator)
			if (e1>-1 && e2>-1)
			{
				if (e2 - e1 == 4)
				{
					cursorposition = cursorposition - 1
				}
			}
		}
		else if (cursorposition < 11)
		{
			cursorposition = cursorposition + 3
		}
		else if (cursorposition == 11)
		{
			cursorposition = cursorposition + 1
		}
		else if (cursorposition >= 12)
		{
			cursorposition = cursorposition
		}
	}
	if(BrowserDetect.browser.toLowerCase() == "safari")
	{
		setCaretToEnd(object);
	}
}
function setCaretToEnd (elm) {
setSelectedTextRange(elm, elm.value.length, elm.value.length);
}
function setSelectedTextRange(elm, selectionStart, selectionEnd) {
if (elm.setSelectionRange) {
elm.focus();
elm.setSelectionRange(selectionStart, selectionEnd);
}
else if (elm.createTextRange) {
var range = elm.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}

function ParseChar(sStr, sChar)
{
	if (sChar.length == null)
	{
		zChar = new Array(sChar);
	}
	else 
		zChar = sChar;

	for (i=0; i<zChar.length; i++)
	{
		sNewStr = "";

		var iStart = 0;
		var iEnd = sStr.indexOf(sChar[i]);

		while (iEnd != -1)
		{
			sNewStr += sStr.substring(iStart, iEnd);
			iStart = iEnd + 1;
			iEnd = sStr.indexOf(sChar[i], iStart);
		}
		sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

		sStr = sNewStr;
	}

	return sNewStr;
}

//*******************************************************************