
/* 
function 1: checkvalid_blank (String Fieldname , String Description);
	checks to see if fieldname contains a value or not

function 2: checkvalid_zip (String Fieldname , String Description);
	checks to see if value in fieldname is 5 digits or not

function 3: checkvalid_ssn (String Fieldname , String Description);	
	checks to see if value in fieldname is 9 digits or not

function 4: checkvalid_password_match (String Fieldname1 , String Fieldname2 , String Description);	
	checks to see if two password fields have the same value
	returns true if yes or inserts description in fieldlist if false

function 5: checkvalid_ndigits (String Fieldname , Mixed NumberOfDigits , String Description);
	For checking a specified number of digits:
	return true if the NumberOfDigits is assigned to a integer value and the field contains the specified number of digits.
	
	For checking at least one digit (or digits only):
	return true if the NumberOfDigits is assigned to "any" and the field contains digits only.

function 6: checkvalid_email (String Fieldname , String Description);
	checks to see if value in fieldname is a valid email address or not
	
function 7: checkradio_value (String Fieldname , String SearchValue);	
	checks to see if the radio button value was selected or not (returns true or false)

function 8: checkselect_value (String Fieldname , String SearchValue);	
	checks to see if the select box value was selected or not (returns true or false)	
	
function 9: checkbox_selected (String Fieldname);	
	checks to see if the specified checkbox was checked or not (returns true or false)
	
function 10: checkvalid_digits_greaterthan (String Fieldname , Int Number , String Description);	
	checks to see if the fieldname value is a digit greater than Number passed in
	
function 11: checkvalid_digits_lessthan (String Fieldname , Int Number , String Description);
	checks to see if the fieldname value is a digit less than Number passed in	
			
function 12: checkvalid_digits_between (String Fieldname , Int Number1 , Int Number2 , String Description);	
	checks to see if the fieldname value is a digit between two specified Numbers passed in	(inclusive)	
	
function 13: checkradio_blank (String Fieldname , String Description); 
	checks for blank radio button arrays and sets the focus on the first element of the blank array.

function 14: checkselect_selected (String Fieldname, String Description);
	validates the first option (should be set to blank) is not selected for a select dropdown
	
*/

/*
	The following two variables are utilized in the validation functions below to ensure the validation 
	alert box doesn't grow so long the user can't fit it within the window.  It seems that 25 items is 
	a good amount for a user with an 800x600 screen resolution.
*/
var failedValFieldCounter = 0; // reset this to 0 at the beginning of your validation function
var maxValFields = 9999999; // set to 9999999 for legacy purposes.  Change in your validation function (probably 25)


function openWin(url,name,width,height,options){
var n = name;
var w = width;
var h = height;
var o = options;
n = (n==null)?"_blank":n;
w = (isNaN(w) || w==null)?700:w;
h = (isNaN(h) || h==null)?600:h;
o = (o==null)?"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1":(o.indexOf(",")!=0)?","+o:o;

var posX = Math.ceil((parseInt(screen.width) - w) / 2);
var posY = Math.ceil((parseInt(screen.height) - h) / 2);
aWindow = window.open(url,n,"width=" + w + ",height=" + h + ",left=" + posX + ",top=" + posY + o);
}


function setAvailableMethods()
{
        this.valid = true;
        this.fieldList = "";
        this.numericlist= "";
        this.focusField = null;

		this.get_value = get_value;
		this.checkvalid_money = checkvalid_money;
		this.checkvalid_date = checkvalid_date;
		this.checkvalid_phone = checkvalid_phone;
		this.check_blank = check_blank;
		this.check_value = check_value;
		this.checkvalid_blank = checkvalid_blank;
		this.checkvalid_email = checkvalid_email;
		this.checkvalid_zip = checkvalid_zip;
		this.checkvalid_ssn = checkvalid_ssn;
		this.checkvalid_password_match = checkvalid_password_match;
		this.checkvalid_3digits = checkvalid_3digits;
		this.checkvalid_4digits = checkvalid_4digits;
		this.checkvalid_digitsonly = checkvalid_digitsonly;
		this.checkvalid_digits_greaterthan = checkvalid_digits_greaterthan;
		this.checkvalid_digits_lessthan = checkvalid_digits_lessthan;
		this.checkvalid_digits_between = checkvalid_digits_between;
		this.checkradio_blank = checkradio_blank;
		this.checkradio_value = checkradio_value;
		this.checkselect_value = checkselect_value;
		this.checkbox_selected = checkbox_selected;
}

function checkvalid_money(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if(!formatMoney(f.elements[fieldname], description, false)) 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
}

function checkvalid_date(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if(!valDate(f.elements[fieldname], description, true)) // true means the alert is hidden
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
}	

function checkvalid_phone(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if(!valPhone(f.elements[fieldname], description, true)) // true means the alert is hidden
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
}

function get_value(fieldname)
{
	return f.elements[fieldname].value;
}	

function check_blank(fieldname)
{
	if(f.elements[fieldname].value=="") return (true);
	else return (false);
}	

function check_value(fieldname, value)
{
	if(f.elements[fieldname].value==value) return (true);
	else return (false);
}

function checkvalid_blank(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (f.elements[fieldname].value=="") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_blank()

	
function checkvalid_zip(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (!(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="")
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_zip()

function checkvalid_ssn(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (!(/(^\d{9}$)|(^\d{3}-\d{2}-\d{4}$)/.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_ssn()


function checkvalid_password_match(fieldname1 , fieldname2 , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if ((f.elements[fieldname1].value) != (f.elements[fieldname2].value)) 
	{
		if (valid) focusField = f.elements[fieldname1];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname1 + "does not match" + fieldname2 + "\n";
		}
	}
} // checkvalid_password_match()
	
	
function checkvalid_3digits(fieldname , description)
{
	checkvalid_ndigits(fieldname , "3" , description)
} // checkvalid_3digits()

function checkvalid_4digits(fieldname , description)
{
	checkvalid_ndigits(fieldname , "4" , description)
} // checkvalid_4digits()
	
function checkvalid_digitsonly(fieldname , description)
{
	checkvalid_ndigits(fieldname , "any" , description)
} // checkvalid_digitsonly()

function checkvalid_lettersonly(fieldname, description){
	if (failedValFieldCounter>=maxValFields) return;
	var let = eval("/[A-Z|a-z]*[ ]*/");
	alert(let.test(f.elements[fieldname].value));
	if(!let.test(f.elements[fieldname].value) || f.elements[fieldname].value==""){
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description){
			this.fieldList += description + "\n";
		}
		else{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_lettersonly()

	
function checkvalid_ndigits(fieldname , num_digits , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (isNaN(num_digits))
	{
		pattern = eval("/^\\d+$/");
	}
	else
	{
		pattern = eval("/^\\d{"+num_digits+"}$/");
	}
	if (!(pattern.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="")
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_ndigits()

function checkvalid_email(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (!validemail(f.elements[fieldname].value)) 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_email()
	
	

	
function checkradio_blank(fieldname, description)
{
	if (failedValFieldCounter>=maxValFields) return;
	for (i = 0; i < f.elements[fieldname].length; i++)
	{
		var arraychecked=0;
		if (f.elements[fieldname][i].checked)
		{
			arraychecked++;
			break;
		}
	}
	if (arraychecked==0)
	{
		if (valid) focusField = f.elements[fieldname][0];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}   
} // checkradio_blank()
	
	
function checkradio_value(fieldname , checkvalue)
{
	for (i = 0; i < f.elements[fieldname].length; i++)
	{
		if (f.elements[fieldname][i].checked)
		{
			if (f.elements[fieldname][i].value == checkvalue) 
			{
				return true;
			}
		}
	} 
	return false;	
} // checkradio_value()
		
		
function checkselect_value(fieldname , checkvalue)
{
	for (i = 0; i < f.elements[fieldname].length; i++)
	{
		if (f.elements[fieldname][i].selected)
		{
			if (f.elements[fieldname].options[i].value == checkvalue) 
			{
				return true;
			}
		}
	} 
	return false;	
} // checkselect_value()
	
function checkbox_selected(fieldname,description )
{
	if (failedValFieldCounter>=maxValFields) return;
	if (f.elements[fieldname].checked)
		return true;
	else
		return false;		
} // checkbox_selected()

var fpu = eval("/^(\\-)?(\\d)+(\\.)?(\\d)*$/");
function checkvalid_digits_greaterthan(fieldname , numvalue , description)
{
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) <= numvalue ||
			f.elements[fieldname].value == "") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_digits_greaterthan()

function checkvalid_digits_lessthan(fieldname , numvalue , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) >= numvalue ||
			f.elements[fieldname].value == "") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_digits_lessthan()

function checkvalid_digits_between(fieldname , numvalue1 , numvalue2 , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) < numvalue1 ||
			parseFloat(f.elements[fieldname].value) > numvalue2 || f.elements[fieldname].value == "") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_digits_between()

function isnumeric(n)
{
	nums = /[\\0-9]/;
	if (nums.test(n)) return true;
} // isnumeric()


function validemail(c)
{
	invalidChars = " /:,;"
	if (c == "")
	{
		return false;
	}
	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (c.indexOf(badChar,0) != -1)
		{
			return false;
		}
	}
	atPos = c.indexOf("@",1)
	if (atPos == -1) 
	{
		return false;
	}
	if (c.indexOf("@",atPos+1) != -1) 
	{
		return false;
	}
	periodPos = c.indexOf(".",atPos)
	if (periodPos == -1)
	{
		return false;
	}
	if (periodPos+3 > c.length)
	{
		return false;
	}
	return true;
} // validemail()
	
	
// Trim leading spaces from a string
String.prototype.ltrim = function () 
{
	var s=this;
	while (s.charAt(0)==" ") s=s.substring(1,s.length);
	return s;
} // String.prototype.ltrim()

// Trim trailing spaces from a string
String.prototype.rtrim = function () 
{
	var s=this;
	while (s.charAt(s.length-1)==" ") s=s.substring(0, s.length-1);
	return s;
} // String.prototype.rtrim()

// Return the rightmost n characters of a string
String.prototype.left = function (n)
{
	if (n < this.length) {
		return this.substring(0, n);
	} 
	else
	{
		return this;
	}
} // String.prototype.left()

// Return the rightmost n characters of a string
String.prototype.right = function (n) {
	if (n < this.length) 
	{
		return this.substring(this.length-n, this.length);
	} 
	else
	{
		return this;
	}
} // String.prototype.right()

// Test a string to see if it's entirely digits
String.prototype.isNumeric = function() 
{
	for (var i=0; i<this.length; ++i)
	{
		if ("0123456789".indexOf(this.charAt(i)) == -1) return false;
	}
	return true;
} // String.prototype.isNumeric()


function formatMoney(fld, show_error) 
{
	var amt=fld.value.ltrim().rtrim();
	var moneyChars="0123456789";
	var cents="00";
	var formatAmt="";
	var errorDesc="";

	// If there is a dollar sign, remove it.
	if (amt.charAt(0) == "$") amt = amt.substring(1,amt.length).ltrim();
	
	// Check for a decimal, and set a flag if there is one in the right place.
	if (amt.indexOf(".") != -1 ) 
	{
		if (amt.indexOf(".") == amt.lastIndexOf(".") && amt.indexOf(".") >= amt.length-3) {
		if (amt.indexOf(".") < amt.length-1) cents = amt.substring(amt.indexOf(".")+1, amt.length);
		if (cents.length==1) cents=cents+"0";
			amt = amt.substring(0,amt.indexOf("."));
		}
		else
			errorDesc = "Decimal placement error";
	}
	
	// Remove any commas.
	if (errorDesc.length == 0) 
	{
		while (amt.indexOf(",") != -1) 
		{
			amt = amt.substring(0,amt.indexOf(","))+amt.substring(amt.indexOf(",")+1, amt.length);
		}
	}
	
	// Now, check for any invalid characters.  There should not be any characters other than digits.  
	for (var i=0; i<amt.length && errorDesc.length==0; ++i) 
	{
		if (moneyChars.indexOf(amt.charAt(i)) == -1) 
		{
			errorDesc = "Dollar amount contains at least one illegal character.";
		}
	}
	
	if (errorDesc.length==0) 
	{
		// The dollar amount now contains only digits, so we can format them.
		if (amt.length == 0) amt="0";
		formatAmt = "."+cents;
		while (amt.length > 3) 
		{
			formatAmt = ","+amt.substring(amt.length-3,amt.length)+formatAmt;
			amt=amt.substring(0,amt.length-3);
		}
		formatAmt = "$"+amt+formatAmt;
		//    formatAmt = amt+""+formatAmt;
	} 
	else 
	{
		if (show_error) 
		{
			alert(errorDesc);
			fld.focus();
		}
		else
		{
			return false;
		}
	}
	fld.value = formatAmt;
	return true;
} // formatMoney()

function checkDate(dateVal)
{
	if (/^(\d{2})([\/\.-]?)(\d{2})(\2)(\d{2}|\d{4})$/.test(dateVal) ||
		/^(\d{1,2})([\/\.-])(\d{1,2})(\2)(\d{2}|\d{4})$/.test(dateVal)) 
	{
		this.month   = RegExp.$1 * 1;
		this.date    = RegExp.$3 * 1;
		this.year    = RegExp.$5 * 1;
		this.isValid = true;
	}
	else
	{
		this.isValid = false;
	} // end if-else
	return (this.isValid);
} // checkDate()

function parseDateOb2Str(dateOb)
//# returns date as a string
{
	var sMonth = String(dateOb.getMonth() + 1);
	var sDate = String(dateOb.getDate());
	var sYear = String(dateOb.getFullYear());
	
	if (sMonth.length == 1) sMonth = '0' + sMonth;
	if (sDate.length == 1) sDate = '0' + sDate;
	
	var newDate = sMonth + '/' + sDate + '/' + sYear;
	return   String(newDate);
} // parseDateOb2Str()


// Validate and format date. Begin and end dates must be javascript date objects
// e.g. <input type="text" name="date" onchange="valDate(this, 'Date of Foo')">
function valDate(f, desc, hideAlert) {
var dateFlag, errorDesc = '';
   if (2 > arguments.length || arguments.length > 6) return;        
   dateFlag = new checkDate(f.value);
   if (dateFlag.isValid){
      var M = dateFlag.month;
      var D = dateFlag.date;
      var Y = dateFlag.year;
      if (Y < 100)
         Y = (Y <= 50) ? Y + 2000 : Y + 1900;
      if (M < 1 || M > 12) {
         errorDesc = " Invalid month.";
      } else {
         var ld = (Y % 400 == 0 || Y % 100 != 0 && Y % 4 == 0) ? "9" : "8";
         var maxdays = "312" + ld + "31303130313130313031";
         if (D < 1 || D > maxdays.substr ((M - 1) * 2, 2))
            errorDesc = " Invalid date.";
      } // end if-else
      var nDate = new Date(Y,(parseInt(M)-1),D);
      if (errorDesc.length == 0) {
         // Date is valid and in range; format it.
         f.value =  parseDateOb2Str(nDate);
      } // end if
   } else {
      errorDesc = "You have entered an invalid date. Please re-enter the date in an accepted format, e.g. 01/01/2000 for January 1, 2000.";
   } // end if-else

             
   if (errorDesc.length > 0) {
      f.focus();
      f.select();
      if (!hideAlert) alert(desc + ": " + errorDesc);
      return (false);
   } // end if
	return (true);
} // end fun valDate
  
  
// Validate and format a Social Security Number
// e.g. <input type="text" name="ssn" onchange="valSSN(this, 'Social Security Number');">
// note: the SSN field should have a maxlength of 11 characters
function valSSN(f, desc)
{	
	var output = "";
	var letter = "";
	var output_counter = -1;
	var failed_validation = false;
	if (!f) alert("Could not find the specified field in valSSN");
	if (!f.value) return true;
	if (f.value.length<9)
	{
		alert (desc + ": " + " You have entered an invalid SSN.  The number must be at least 9 digits in length.  Please re-enter, formatting will be completed automatically.\\n");       
		return false;
	}
	for (var i=0; i<11; i++)
	{
		letter = f.value.charAt(i);
		if (letter.charCodeAt(0)>="0".charCodeAt(0) && letter.charCodeAt(0)<="9".charCodeAt(0))
		{
			output += letter;
			output_counter++;
			if (output_counter==2 || output_counter==5)
			{
				output += "-";
				output_counter++;
			}
			if (output_counter==10)
			{
				f.value = output;
				return true;
			}
		}
	}
	if (output_counter<11)
	{
		alert (desc + ": " + " You have entered an invalid SSN.  The number must be at least 9 digits in length.  Please re-enter, formatting will be completed automatically.\\n");       
		return false;
	}
} // valSSN()


// Validate and format phone number.
// e.g. <input type="text" name="phoneNum" onchange="valPhone(this, 'Foo Phone');">
function valPhone (f, desc, hideAlert) {
   if (/^\((\d{3})\)\s?(\d{3})[\s|\.|-](\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d{0,5}))?$/.test(f.value) ||
      /^(\d{3})[\s|\/|\.|-]?(\d{3})[\s|\.|-]?(\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d{0,5}))?$/.test(f.value)) {
      if (RegExp.$7) f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3+" ext."+RegExp.$7;
      else f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3;
   } else {
      f.focus ();
      f.select ();

	  var x10tionMesg;
	  if (desc == "Fax Number") {
		  x10tionMesg = "";
	  } else {
		  x10tionMesg = "\n If you wish to enter an extension (up to five digits), place it at the end of the main telephone number, and precede it with an 'x'.";
	  }	
	
      if (!hideAlert) alert (desc + ": " + " You have entered an invalid number.  The number must be 10 digits in length and contain the 3 digit area code.  Please re-enter, formatting will be completed automatically.\n " + x10tionMesg);       
      return (false);
   } // end if-else
   return (true);
} // end fun valPhone


function checkselect_selected(fieldname,description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (f.elements[fieldname].selectedIndex!==0)
	{
		return (true);
	}
	else
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}	
		return (false);		
	}
	
	return (false);		
} // checkselect_selected()

/**********Dynamic Required Script***********/

//set appendStar to true if the FI's required stars are to the right of the text
var appendStar = false;

function addStar( currentSectionName ) {
	$("."+currentSectionName).each(function(){
		var labelText = $(this).text();
		if (appendStar && labelText.substr(labelText.length-1,1)!="*") $(this).append("*");
		else if (labelText.substr(0,1)!="*") $(this).prepend("*");
	});
} // addStar()
function removeStar( currentSectionName ) {
	$("."+currentSectionName+".required").each(function(){
		var labelText = $(this).text();
		if( appendStar && labelText.substr(labelText.length-1,1)=="*" )
			$(this).text( labelText.substr(0,labelText.length-1) );
		else if (labelText.substr(0,1)=="*") $(this).text( labelText.substr(1) );
	});
} // removeStar()
function setRequired( name, enabled ) {
	if ( enabled ) {
		$("."+name).addClass("required");
		addStar( name );
	} else {
		removeStar( name );
		$("."+name).removeClass("required");
	}
} // setRequired()


//bind checkbox handlers
$("input\[@type='checkbox'\].dynamicRequired").live( "click", function(){
	if( $("."+this.name).filter(".required").size() == 0 ) {
		$("."+this.name).addClass("required");
		addStar( this.name );
	}
	else {
		removeStar(this.name);
		$("."+this.name).removeClass("required");
	}
});
//bind radio handlers
$("input\[@type='radio'\].dynamicRequired").live( "click", function(){
	if( $(this).parent("label").filter(".dr_on").size() ) {
		setRequired( this.name, false );
		setRequired( this.name, true );
	}
	else if( $(this).parent("label").filter(".dr_multchoice").size() ) {
		setRequired( this.name, false );
		if (this.id)setRequired( this.id, true );
	}
	else {
		setRequired( this.name, false );
	}
});
//bind select dropdown handlers
$("select.dynamicRequired").live( "change", function(){
	parentVal = $(this).val();
	parentName = this.name;
	if( parentVal == $($(this).children(".dr_on")).val() ) {
	 	setRequired( parentName, false );
		setRequired( parentName, true );
	}
	else {
		isExclusive = false;
		if( $(this).children(".dr_multchoice").size() ) {
			$($(this).children(".dr_multchoice")).each(function(i) {
				if( parentVal == $(this).val() ) {
				 	setRequired( parentName, false );
					setRequired( this.id, true );
					isExclusive = true;
				}
			});
		}
		if( !isExclusive )
			setRequired( parentName, false );
	}
});
/**********Dynamic Required Script***********/

