var railp= new Image();
var welcomec= new Image();
var welcomef= new Image();
var welcomep= new Image();
var trainp= new Image();
var trainf= new Image();
var trainc= new Image();
var consentc= new Image();
var consentp= new Image();
var consentf= new Image();
var biof= new Image();
var bioc= new Image();
var biop= new Image();
var prec= new Image();
var pref= new Image();
var prep= new Image();
var postf= new Image();
var postc= new Image();
var postp= new Image();
var wrapupp = new Image();
var wrapupc = new Image();
var wrapupf = new Image();
var followupp = new Image();
var followupc = new Image();
var followupf = new Image();
var railf= new Image();
var railc= new Image();
railp.src = 'images/railp.gif';
welcomec.src = 'images/welcomec.gif';
welcomef.src = 'images/welcomef.gif';
welcomep.src = 'images/welcomep.gif';
trainp.src = 'images/trainp.gif';
trainf.src = 'images/trainf.gif';
trainc.src = 'images/trainc.gif';
consentc.src = 'images/consentc.gif';
consentp.src = 'images/consentp.gif';
consentf.src = 'images/consentf.gif';
biof.src = 'images/biof.gif';
bioc.src = 'images/bioc.gif';
biop.src = 'images/biop.gif';
prec.src = 'images/prec.gif';
pref.src = 'images/pref.gif';
prep.src = 'images/prep.gif';
postf.src = 'images/postf.gif';
postc.src = 'images/postc.gif';
postp.src = 'images/postp.gif';
wrapupp.src = 'images/wrapupp.gif';
wrapupc.src = 'images/wrapupc.gif';
wrapupf.src = 'images/wrapupf.gif';
followupp.src = 'images/followupp.gif';
followupc.src = 'images/followupc.gif';
followupf.src = 'images/followupf.gif';
railf.src = 'images/railf.gif';
railc.src = 'images/railc.gif';

function swap(label,imgname)
{
    document.images[label].src = eval(imgname + ".src");
}
function donothing()
{
    //DO NOTHING
}
var i=0;
function movenext()
{
	if (document.theform.OFFICEPHONEP1.value.length == 3 && i==0)
	{
		document.theform.OFFICEPHONEP2.focus();
		i++;
	}
	if (document.theform.OFFICEPHONEP2.value.length == 3 && i==1)
	{
		document.theform.OFFICEPHONEP3.focus();
		i=0;
	}
}

var i=0;
function movenextd()
{
	if (document.theform.MONTH.value.length == 2 && i==0)
	{
		document.theform.DAY.focus();
		i++;
	}
	if (document.theform.DAY.value.length == 2 && i==1)
	{
		document.theform.YEAR.focus();
		i=0;
	}
}
function countYear()
{
	if (document.theform.YEAR.value.length != 4)
	{
		alert('Please enter the year as 4 digits');
	}
}


function checkBioa(check)
{
if (document.theform.doacheck.value == 1) 
{
	if (document.theform.FIRSTNAME.value.length == 0) //Check First Name
	{
		alert('Please provide your First Name');
		return;
	}
	else
	{
		if (document.theform.LASTNAME.value.length == 0) // Check Last Name
		{
			alert('Please provide your Last Name');
			return;
		}
		else
		{
			if (document.theform.OFFICEPHONEP1.value.length != 3 || document.theform.OFFICEPHONEP2.value.length != 3 || document.theform.OFFICEPHONEP3.value.length != 4 ) // Check all of the Office Phone
			{
				alert('Please provide your Phone Number');
				return;
			}
			else
			{
				if (document.theform.EMAILADDRESS.value.length == 0) // Check the Email Address
				{
					alert('Please provide your Email Address');
					return;
				}
				else
				{
					if (document.theform.ZIPCODE.value.length != 5) // Check Zip Code
					{
						alert('Please fill out your Zip Code');
						return;
					}
					else
					{
						if (document.theform.GENDER.value == '~') // Check Gender
						{
							alert('Please choose your Gender');
							return;
						}
						else
						{							
							var themonth = document.theform.MONTH.value;
							var theday = document.theform.DAY.value;
							var theyear = document.theform.YEAR.value;
							if (isDate(themonth + '/' + theday + '/' + theyear)==false)
							{
								alert('Please provide your Birth Date in this format i.e. MM/DD/YYYY');
								return;
							}
							else
							{
								if (document.theform.ETHNICGROUP.value == '~') // Check Ethnic Group
								{
									alert('Please choose your Ethnic Group');
									return;
								}
								else
								{
									if (document.theform.SUBJECTID.value.length == 0) // User Name 
									{
										alert('Please provide your User Name');
										return;
									}
									else
									{
										if (document.theform.PASSWORD.value.length == 0) // Password
										{
											alert('Please provide your Password');
											return;
										}
										else
										{
											if (document.theform.SUBJECTID.value.indexOf(' ') != -1) 
											{
												alert('No spaces are allowed in Username');
												return;
											}
											else
											{
												if (document.theform.SUBJECTID.value.length < 7)
												{
													alert('Username needs to be greater than 7 characters');
													return;
												}
												else
												{
													if (document.theform.PASSWORD.value.length < 7) // Password
													{
														alert('Password needs to be greater than 7 characters');
														return;
													}
												}
												if (document.theform.retypepassword.value.length == 0) // Retyped Password
												{
													alert('Please reenter your Password');
													return;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
}

function CheckMultiSurv(thisform,totalcount)
{
	//var countthem = 0;
	//for (var i=0; i < thisform.length; i++)
	//{
		
		//for (var j=0; j < eval("thisform.q"+i+"[0].length"); j++)
		//{
		//	alert("thisform.q"+i+"["+j+"].length");
		//	if (eval("thisform.q"+i+"["+j+"].length"))
		//	{
		//		countthem++;	
		//	}
		//}
	//}
}

var whichone="";
function msgbox(whichone)
{
	switch(whichone)
	{
		case "lostemail":
			alert("You have just been sent an email that contains you Username and Password.");
			return;
		case "certificate":
			alert("The certificate that you requested has been\r generated and emailed to your address.");
			return;
		case "removeme":
			alert("You have been removed from our mailing list.");
			return;
		case "notifyme":
			alert("You have been added to our mailing list.");
			return;
		case "mem":
			alert("Do not allow IE to remember your password.");
			return;
		case "surveyisnumcheck":
			alert('All Textboxes are to have numbers not text!');
			return;
		case "surveycolumncheck":
			alert('You are missing one or more columns,\r You need to answer at least one subquestion\r for each column in each question!');
			return;
		case "emailsuper" :
			alert('The email notification has been sent.');
			return;
	}
}

var whichtext="";
function popup(whichtext)
{
	window.open("info/" + whichtext,"InfoWindow","menubar=no,height=500,width=550, resizable=no,scrollbars=yes");
}


function popupmod(URL)
{
        var width=screen.width-10;
        var height=screen.height-52;
        var agent = navigator.userAgent.toLowerCase();
        isNav4 = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
        isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
        var win = window.opener;
        if (isIE4 && (agent.indexOf("mac")==-1)){       
                window.open(URL,'Modules','width='+width+',height='+height+',top=0,left=0,scrollbars=no,toolbar=no,status=no,menubar=no,resizable=yes,location=no,dependent=no');
        }
        else{
                window.open(URL,'Modules','width='+width+',height='+height+',top=0,left=0,scrollbars=no,toolbar=no,status=no,menubar=no,resizable=yes,location=no,dependent=no');
        }
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function popupbegin(buttonclicked,URLLocation)
{
	var width = screen.width-10;
	var height = screen.height-80;
	var agent = navigator.userAgent.toLowerCase();
	isNav4 = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
	isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
	var win = window.opener;
	if (buttonclicked == "New Participants")
	{
		window.open('https://xtrain.cfs.purdue.edu/index1.asp?WCI=welcomeb','XTrain','width='+width+',height='+height+',top=0,left=0,scrollbars=yes,toolbar=no,status=yes,menubar=no,resizable=yes,location=no,dependent=no');
		//window.open('http://192.168.0.2/index1.asp?WCI=welcomeb','XTrain','width='+width+',height='+height+',top=0,left=0,scrollbars=yes,toolbar=no,status=yes,menubar=no,resizable=yes,location=no,dependent=no');		
	}
	else
	{
		window.open('https://xtrain.cfs.purdue.edu/index1.asp?WCI=reglogin','XTrain','width='+width+',height='+height+',top=0,left=0,scrollbars=yes,toolbar=no,status=yes,menubar=no,resizable=yes,location=no,dependent=no');
		//window.open('http://192.168.0.2/index1.asp?WCI=reglogin','XTrain','width='+width+',height='+height+',top=0,left=0,scrollbars=yes,toolbar=no,status=yes,menubar=no,resizable=yes,location=no,dependent=no');
	}

}

if(document.layers) document.captureEvents(Event.KEYPRESS);
document.onkeypress = keyhandler;
function checkenter()
{
	if (keyhandler == false)
	{
		return false;
	}
	else
	{
		return true;
	}
}
function keyhandler(e)
{
	if (document.layers)
	{
		Key = e.which
	}
	else
	{
		Key = window.event.keyCode;
	}
	if (Key == 13)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function checksurvey(thisform)
{
	var QNum=1;
	var QNumInnerLoop=0;
	var QNumNotAnsArray = new Array();
	var QNumAnsArray = new Array();
	var ans=0;
	var notans=0;
	var QNumAns=0;
	var QNumNotAns=0;
	for (var i=0;i<thisform.length;i++)
	{
		if (thisform.elements[i].type == "radio" && QNumInnerLoop !=  QNum)
		{
			for (var j=0;j < eval("thisform.q"+ QNum +".length"); j++)
			{
				QNumInnerLoop = QNum; //only advance Question Numbers after group has been searched.
				if (eval("thisform.q"+ QNum +"[" + j + "].checked") == true)
				{
					ans++;
					QNumAnsArray[ans]=QNum;
					QNumAns = QNum;
				}
				else
				{
					if (j == eval(eval("thisform.q"+ QNum +".length")-1))
					{
						if(QNumAns != QNum)
						{
							notans++;
							QNumNotAnsArray[notans] = QNum;
						}
					}
				}
			}
		}
		QNum = replaceString("q","",thisform.elements[i].name);
	}
	if (QNumNotAnsArray!="")
	{
		alert("Please answer the following question(s)" + QNumNotAnsArray);
	}
}

function checkbio2(thisform)
{
	if (thisform.ADDRESSLN1.value == "")	
	{
		alert("Please enter Address Line 1");
	}
	if (thisform.ZIPCODE.value == "")
	{
		alert("Please enter a Zip Code");
	}
}

function checkbioinfoc(thisform) 
{
	var totalcount = thisform.REQUIREDCOUNT.value;
	var totalcountREQ = thisform.REQUIREDCOUNT.value;
	for (var i=0; i < thisform.length; i++)
	{
		var elementname = thisform.elements[i].name
		if (elementname.indexOf("REQ") == -1)
		{
			if (eval("thisform.REQ"+ elementname + ".value") == 1)
			{
				if (eval("thisform." + elementname + ".value") == -1 || eval("thisform." + elementname + ".value") == '')
				{
					totalcount--;
					alert("Please Answer all Questions");
					return false;
				}
			}
		}
	}
}

function replaceString(oldS,newS,fullS)
{
	for (var i=0; i<fullS.length; i++)
	{
		if (fullS.substring(i,i+oldS.length) == oldS)
		{
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
		}
	}
	return fullS
}

function CheckTabularSurveys(NumQuest,NumCols)
{
	var CorrectAmount=0;
	var CurrentAmount=0;
	CorrectAmount = NumQuest * NumCols;
	CurrentAmount = ColA + ColB + ColC + ColD + ColE + ColF;
	alert(CorrectAmount);
	alert(CurrentAmount);
	//msgbox('surveyisnumcheck');
	if (CorrectAmount == CurrentAmount)
	{
		for (var kk=0; kk < length.form; kk++)
		{
			alert(kk);
		}

		FirstTimeColA=0,ColA=0;
		FirstTimeColB=0,ColB=0;
		FirstTimeColC=0,ColC=0;
		FirstTimeColD=0,ColD=0;
		FirstTimeColE=0,ColE=0;
		FirstTimeColF=0,ColF=0;
		return false;
	}
	else
	{
		msgbox('surveycolumncheck');
		return false;
	}
}

var ColA=0;
var FirstTimeColA=0;
function CountA(QuestNum,NumOfQuest,theobj)
{
	FirstTimeColA++;
	if (FirstTimeColA == 1)
	{
		aColA = new Array(NumOfQuest);
		for (var i = 0; i <= NumOfQuest; i++)
		{
			aColA[i] = 0;
		}
	}
	if (aColA[QuestNum] == 0)
	{
		ColA++;
		aColA[QuestNum] = aColA[QuestNum] + ColA;
	}
}

var ColB=0;
var FirstTimeColB=0;
function CountB(QuestNum,NumOfQuest,theobj)
{
	FirstTimeColB++;
	if (FirstTimeColB == 1)
	{
		aColB = new Array(NumOfQuest);
		for (var i = 0; i <= NumOfQuest; i++)
		{
			aColB[i] = 0;
		}
	}
	if (aColB[QuestNum] == 0)
	{
		ColB++;
		aColB[QuestNum] = aColB[QuestNum] + ColB;
	}
}

var ColC=0;
var FirstTimeColC=0;
function CountC(QuestNum,NumOfQuest,theobj)
{
	FirstTimeColC++;
	if (FirstTimeColC == 1)
	{
		aColC = new Array(NumOfQuest);
		for (var i = 0; i <= NumOfQuest; i++)
		{
			aColC[i] = 0;
		}
	}
	if (aColC[QuestNum] == 0)
	{
		ColC++;
		aColC[QuestNum] = aColC[QuestNum] + ColC;
	}
}

var ColD=0;
var FirstTimeColD=0;
function CountD(QuestNum,NumOfQuest,theobj)
{
	FirstTimeColD++;
	if (FirstTimeColD == 1)
	{
		aColD = new Array(NumOfQuest);
		for (var i = 0; i <= NumOfQuest; i++)
		{
			aColD[i] = 0;
		}
	}
	if (aColD[QuestNum] == 0)
	{
		ColD++;
		aColD[QuestNum] = aColD[QuestNum] + ColD;
	}
}

var ColE=0;
var FirstTimeColE=0;
function CountE(QuestNum,NumOfQuest,theobj)
{
	FirstTimeColE++;
	if (FirstTimeColE == 1)
	{
		aColE = new Array(NumOfQuest);
		for (var i = 0; i <= NumOfQuest; i++)
		{
			aColE[i] = 0;
		}
	}
	if (aColE[QuestNum] == 0)
	{
		ColE++;
		aColE[QuestNum] = aColE[QuestNum] + ColE;
	}
}

var ColF=0;
var FirstTimeColF=0;
function CountF(QuestNum,NumOfQuest,theobj)
{
	FirstTimeColF++;
	if (FirstTimeColF == 1)
	{
		aColF = new Array(NumOfQuest);
		for (var i = 0; i <= NumOfQuest; i++)
		{
			aColF[i] = 0;
		}
	}
	if (aColF[QuestNum] == 0)
	{
		ColF++;
		aColF[QuestNum] = aColF[QuestNum] + ColF;
	}
}





// Declaring valid date character, minimum year and maximum year
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
	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)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}