/**************************************************************************
PI Client Javascript validation file
Author: Erik Kins
Date:	8/8/2003
**************************************************************************/

var gField; //global for current Field that's being validated
var bAllowSubmit = true;	//global for whether the form should submit or not
var sError; //error string
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1; //boolean to detect IE or not
var ErrProfileBlank;  //profile page blank error
var ErrProfileEmail; //profile page email error
var ErrMsg;			//profile error msg
var gErrImg = "";		//global error img
/************************************************************************************
The main validation function for all global PI error/submit calls
************************************************************************************/
function validate(obj, validation){
    
    	
	if (!bAllowSubmit)
	{
		//don't validate other fields if we have a bad one.
		return false;
	}
	
	gField = eval('document.NavForm.' + obj);
	
	var args = validate.arguments;
	
	for (i=1; i < args.length; i++)
	{
		if (args[i].indexOf("(")==-1)
		{
			
		     //do the validation with the global object, knowing that the functions carry no params
		     if (!funcLookup[args[i]].doValidate()){
			     bAllowSubmit = false;
			      writeError("errordiv",sError);
			      imgError();
			      return false;						
		     }
		}
		else
		{
			//this is a function call with params...just call the proc directly
			bAllowSubmit = eval(args[i]);
			if (!bAllowSubmit)
			{
				writeError("errordiv", sError);
			    imgError();
				return false;
			}
		}
	}
	return true;
}

function validate2(obj, validation){	
	if (!bAllowSubmit)
	{
		//alert("test1");
		//don't validate other fields if we have a bad one.
		return false;
	}

	gField = eval('document.NavForm.' + obj);
	
	var args = validate2.arguments;
	
	for (i=1; i < args.length; i++)
	{
		if (args[i].indexOf("(")==-1)
		{
		     //do the validation with the global object, knowing that the functions carry no params
		     if (!funcLookup[args[i]].doValidate()){
			     bAllowSubmit = false;
			     writeError("errordiv",sError);
			     imgError2();
			     return false;						
		     }
		}
		else
		{
			//this is a function call with params...just call the proc directly
			bAllowSubmit = eval(args[i]);
			if (!bAllowSubmit)
			{
				writeError("errordiv", sError);
			    imgError2();
				return false;
			}
		}
	}
	return true;
}

function validateFreeTextRanking(qnum, range)
{	
	
	var x = 1;
	var holdqchoice
	while(x <= range)
	{	
		validate("document.NavForm." + qnum + "_" + x, 'hasAnswer');
		if (!bAllowSubmit)
			{
				if (gErrImg != "rankerror2.gif")
					{
						gErrImg = "singleerror.gif";
					}
								
				imgError();
				return false;
			}
			
		if (bAllowSubmit){
	
			holdqchoice = eval("document.NavForm." + qnum + "_" + x)
			bAllowSubmit = isInteger(holdqchoice.value);
			//alert(bAllowSubmit);
			if (!bAllowSubmit)
				{
					gErrImg = "numericerror.gif";
					imgError();
					return false;
				}
		}	
			
		if (bAllowSubmit){
 			validate("document.NavForm." + qnum + "_" + x, 'withinRange(1, ' +  range + ')');
 			
			if (!bAllowSubmit)
				{
					gErrImg = "rankerror.gif";
					imgError();
					return false;
				}
		}		
		x++;
	}
	
			

	var i = 1;

	
	while (i <= range)
	{
		var j = i + 1;
		holdqchoice = eval("document.NavForm." + qnum + "_" + i);
		var answer = holdqchoice.value;
		while (j > i && j <= range)
		{
			holdqchoice = eval("document.NavForm." + qnum + "_" + j);
			
			if (answer == holdqchoice.value)
			{
				gErrImg = "rankerror.gif";
				bAllowSubmit=false;
				imgError();
				return false;
			}
			j++;
		}		
		i++;
	}
	
	return true;
}

function validateFreeTextRanking2(qnum,NoBox,range)
{	
	
	var x = 1;
	var y = 0;
	var holdqchoice
	var ArrBoxes = new Array(NoBox); 
	
	while(x <= NoBox)
	{
		bAllowSubmit=true;
		validate("document.NavForm." + qnum + "_" + x, 'hasAnswer');
		if (bAllowSubmit)
			{
				ArrBoxes[y] = "document.NavForm." + qnum + "_" + x;
				y++;
			}
			x++;
	}
	
	bAllowSubmit=true;

	if (y!=range)
	{
		bAllowSubmit=false;
		return false;
	}
	
	
	x = 0;	
	while(x < range)
	{	
			if (bAllowSubmit){
				
				holdqchoice = eval(ArrBoxes[x])
				bAllowSubmit = isInteger(holdqchoice.value);
				//alert(bAllowSubmit);
				if (!bAllowSubmit)
					{
						gErrImg = "numericerror.gif";
						imgError();
						return false;
					}
			}	
				
			if (bAllowSubmit){
 				validate(ArrBoxes[x], 'withinRange(1, ' +  range + ')');
 				
				if (!bAllowSubmit)
					{
						gErrImg = "rangeerror.gif";
						imgError();
						return false;
					}
			}		
			x++;
	}
	
	var i = 0;	
	
	while (i < range)
	{
		var j = i + 1;
		holdqchoice = eval(ArrBoxes[i]);
		var answer = holdqchoice.value;
		
		while (j > i && j < range)
		{
			holdqchoice = eval(ArrBoxes[j]);
			
			if (answer == holdqchoice.value)
			{
				gErrImg = "rankerror.gif";
				bAllowSubmit=false;
				imgError();
				return false;
			}
			j++;
		}		
		i++;
	}	
	
	return true;
}



//if an error div exists, this will write the canned error message to it
//(assuming the error div exists)

function writeError(d, msg)
{
	
	
	
	if(navigator.appName == "Netscape")
		{			
			var el = eval("document.getElementById('" + d + "')");
			
		    if (el != null)
		    {
				if (el.id =='undefined' )
				{
					alert(msg);
				}
				else
				{
					(el.innerHTML = msg);
				}
			}	
		}
	else
		{
			textTag=msg;
			if (document.all){
				var dobj = eval("document.all." + d);
				if (dobj)
					{
						dobj.innerHTML = textTag;
					}
				}

			else if (document.layers) {
				var docobj = eval("document.layers['" + d + "']");
				if (docobj)
					{
						obj = docobj.document.open();
	       					obj.write(textTag);
	        				obj.close();
					}
				}
			else
				{
					alert(msg);
				}
		}	
	
	return false;
}


//writes the correct div or layer for text-errors
function genErrorDiv()
{
	if(document.all)
	{
		document.write("<div id=errordiv>&nbsp;&nbsp;</div>");
	}
	else if (document.layers)
	{
		document.write("<layer id=errordiv>&nbsp;&nbsp;</layer>");
	}
}


//swaps images for error handling
function imgError()
{
	//is there an image out there that meets our requirements?
	
	var imgObj = null;
	if (gErrImg.length > 0)
		{
			document.NavForm.errormsg.src = "images/" + iprogID + "/" + gErrImg;
		}
	else
		{
			if (gField.length > 0)
			{
				//check if there's a global image for all errors		
				var fieldID = gField[0].id;
				var usloc = fieldID.indexOf("_");			
				if (usloc != -1)
				{
					var allarray = new Array();
					allarray = fieldID.split("_");
					var theall = eval("document.NavForm.img" + allarray[0] + "_all");
					if (String(theall) != "undefined")
					{
						//swap the spacer with the all image
						imgObj = eval("document.NavForm.img" + allarray[0] + "_all");
					}
					else
					{
						var theval = eval("document.NavForm.img" + fieldID + "error");
						//grab the switcheroo for the first object within the question array		
						if (String(theval) != "undefined")
						{
							imgObj = eval("img" + fieldID + "error");
						}
					}
				}
				
				
				/*	
				if (usloc != -1)
				}
					var allarray = new Array();		
					allarray = fieldID.split("_");			
					var theall = eval("document.NavForm.img" + allarray[0] + "_all");			
					if (String(theall) != "undefined")
					{
						//swap the spacer with the all image
						imgObj = eval("document.NavForm.img" + allarray[0] + "_all");
					}
					else
					{		
						var theval = eval("document.NavForm.img" + gField[0].id + "error");		
						//grab the switcheroo for the first object within the question array		
						if (String(theval) != "undefined")
						{						
							imgObj = eval("img" + gField[0].id + "error");
						}
					}		
				}		
				*/			
			}
			else
			{
				var theval = eval("document.NavForm.img" + gField.id + "error")
				if (String(theval) != "undefined")
				{		
					imgObj = eval("img" + gField.id + "error");
				}
			}
	
			if (imgObj != null)
			{		
				document.NavForm.errormsg.src = "images/" + iprogID + "/" + imgObj.src;
			}
			else
			{
				document.NavForm.errormsg.src = "images/" + iprogID + "/singleerror.gif";
			}
		}
}

function imgError2()
{
	//is there an image out there that meets our requirements?
	
	var imgObj = null;
	if (gErrImg.length > 0)
		{
			document.NavForm.errormsg.src = "images/" + iprogID + "/" + gErrImg;
		}
	else
		{
			if (gField.length > 0)
			{
				//check if there's a global image for all errors		
				var fieldID = gField[0].id;
				var usloc = fieldID.indexOf("_");			
				if (usloc != -1)
				{
					var allarray = new Array();
					allarray = fieldID.split("_");
					var theall = eval("document.NavForm.img" + allarray[0] + "_all");
					if (String(theall) != "undefined")
					{
						//swap the spacer with the all image
						imgObj = eval("document.NavForm.img" + allarray[0] + "_all");
					}
					else
					{
						var theval = eval("document.NavForm.img" + fieldID + "error");
						//grab the switcheroo for the first object within the question array		
						if (String(theval) != "undefined")
						{
							imgObj = eval("img" + fieldID + "error");
						}
					}
				}
				
				
				/*	
				if (usloc != -1)
				}
					var allarray = new Array();		
					allarray = fieldID.split("_");			
					var theall = eval("document.NavForm.img" + allarray[0] + "_all");			
					if (String(theall) != "undefined")
					{
						//swap the spacer with the all image
						imgObj = eval("document.NavForm.img" + allarray[0] + "_all");
					}
					else
					{		
						var theval = eval("document.NavForm.img" + gField[0].id + "error");		
						//grab the switcheroo for the first object within the question array		
						if (String(theval) != "undefined")
						{						
							imgObj = eval("img" + gField[0].id + "error");
						}
					}		
				}		
				*/			
			}
			else
			{
				var theval = eval("document.NavForm.img" + gField.id + "error")
				if (String(theval) != "undefined")
				{		
					imgObj = eval("img" + gField.id + "error");
				}
			}
	
			if (imgObj != null)
			{		
				document.NavForm.errormsg.src = "images/" + iprogID + "/" + imgObj.src;
			}
			else
			{
				document.NavForm.errormsg.src = "images/" + iprogID + "/singleerror2.gif";
			}
		}
}

/************************************************************************************
GLOBAL FUNCTIONS WITH NO PARAMETERS -- USE THE funcLookup array and reference
				       the gField value within the function
//each test is a single validation test.  If validation passes, return true, otherwise return false
************************************************************************************/



function hasAnswer()
{
	var testString;
	if (gField.length > 0)
	{
		//we have multiple controls with the same name (if any of them have an answer, return true)
		for(j=0; j < gField.length;j++)		
		{
			if (gField[j].type=="radio" || gField[j].type=="checkbox")
			{
				if (gField[j].checked)
				{
					return true;
				}
			}
			else
			{		
				testString = gField[j].value;
				if (testString != null || testString != ""){
					return true;
				}			
			}
		}
		sError="Please answer all questions before proceeding."
		return false;

	}
	else
	{
		//single control
		testString = gField.value;
	
		if (testString == null || testString == ""){
			sError="Please answer all questions before proceeding."
			return false;
		}
		return true;
	}
}


function hasAnswerDropDown()
{
	var testString;
	
	testString = gField.value;
	
		if (testString == "0")
		{
			sError="Please make a selection from the dropdown before proceeding.";
			return false;
		}
		else
		{
			return true;
		}
}


function message()
{
	alert(gField.name + " = " + gField.value);
}



function percent100()
{
	if (isNumber(gField.value) && gField.value!="")
	{	
	  //now check for numeric value less than 100%
	  if (gField.value <= 100 && gField.value >= 0 && gField.value != "")
	  {
		return true;
	  }
	  else
	  {
		sError = "Answer must be between 0% and 100%.";
	  }
	}
	else
	{
		sError = "Answer must be numeric and less-than or equal-to 100%"
	}
	return false; 
}

function EqualPercent100()
{
	if (isNumber(gField.value) && gField.value!="")
	{	
	  //now check for numeric value equals 100%
	  if (gField.value == "100" && gField.value != "")
	  {
		return true;
	  }
	  else
	  {
		sError = "Answer must equal 100%.";
		gErrImg = "sumerror.gif";
		return false; 
	  }
	}
	else
	{
		return false;
	}
}



function isNumber(inputVal)
{ 
	oneDecimal = false;
	inputStr = inputVal.toString();
	for (var n = 0; n < inputStr.length; n++)
	{
		var oneChar = inputStr.charAt(n);
		if (n == 0 && oneChar == "-")
		{
			continue;
		}

		if (oneChar == "." && !oneDecimal)
		{
			oneDecimal = true;
			continue;
		}

		if (oneChar < "0" || oneChar > "9")
		{
			return false;
		}
	}
	return true;
}
function isInteger(inputVal)
{ 
	oneDecimal = false;
	inputStr = inputVal.toString();
	for (var n = 0; n < inputStr.length; n++)
	{
		var oneChar = inputStr.charAt(n);
		if (n == 0 && oneChar == "-")
		{
			continue;
		}

		if (oneChar == "." && !oneDecimal)
		{
			return false;
		}

		if (oneChar < "0" || oneChar > "9")
		{
			return false;
		}
	}
	return true;
}
//states array
var USStates = new Array(51);
USStates["AL"] = "ALABAMA";
USStates["AK"] = "ALASKA";
USStates["AZ"] = "ARIZONA";
USStates["AR"] = "ARKANSAS";
USStates["CA"] = "CALIFORNIA";
USStates["CO"] = "COLORADO";
USStates["CT"] = "CONNECTICUT";
USStates["DE"] = "DELAWARE";
USStates["DC"] = "DISTRICT OF COLUMBIA";
USStates["FL"] = "FLORIDA";
USStates["GA"] = "GEORGIA";
USStates["HI"] = "HAWAII";
USStates["ID"] = "IDAHO";
USStates["IL"] = "ILLINOIS";
USStates["IN"] = "INDIANA";
USStates["IA"] = "IOWA";
USStates["KS"] = "KANSAS";
USStates["KY"] = "KENTUCKY";
USStates["LA"] = "LOUISIANA";
USStates["ME"] = "MAINE";
USStates["MD"] = "MARYLAND";
USStates["MA"] = "MASSACHUSETTS";
USStates["MI"] = "MICHIGAN";
USStates["MN"] = "MINNESOTA";
USStates["MS"] = "MISSISSIPPI";
USStates["MO"] = "MISSOURI";
USStates["MT"] = "MONTANA"
USStates["NE"] = "NEBRASKA"
USStates["NV"] = "NEVADA"
USStates["NH"] = "NEW HAMPSHIRE"
USStates["NJ"] = "NEW JERSEY"
USStates["NM"] = "NEW MEXICO"
USStates["NY"] = "NEW YORK"
USStates["NC"] = "NORTH CAROLINA"
USStates["ND"] = "NORTH DAKOTA"
USStates["OH"] = "OHIO"
USStates["OK"] = "OKLAHOMA"
USStates["OR"] = "OREGON"
USStates["PA"] = "PENNSYLVANIA"
USStates["RI"] = "RHODE ISLAND"
USStates["SC"] = "SOUTH CAROLINA"
USStates["SD"] = "SOUTH DAKOTA"
USStates["TN"] = "TENNESSEE"
USStates["TX"] = "TEXAS"
USStates["UT"] = "UTAH"
USStates["VT"] = "VERMONT"
USStates["VA"] = "VIRGINIA"
USStates["WA"] = "WASHINGTON"
USStates["WV"] = "WEST VIRGINIA"
USStates["WI"] = "WISCONSIN"
USStates["WY"] = "WYOMING"


// input value is a U.S. state abbreviation; set entered value to all uppercase
// also set companion field (NAME="<xxx>_expand") to full state name
function isUSState() {
	var inputStr = gField.value.toUpperCase()
	if (inputStr.length > 0 && USStates[inputStr] == null) {
		var msg = ""
		var firstChar = inputStr.charAt(0)
		if (firstChar == "A") {
			msg += "\n(Alabama = AL; Alaska = AK; Arizona = AZ; Arkansas = AR)"
		}
		if (firstChar == "D") {
			msg += "\n(Delaware = DE; District of Columbia = DC)"
		}
		if (firstChar == "I") {
			msg += "\n(Idaho = ID; Illinois = IL; Indiana = IN; Iowa = IA)"
		}
		if (firstChar == "M") {
			msg += "\n(Maine = ME; Maryland = MD; Massachusetts = MA; Michigan = MI; Minnesota = MN; Mississippi = MS; Missouri = MO; Montana = MT)"
		}
		if (firstChar == "N") {
			msg += "\n(Nebraska = NE; Nevada = NV)"
		}
		sError = "Check the spelling of the state abbreviation." + msg;		
		return false;
	}
	gField.value = inputStr
	return true
}



function ZIP() {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (gField.value.length!=5 && gField.value.length!=10) {
		sError = "Please enter a 5 digit or 5 digit+4 zip code.";
		return false;
	}

	for (var i=0; i < gField.value.length; i++) 
	{
		temp = "" + gField.value.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			sError = "Invalid characters in your zip code.  Please try again.";
			return false;
		}
	
		if ((hyphencount > 1) || ((gField.value.length==10) && ""+ gField.value.charAt(5)!="-")) 
		{
			sError = "The hyphen character should be used with a properly formatted 5 digit+4 zip code, like '12345-6789'.";
			return false;
   		}
	}
	return true;
}



/************************************************************************************
GLOBAL FUNCTIONS WITH PARAMETERS -- DO NOT USER THE funcLookup array
************************************************************************************/
function withinRange(low, high)
{
	if (gField)
	{
		//make sure only values between low and high are allowed to be typed in
		if (gField.value >= low && gField.value <= high)
		{
			return true;
		}
		sError = "Answer must be between " + low + " and " + high + ".";
	}
	return false;
}


function limitedCheck(max)
{
	var checkCount = 0
	var totalCount = 0;
	if (gField.length > 0)
	{
		totalCount = gField.length;

		for (m=0; m < gField.length; m++)
		{
			if (gField[m].checked)
			{
				checkCount = checkCount + 1;
			}
		}
	}

	if (checkCount <= max)
	{
		return true;
	}
	else
	{
		sError = "You can only select " + max + " of the " + totalCount + " checkboxes.";		
	}	
	return false;
}

function limitedCheckMin(min)
{
	var checkCount = 0
	var totalCount = 0;
	if (gField.length > 0)
	{
		totalCount = gField.length;

		for (m=0; m < gField.length; m++)
		{
			if (gField[m].checked)
			{
				checkCount = checkCount + 1;
			}
		}
	}

	if (checkCount < min)
	{
		sError = "You would have to select atleast " + min + " of the " + totalCount + " checkboxes.";		
	}
	else
	{
		return true;
	}	
	return false;
}

/************************************************************************************
OTHER FUNCTIONS -- non-global for use with onChange type events
		   not connected to the greater error/submit architecture
************************************************************************************/
function restrictRange(e, low, high)
{
	var charCode = (navigator.appName == "Netscape") ? e.charCodeAt(0) : e.charCodeAt(0)
	
	if (charCode - 48 < low || charCode - 48 > high)
	{
		bAllowSubmit=false;
		return false;
	}
	bAllowSubmit=true;
	return true;
}


function restrictNumeric(e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode

	//only allow 0-9 and period
	if ((charCode < 48 || charCode > 57) && charCode != 46)
	{
		bAllowSubmit=false;
		return false;
	}
	bAllowSubmit=true;
	return true;
}

function Sum(Qnum)
{
	var thesum =0;
	var qname;
	var tname;
	var lname; 
	var lck = ('Q' + Qnum + '_');
	tname = ('Total' + Qnum);
	var fieldTotal = eval('document.NavForm.' + tname);

	for (i=0;i < document.NavForm.length;i++) 
		{
			lname = document.NavForm.elements[i].name;
			if (lname.substring(0,3) == lck || lname.substring(0,4) == lck)
				{
					for (c=0;c<20;c++)
						{
							 qname =  'Q' + Qnum + '_' + c;
							//alert(document.NavForm.elements[i].name + ' = ' + qname);
							if (document.NavForm.elements[i].name == qname)
								{
									if (isNaN(parseFloat(document.NavForm.elements[i].value)))
										{
											thesum = thesum + 0;
										}
							else
										{
									thesum = thesum + parseFloat(document.NavForm.elements[i].value);
										}
								}
						}
				}
		}
	fieldTotal.value=thesum;	
					
}


/************************************************************************************
FUNCTION HANDLER -- place all custom functions above, then add to the funcLookup array
		    if functions have no parameters and reference the gField object.
************************************************************************************/

//Function lookup caller
function initFunction(func)
{
	this.doValidate = func;	
}

//validation function array
//each time a new function is added, a new bound should be added to accommodate
var funcLookup;
funcLookup = new Array();
funcLookup["hasAnswer"] = new initFunction(hasAnswer);
funcLookup["message"] = new initFunction(message);
funcLookup["percent100"] = new initFunction(percent100);
funcLookup["EqualPercent100"] = new initFunction(EqualPercent100);
funcLookup["isUSState"] = new initFunction(isUSState);
funcLookup["ZIP"] = new initFunction(ZIP);
funcLookup["hasAnswerDropDown"] = new initFunction(hasAnswerDropDown);
//funcLookup["validateFreeTextRanking"] = new initFunction(validateFreeTextRanking);



/************************************************************************************
FLASH HANDLER
************************************************************************************/

// Handle all the the FSCommand messages in a Flash movie
function piflash_DoFSCommand(command, args) {
  //var piflashObj = InternetExplorer ? forms[0].piflash : document.forms[0].piflash;
  var piflashObj = document.forms[0].piflash;
  //
  // Place your code here...
  //
	if (args != null || args != "")
	{
		//var thevar = eval("NavForm." + command);
		//thevar.value = args;
		if (command == "eval")
		{
		 //let's just evaluate the args
		 eval(args);
		}
		else
		{
			//let's intelligently set the form value of command to whatever args is
			setVal(command, args);
		}
		
		//alert(command + " = " + args);
	}
//alert("args = " + args);	
}
// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCR'+'IPT LANGUAGE=VBScr'+'ipt\>  \n' );
	document.write('on error resume next \n');
	document.write('Sub piflash_FSCommand(ByVal command, ByVal args) \n');
	document.write('  call piflash_DoFSCommand(command, args) \n');
	document.write(' end sub \n');
	document.write('</SCR'+'IPT>');
}

// Handle all the the FSCommand messages in a Flash movie
function piflash_validate(command, args) {
  //var piflashObj = InternetExplorer ? forms[0].piflash : document.forms[0].piflash;
  var piflashObj = document.forms[0].piflash;
  //
  // Place your code here...
  //
	if (args != null || args != "")
	{
		//var thevar = eval("NavForm." + command);
		//thevar.value = args;
		if (command == "eval")
		{
		 //let's just evaluate the args
		 eval(args);
		}
		else
		{
			//let's intelligently set the form value of command to whatever args is
			setVal(command, args);
		}
		
		//alert(command + " = " + args);
	}
//alert("args = " + args);	
}
// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCR'+'IPT LANGUAGE=VBScr'+'ipt\>  \n' );
	document.write('on error resume next \n');
	document.write('Sub piflash_validate(ByVal command, ByVal args) \n');
	document.write('  call piflash_validate(command, args) \n');
	document.write(' end sub \n');
	document.write('</SCR'+'IPT>');
}

function passFlash(varval, textval)
{
	window.document.piflash.SetVariable(varval, textval);
}

function isblank(s)
	{
		for(var i = 0; i < s.length; i++)
			{
				var c = s.charAt(i);
				if((c != ' ') && (c != '\n') && (c != '\t')) return false;
			}
		return true;
	}

function setVal(formelmt, val)
{
    if (formelmt != null && val != null && document.getElementById(formelmt) != null)
    {
	    if (navigator.userAgent.indexOf("MSIE") == -1)
	    {
		    eval("document.forms['NavForm'].elements['" + formelmt + "'].value='" + val + "'");
	    }
	    else
	    {
		    eval("NavForm." + formelmt + ".value='" + val + "'");
	    }
	}
}
/************************************************************************************
PROFILE PAGE VAILIDATION
************************************************************************************/
/*
Added on 12/15/2010 by Parag to validate if the text entered is numeric or not
*/
function IsNumeric(s,d)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   var obj = eval("document.NavForm." + s);
   var fieldvalue = obj.value;
 
   for (i = 0; i < fieldvalue.length && IsNumber == true; i++) 
      { 
      Char = fieldvalue.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         	IsNumber = false;
		ErrProfileBlank += "\n" + d;
         }
      }
   return IsNumber;
   
}

function validateblank(s,d)
	{
		var obj = eval("document.NavForm." + s);
		var fieldvalue = obj.value;
		fieldvalue=fieldvalue.replace(/^\s*|\s*$/g,"");
		if((fieldvalue.length == 0) || (fieldvalue == " ") || (fieldvalue == '\n') || (fieldvalue == '\t') || (fieldvalue == null) || (fieldvalue == ""))
			{ 
				ErrProfileBlank += "\n" + d;
				
			}
	}
	
// Added by Nayse on 08/24/2011	to check for duplicate emails
function validateDupsEmail(uid,email)
{
	var obj = eval("document.NavForm." + uid);
	var uidValue = obj.value;
	uidValue = uidValue.replace(/^\s*|\s*$/g,"");
	
	obj = eval("document.NavForm." + email);
	var emailValue = obj.value;
	emailValue = emailValue.replace(/^\s*|\s*$/g,"");
	
	var xmlHttp = null;      
	xmlHttp = new XMLHttpRequest(); 
	var url = "http://data.physinteractive.com/PIFlashInterface/PIFlashInterface.aspx?PROC=CHKEMAILDUPS&UID=" + uidValue + "&Email=" + emailValue;
	xmlHttp.open( "GET", url, false );     
	xmlHttp.send( null );
	
	if (xmlHttp.responseText == "True")
	{
		ErrProfileEmail += "\nDuplicate";
	}
}


// Renamed by Nayse on 06/14/2011
function validateemailOld(s,d)
	{
		var obj = eval("document.NavForm." + s);
		var email = obj.value;
		if(email.length > 5)
		{
  		    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
			if(email.substring(email.length-4,email.length - 3) == "." || email.substring(email.length-3,email.length - 2) == "." || email.substring(email.length-5,email.length - 4) == ".")
			{
			}
			else
			{
				ErrProfileEmail += "You must enter a valid email address";
			}
			for (var b=0; b < email.length; b++)
			{
                var letter = email.charAt(b).toLowerCase();
    			if (validchars.indexOf(letter) != -1)
      				continue;
                ErrProfileEmail += "\n You must enter a valid email address";
                break;
			}
			if((email.search("@") < 0 || email.search(".") < 0) && ErrProfileEmail.length == 0 )
			{
				ErrProfileEmail += "\n You must enter a valid email address";
			} 
		}
		else
		{
			ErrProfileBlank += "\n" + d;
		}
	}


// Done by Nayse on 06/14/2011 to properly validate email addresses	
function validateemail(s,d)
	{
		var obj = eval("document.NavForm." + s);
		var email = obj.value;
		
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		if (email.match(emailPattern) == null)
		{
            ErrProfileEmail += "You must enter a valid email address";
            ErrProfileBlank += "\n" + d;
        }
	}
	

//PhoneNumber Field Tab
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
	found = true;
	else
	index++;
	return found;
}

function EnableField(obj,b)
	{
		var str = "document.forms.item('NavForm')."+ obj;
		var gField = eval(str);
		gField.disabled = b; 
	 }


function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
}

}


