function openInPopUp(pfpage, pfname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
win = window.open(pfpage, pfname, winprops);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function newWin(pfpage, pfname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(pfpage, pfname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function newWinNoScroll(pfpage, pfname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl
	win = window.open(pfpage, pfname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function loLite(name) {
   if (document.images && document.images[name]) {
	   document.images[name].src=window[name+'off'].src;
   }
}

function hiLite(name) {
   if (document.images && document.images[name]) {
	   document.images[name].src=window[name+'on'].src;
   }
}

// -------------------------------------------------------------------
// TabNext()
// Function to auto-tab to next field
// Arguments:
// obj: The input object (this)
// event: Either 'up' or 'down' depending on the keypress event
// len: Max length of field - tab when input reaches this length
// next_field: Object to get focus after this one
// -------------------------------------------------------------------
var field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		field_length=obj.value.length;
	} else if (event == "up") {
		if (obj.value.length != field_length) {
			field_length=obj.value.length;
			if (field_length == len) {
				next_field.focus();
			}
		}
	}
}

// -------------------------------------------------------------------
// Validation scripts
// Registration Form State 0
// -------------------------------------------------------------------
function Checkform_reg_form_0() {
	var ssnx = document.reg_form_0.ssn3.value;
	var dobx = document.reg_form_0.dob1.value + "/" + document.reg_form_0.dob2.value + "/" + document.reg_form_0.dob3.value;
	var phonex = document.reg_form_0.phone1.value + "-" + document.reg_form_0.phone2.value + "-" + document.reg_form_0.phone3.value;
	if (document.reg_form_0.activationcode1.value != '' && document.reg_form_0.activationcode2.value != '') {
		var activationcodex = document.reg_form_0.activationcode1.value + "-" + document.reg_form_0.activationcode2.value;
		document.reg_form_0.activationcode.value = activationcodex;
	}else{
		document.reg_form_0.activationcode.value = '';
	}

	
	document.reg_form_0.ssn.value = ssnx;
	document.reg_form_0.dob.value = dobx;
	document.reg_form_0.phone.value = phonex;

	
	if (document.reg_form_0.fname.value == "") {
	   alert("Please enter your First Name.");
	   document.reg_form_0.fname.focus();
	   return false;
	 }
	 if (document.reg_form_0.lname.value == "") {
	   alert("Please enter your Last Name.");
	   document.reg_form_0.lname.focus();
	   return false;
	 }
	 if (document.reg_form_0.email.value != "") {
		var emailAddy = ((document.reg_form_0.email.value).match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.info)|(\.us)|(\.mobi)|(\.bz)|(\.eu)|(\.de)|(\.cc)|(\.tv)|(.\cn)|(\.name)|(\.ws)|(\..{2,2}))$)\b/gi));
		if (!emailAddy) {
			alert("Invalid Email address!");
			document.reg_form_0.email.focus();
			return false;
		}
	}
	if (document.reg_form_0.dob3.value == "") {
	   alert("Please enter your Date of Birth.");
	   document.reg_form_0.dob3.focus();
	   return false;
	 }
	 
	 if (isDate(document.reg_form_0.dob.value)==false){
		 //alert("Please enter your correct of Birth");
		return false;
	}
	 
	 if (document.reg_form_0.ssn3.value == "") {
	   alert("Please enter your Social Security Number.");
	   document.reg_form_0.ssn3.focus();
	   return false;
	 }
	 if (document.reg_form_0.ssn3.value.length < 4) {
	   alert("Social Security Number is not the correct length.");
	   document.reg_form_0.ssn3.focus();
	   return false;
	 }
	  
	 if(isInteger(document.reg_form_0.ssn3.value)==false){
	   alert("Only numbers are valid for the \"Social Security Number\" field.");
	   document.reg_form_0.ssn3.focus();
	   return false;
	 }

	 if (document.reg_form_0.mrn.value != "" && (document.reg_form_0.mrn.value.length > 10 || document.reg_form_0.mrn.value.length < 3)) {
	   alert("Medical Record Number is not the correct length.");
	   document.reg_form_0.mrn.focus();
	   return false;
	 }
	 
//	 if (document.reg_form_0.mrn.value != "" && isInteger(document.reg_form_0.mrn.value)==false){
//	    alert("Only numbers are valid for the \"Medical Record Number\" field.");
//		document.reg_form_0.mrn.focus();
//    	return false;
//  	}
	 
	 if (document.reg_form_0.activationcode1.value != "" && (document.reg_form_0.activationcode1.value.length < 5)) {
	   alert("Activation Code is not the correct length.");
	   document.reg_form_0.activationcode1.focus();
	   return false;
	 }

	if (document.reg_form_0.activationcode2.value != "" && (document.reg_form_0.activationcode2.value.length < 5)) {
	   alert("Activation Code is not the correct length.");
	   document.reg_form_0.activationcode2.focus();
	   return false;
	 }
 
     if (document.reg_form_0.phone1.value == "") {
	   alert("Please enter your Phone number.");
	   document.reg_form_0.phone1.focus();
	   return false;
	 }
	 if(isInteger(document.reg_form_0.phone1.value)==false){
	   alert("Only numbers are valid for the \"Phone Number\" field.");
	   document.reg_form_0.phone1.focus();
	   return false;
	 }
	 if (document.reg_form_0.phone2.value == "") {
	   alert("Please enter your Phone number.");
	   document.reg_form_0.phone2.focus();
	   return false;
	 }
 	 if(isInteger(document.reg_form_0.phone2.value)==false){
	   alert("Only numbers are valid for the \"Phone Number\" field.");
	   document.reg_form_0.phone2.focus();
	   return false;
	 }

	 if (document.reg_form_0.phone3.value == "") {
	   alert("Please enter your Phone number.");
	   document.reg_form_0.phone3.focus();
	   return false;
	 }
	  if(isInteger(document.reg_form_0.phone3.value)==false){
	   alert("Only numbers are valid for the \"Phone Number\" field.");
	   document.reg_form_0.phone3.focus();
	   return false;
	 }

	 if (document.reg_form_0.phone.value.length < 12) {
	   alert("Phone Number is not the correct length.");
	   document.reg_form_0.phone1.focus();
	   return false;
	 }

     var phoneregx = /\d\d\d\-\d\d\d\-\d\d\d\d/;
	 
	 if (!phoneregx.test(document.reg_form_0.phone.value)) {
	    alert("Your Phone number is not in the correct format.");
	    document.reg_form_0.phone1.focus();
	    return false;
	 }
	 
	 // set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < document.reg_form_0.mcoptions.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (document.reg_form_0.mcoptions[counter].checked)
radio_choice = true; 
}

if (!radio_choice)
{
// If there were no selections made display an alert box 
alert("Please select an Online Medical Record Option to continue with MediSense Registration.")
return (false);
}


	var radioLength = document.reg_form_0.mcoptions.length;
	for(var i = 0; i < radioLength; i++) {
		if(document.reg_form_0.mcoptions[i].checked) {
			if (document.reg_form_0.mcoptions[i].value == 'IHAVEACODE' && document.reg_form_0.activationcode.value == ''){
				 alert("Activation Code field cannot be empty when \"Ready to Activate\" option selected.");
				 document.reg_form_0.activationcode1.focus();
				 return false;
			}
		}
	}



   return true;
   
}

var dtCh= "/";
var z = 0;
var y = 0;
var x = 0;
var minYear=1900;
var curdate = new Date()
var maxYear = curdate.getFullYear()

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.")
		document.reg_form_0.dob1.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day.")
		document.reg_form_0.dob2.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+".")
		document.reg_form_0.dob3.focus();
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date.")
		document.reg_form_0.dob2.focus();
		return false
	}
return true
}

// -------------------------------------------------------------------
// Validation scripts
// Registration Form State 1
// -------------------------------------------------------------------
function Checkform_reg_form_1() {
	if (document.reg_form_1.uid.value == "") {
		alert("Please enter a User ID.");
		document.reg_form_1.uid.focus();
		return false;
	}
	if ((document.reg_form_1.uid.value).length < 3 || (document.reg_form_1.uid.value).length > 10) {
		alert("Your User ID must be 3-10 characters in length.");
		document.reg_form_1.uid.focus();
		return false;
	}
	
	var uidregex = /[^\w]/;
	
	if (uidregex.test(document.reg_form_1.uid.value)) {
		alert("Your User ID may only contain letters, numbers and underscores (_).");
		document.reg_form_1.uid.focus();
		return false;
		}
	// Why?
	if ((document.reg_form_1.uid.value) <= 1000000) {
		alert("User ID below 1000000 are not allowed.");
		document.reg_form_1.uid.focus();
		return false;
	} 
	if (document.reg_form_1.password.value == "") {
		alert("Please enter a PIN number.");
		document.reg_form_1.password.focus();
		return false;
	}
	if (document.reg_form_1.password_reconfirm.value == "") {
		alert("Please confirm your PIN.");
		document.reg_form_1.password_reconfirm.focus();
		return false;
	}
	if (document.reg_form_1.password.value.toLowerCase() == document.reg_form_1.uid.value.toLowerCase()) {
		alert("Your PIN cannot be the same as your User ID.");
		document.reg_form_1.password.focus();
		return false;
	}
	if ((document.reg_form_1.password.value).length < 4 || (document.reg_form_1.password.value).length > 6) {
		alert("Your PIN must be 4-6 characters in length.");
		document.reg_form_1.password.focus();
		return false;
	}
	if (((document.reg_form_1.password.value).search(/[0-9]/)) == -1) {
		alert("Your PIN must contain at least one number.");
		document.reg_form_1.password.focus();
		return false;
	}

	var pwdCriteria = /[\W+]/;

	if (pwdCriteria.test(document.reg_form_1.password.value)) {
		alert("Your PIN may only contain letters and numbers.");
		document.reg_form_1.password.focus();
		return false;
	}
	if (document.reg_form_1.password.value != document.reg_form_1.password_reconfirm.value)	{
		alert("PIN and Confirmation PIN values do not match.");
		document.reg_form_1.password.focus();
		return false;
	} 
	if (document.reg_form_1.challengeQ.options[document.reg_form_1.challengeQ.selectedIndex].value == "") {
		alert("Please select a Challenge Question.");
		document.reg_form_1.challengeQ.focus();
		return false;
	}
	if (document.reg_form_1.challengeA.value == "") {
		alert("Please enter your Challenge Answer.");
		document.reg_form_1.challengeA.focus();
		return false;
	}
	
	if (pwdCriteria.test(document.reg_form_1.challengeA.value)) {
		alert("Your Challenge Answer may only contain letters, numbers and underscores (_).");
		document.reg_form_1.challengeA.focus();
		return false;
		}
		
	if (document.reg_form_1.challengeA.value.toLowerCase() == "shadow") {
		alert("'Shadow' cannot be used as a Challenge Answer.");
		document.reg_form_1.challengeA.focus();
		return false;
	}
	if (document.reg_form_1.rchallengeA.value == "") {
		alert("Please confirm your Challenge Answer.");
		document.reg_form_1.rchallengeA.focus();
		return false;
	}
	if (document.reg_form_1.challengeA.value != document.reg_form_1.rchallengeA.value) {
		alert("Challenge Answer and Confirm Challenge Answer values do not match.");
		document.reg_form_1.challengeA.focus();
		return false;
	}
//	if (document.reg_form_1.mcoptions[0].checked == false && document.reg_form_1.mcoptions[1].checked == false && document.reg_form_1.mcoptions[2].checked == false) {
//		alert("Please select an Access option");
//		return false;
//	}
	
	return true;
}

// -------------------------------------------------------------------
// Submission scripts
// Registration Form State 1
// -------------------------------------------------------------------
function button_onclick_submit() {
   document.forms[0].clicked.value = 1;
}

function button_unclick_submit() {
   document.forms[0].clicked.value = 0;
}

// -------------------------------------------------------------------
// Validation Scripts
// Access Code Form
// -------------------------------------------------------------------
function Checkform_form_accesscode() {
	var codex = document.accessForm.code1.value + "-" + document.accessForm.code2.value;
	document.accessForm.code.value = codex;
	
	//alert(codex.length);
	
	if (document.accessForm.code.value == "") {
		alert("Please enter your Access Code.");
		document.accessForm.code1.focus();
		return false;
	}
	if (codex.length != 11) {
		alert("Access Code is not the correct length.");
		document.accessForm.code1.focus();
		return false;
	}
	 
	return true;
}

// -------------------------------------------------------------------
// Validation scripts
// MC Enrollment Form
// -------------------------------------------------------------------
function Checkform_form_mcenroll() {
	var dobx = document.form_mcenroll.dob1.value + "/" + document.form_mcenroll.dob2.value + "/" + document.form_mcenroll.dob3.value;
	var ssnx = "000-00-" + document.form_mcenroll.ssn3.value;
	document.form_mcenroll.ssn.value = ssnx;
	document.form_mcenroll.dob.value = dobx;
	if (document.form_mcenroll.ssn.value == "") {
		alert("Please enter your Social Security Number.");
		document.form_mcenroll.ssn1.focus();
		return false;
	}
	if (document.form_mcenroll.ssn.value.length < 11) {
		alert("Please enter your Social Security Number.");
		document.form_mcenroll.ssn1.focus();
		return false;
	}
	
	var ssnregex = /\d\d\d-\d\d-\d\d\d\d/;
	
	if (!ssnregex.test(document.form_mcenroll.ssn.value)) {
		alert("Social Security Number was not entered in the correct format. Example: nnn-nn-nnnn.");
		document.form_mcenroll.ssn1.focus();
		return false;
	}	
	if (document.form_mcenroll.dob.value == "") {
		alert("Please enter your Date of Birth.");
		document.form_mcenroll.dob1.focus();
		return false;
	}
	
	var dobregex = /\d\d\/\d\d\/\d\d\d\d/;
	
	if (!dobregex.test(document.form_mcenroll.dob.value)) {
		alert("Date of Birth was not entered in the correct format. Example: mm/dd/yyyy.");
		document.form_mcenroll.dob1.focus();
		return false;
	}
	
	return true;
}

// -------------------------------------------------------------------
// Cancel / Reset Registration Process
// -------------------------------------------------------------------
function button_onclick_cancel() {
	for (i=0; i<document.forms.length; i++) {
		document.forms[i].reset();
	}
	// Now Reset the Coldfusion session
	window.location = "asp/med-out.asp";
}

function button_onclick_cancel1() {
	for (i=0; i<document.forms.length; i++) {
		document.forms[i].reset();
	}
	// Now Reset the Coldfusion session
	window.location = "med-out.asp";
}

//function noactivation(){
//	var radioLength = document.reg_form_0.mcoptions.length;
//	if(radioLength == undefined)
//		if(document.reg_form_0.mcoptions.checked)
//			return document.reg_form_0.mcoptions.value;
//		else
//			return "";
//	for(var i = 0; i < radioLength; i++) {
//		if(document.reg_form_0.mcoptions[i].checked) {
//			if (document.reg_form_0.mcoptions[i].value == 'INEEDACODE' || document.reg_form_0.mcoptions[i].value == 'NOMYCHART'){
//			document.reg_form_0.activationcode1.value = 'hello';
//			document.reg_form_0.activationcode1.disabled = true;
//			document.reg_form_0.activationcode2.value = '';
//			document.reg_form_0.activationcode2.disabled = true;
//			}
//			if (document.reg_form_0.mcoptions[i].value == 'IHAVEACODE'){
//			document.reg_form_0.activationcode1.disabled = false;
//			document.reg_form_0.activationcode1.value = 'Hello';
//			document.reg_form_0.activationcode2.disabled = false;
//			document.reg_form_0.phone1.focus();
//			}
//		}
//	}
//		return true;
//}