﻿var inputAR = new Array();
var selectAR = new Array();
var tdAR = new Array();
var aCbAR = new Array();
var aRbAR = new Array();
var aSesAR = new Array();
var bCbAR = new Array();
var bRbAR = new Array();
var bSesAR = new Array();
var titleAR = new Array();
var titleValAR = new Array();
var validateAR = new Array();

var MainAllInputAR = new Array();


function makeId(lastNameObj, ssNoObj)
{ 
	if ( !ssNoObj ) { return false; }
	if ( !lastNameObj ) { return false; }
	var cmeidObj = document.getElementById("CMEID");
	if ( !cmeidObj ) { return false; }
	var scratchStr = lastNameObj.value;
	while ( scratchStr.length < 4 ) { scratchStr += "X"; }
	lastNameObj.value = scratchStr.toUpperCase();
	cmeidObj.value = scratchStr.substr(0,4).toUpperCase() + ssNoObj.value;
	return true;

}

function ckID(fld)
{ 
	var testNum = new Number(fld.value);
	if (testNum.toString() == "NaN" || fld.value.indexOf("e") > -1 ||  fld.value.indexOf(".") > -1 || fld.value.length < 4 ) 
		{ 
		fld.focus();
		fld.select();
		alert('Invalid ID \n Please provide 4 digits');
		return false; 
		}
	return true;
}

function isBlank(val)
{
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) 
		{
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
}

function ckTitle(fld)
{
	var fldTitle = document.getElementById("fldTitle");
	fldTitle.value = ""
	titleValAR[getIndex(titleAR, fld.id)] = (fld.checked)? fld.value :  "";
	for (cnt = 0; cnt < titleValAR.length; cnt++)
   	{
   	if ( isBlank(fldTitle.value) ) { fldTitle.value = titleValAR[cnt]; }
   	else { if ( !isBlank(titleValAR[cnt]) ) { fldTitle.value = fldTitle.value + ", " + titleValAR[cnt]; } }
		}
}


function goodEmail(str)
{	
	var atSgnPos = str.indexOf("@")
   if ( ( atSgnPos > 2 ) && ( str.lastIndexOf(".") > atSgnPos + 2 ) ) { return true; };
   return false;
}


function ckRequired(formAct) 
{ 
/*
Required to use 'ckRequired':
	a hidden field with id = 'form-id' and value holding the name of the form to submit
	the argument 'formAct' is a string that is the action to be passed to the submit form
	each input field and select field should have a label with an id = 'lbl' plus the field id
Note:
	each label containing a "*' will make the corresponding field 'required' and will alert an error if empty
	the global array 'validateAR' is filled in the 'initPage' function */


/*This section of ckRequired is REQUIRED for functionality outside of validation of fields */

	var lastNameObj = document.getElementById("LastName");
	var ssNoObj = document.getElementById("SSno");
	makeId(lastNameObj, ssNoObj);

/*End Required Section */

/*This section of ckRequired relates strictly to validation of fiels--check to see if populated and alert user */
	var validateID = ""; 
	var validateLbl = ""; 
	var errorStr = ""; 
	var returnErrorStr = "";
	for (cnt = 0; cnt < validateAR.length; cnt++)
    	{
    	validateID = validateAR[cnt].id;
    	validateLblID = "lbl" + validateID; 
		var lbl = new getObj(validateLblID);
		errorStr = lbl.obj.innerHTML + " is Required";
    	if ( isBlank(validateAR[cnt].value) ) { returnErrorStr += "\n" + errorStr; }
    	}
		if ( !isBlank(returnErrorStr) ) { alert(returnErrorStr); return; }
		if (goodEmail(document.getElementById("Email").value) == false)   	
			{
			alert("The Email Address field cannot be left blank and must be in the correct format.  Please enter your email address in the format myname@mycompany.com.");
			return;
			}

/*End Validation Checking */

/*This section of ckRequired submits the form and is REQUIRED */

	var formID = new getObj("form-id");
	var submitForm = new getObj(formID.obj.value);
	submitForm.obj.action = formAct;  
	submitForm.obj.submit();	

/*End submit section */
   	
}

function initPage()
{	
	loadAR()
	var LastName = new getObj("LastName");
	var confType = new getObj("A-RB-1");
	if ( LastName.obj.id != "h-fldError" ) { if ( !isBlank(LastName.obj.value) ) { fillid(LastName.obj); }}
	confType.obj.click()
}

function loadAR()
{ 
	inputAR = document.getElementsByTagName("input");
	selectAR = document.getElementsByTagName("select");
	tdAR = window.parent.document.getElementsByTagName("td");
	var cbIndex = 0;
	var feeIndex = -1;
	var sesIndex = 0;
	var titleIndex = -1;
	var validateIndex = -1;
	var validateID = ""; 
	var validateLblID = "";
	for (cnt = 0; cnt < selectAR.length; cnt++)
		{
    	validateID = selectAR[cnt].id;
    	validateLblID = "lbl" + validateID; 
		var lbl = new getObj(validateLblID);
		if ( lbl )
			{
			if ( lbl.obj.innerHTML.indexOf("*") > -1 )
				{
				validateIndex = validateIndex + 1;
				validateAR[validateIndex] = selectAR[cnt]; 
				}
			}
		}

	for (cnt = 0; cnt < inputAR.length; cnt++)
		{ 
    	validateID = inputAR[cnt].id;
    	validateLblID = "lbl" + validateID; 
		var lbl = new getObj(validateLblID);
		if ( lbl )
			{
			if ( lbl.obj.innerHTML.indexOf("*") > -1 )
				{
				validateIndex = validateIndex + 1;
				validateAR[validateIndex] = inputAR[cnt]; 
				}
			}

		if ( inputAR[cnt].id.indexOf("A-CB-") > - 1 && inputAR[cnt].id.indexOf("_") < 0 )
			{ 
			if ( inputAR[cnt].id.substr(inputAR[cnt].id.length - 2,1) == "-" ){cbIndex = inputAR[cnt].id.substr(inputAR[cnt].id.length - 1)}
			else {cbIndex = inputAR[cnt].id.substr(inputAR[cnt].id.length - 2)}

			aCbAR[cbIndex] = inputAR[cnt]; 
			}
		if ( inputAR[cnt].id.indexOf("A-RB-") > - 1 && inputAR[cnt].id.indexOf("_") < 0 )
			{ 
			cbIndex = inputAR[cnt].id.substr(inputAR[cnt].id.length - 1)	
			aRbAR[cbIndex] = inputAR[cnt]; 
			}
		if ( inputAR[cnt].id.indexOf("B-CB-") > - 1 && inputAR[cnt].id.indexOf("_") < 0 )
			{ 
			cbIndex = inputAR[cnt].id.substr(inputAR[cnt].id.length - 1)	
			bCbAR[cbIndex] = inputAR[cnt]; 
			}
		if ( inputAR[cnt].id.indexOf("B-RB-") > - 1 && inputAR[cnt].id.indexOf("_") < 0 )
			{ 
			cbIndex = inputAR[cnt].id.substr(inputAR[cnt].id.length - 1)	
			bRbAR[cbIndex] = inputAR[cnt]; 
			}
		if ( inputAR[cnt].id.indexOf("A-") > - 1 && inputAR[cnt].id.indexOf("_SES") > - 1 )
			{ 
			if ( inputAR[cnt].id.substr(inputAR[cnt].id.indexOf("_") - 2, 1) == "-" ){sesIndex = 			inputAR[cnt].id.substr(inputAR[cnt].id.indexOf("_") - 1, 1)}
			else {sesIndex = inputAR[cnt].id.substr(inputAR[cnt].id.indexOf("_") - 2, 2)}
			aSesAR[sesIndex] = inputAR[cnt]; 

			}
		if ( inputAR[cnt].id.indexOf("B-") > - 1 && inputAR[cnt].id.indexOf("_SES") > - 1 )
			{ 
			sesIndex = inputAR[cnt].id.substr(inputAR[cnt].id.indexOf("_") - 1, 1)
			bSesAR[sesIndex] = inputAR[cnt]; 
			}
			
		if ( inputAR[cnt].id.indexOf("TITLE-") > - 1 )
			{ 
			titleIndex = titleIndex + 1	
			titleAR[titleIndex] = inputAR[cnt]; 
			titleValAR[titleIndex] = "";
			}
		}
}


function addToSesLst(fldObj, val, qty, fee)
{  
	if ( isBlank(fldObj.value) ) { fldObj.value = val + "-" + qty + ";" + fee; }
	else { fldObj.value = fldObj.value + "|" + val + "-" + qty + ";" + fee; } 
}	

function ckBlock(obj)
{     
	var sessListObj = document.getElementById("fldSess");
	var eventIDObj = document.getElementById("h-fldEventID");
	var attenTypeObj = document.getElementById("SelectAttendeeType");
	var feeObj = document.getElementById("fee");
	var nonSymposiaObj = document.getElementById("NonSymposia");
	var amountObj = document.getElementById("amount");
	sessListObj.value = "";
	feeObj.value = "";
	attenTypeObj.value = ""

	for (var cnt = 1; cnt < aRbAR.length; cnt++) { if ( aRbAR[cnt].checked ){attenTypeObj.value = aRbAR[cnt].value; }}
	
	switch (attenTypeObj.value)
		{
		case "Physician"
			:	amountObj.value = "595";
				feeObj.value = "$" + amountObj.value; var isA = true; var isB = false;
				addToSesLst(sessListObj, eventIDObj.value, "1", amountObj.value);
				addToSesLst(sessListObj, nonSymposiaObj.value, "1", "0");
 
				for (var cnt = 1; cnt < aCbAR.length; cnt++) 
					{
					if ( aCbAR[cnt].checked ) {addToSesLst(sessListObj, aSesAR[cnt].value, "1", "0"); }
					}
					break;
		case "Symposia"
			:	feeObj.value = ""; var isB = true; var isA = false; 
				addToSesLst(sessListObj, eventIDObj.value, "1", "0");
				var feeNum = 0; 
				for (var cnt = 1; cnt < bCbAR.length; cnt++) 
					{
					if ( bCbAR[cnt].checked ) { feeNum += Number(bCbAR[cnt].value); addToSesLst(sessListObj, bSesAR[cnt].value, "1", bCbAR[cnt].value); }
					}
				amountObj.value = feeNum.toString();
				feeObj.value = "$" + amountObj.value;  
				break;
		default
			:	amountObj.value = "300"; 
				feeObj.value = "$" + amountObj.value; var isA = true; var isB = false; 
				addToSesLst(sessListObj, eventIDObj.value, "1", amountObj.value);
				addToSesLst(sessListObj, nonSymposiaObj.value, "1", "0");

				for (var cnt = 1; cnt < aCbAR.length; cnt++) 
					{
					if ( aCbAR[cnt].checked ) { addToSesLst(sessListObj, aSesAR[cnt].value, "1", "0"); }
					}
				break;
		}
	if ( isA ) { for (var cnt = 1; cnt < aCbAR.length; cnt++) { aCbAR[cnt].disabled = false; }}
	if ( isB ) { for (var cnt = 1; cnt < bCbAR.length; cnt++) { bCbAR[cnt].disabled = false; }}
	if ( !isA ) { for (var cnt = 1; cnt < aCbAR.length; cnt++) { aCbAR[cnt].disabled = true; }}
	if ( !isB ) { for (var cnt = 1; cnt < bCbAR.length; cnt++) { bCbAR[cnt].disabled = true; }}
}


function getIndex(arObj, valStr)
{ 
	var valStrUpper = valStr.toUpperCase();
	var idUpper = "";
	for (cnt = 0; cnt < arObj.length; cnt++) 
		{ 
		if ( arObj[cnt] )
			{ 
			idUpper = arObj[cnt].id.toUpperCase()
			if ( idUpper == valStrUpper ) { return cnt; } 
			}
		} 
	return -1;
}

function lTrim(str) 
{
	var returnStr = str;
	if ( returnStr  == "" ) { return returnStr; }
	for ( var i=0; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t"; i++ );
	return str.substring(i,str.length);
}


function rTrim(str) 
{
	if ( str==null){return str; }
	for ( var i=str.length-1; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t"; i-- );
	return str.substring(0,i+1);
}


function setAmount(obj)
{ 	
	var amount = document.getElementById("amount");
	amount.value = obj.value;
	
	if ( !amount ) { return; }
	var attendeeType = document.getElementById("SelectAttendeeType");
	if ( !attendeeType ) { return; }
	if ( obj.value.indexOf("-") == -1 ) { return; }
	var splitAR = obj.value.split("-")
	attendeeType.value = rTrim(splitAR[0]);
	amount.value = lTrim(splitAR[1]);

}


function getObj(name)
{ 
  if (document.getElementById)
  	{ 
  	var testobj = document.getElementById(name);
  	if ( testobj )
  		{
  		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style; 
 	 	}
 	else
		{
		this.obj = document.getElementById("h-fldError");
		this.style = document.getElementById("h-fldError").style; 
 		}
	
  	}
  else if (document.all)
  	{
  	var testobj = document.all[name];
  	if ( testobj )
  		{
		this.obj = document.all[name];
		this.style = document.all[name].style;
 	 	}
 	else
		{
		this.obj = document.all["h-fldError"];
		this.style = document.all["h-fldError"].style; 
 		}
  }
  else if (document.layers)
  	{
  	var testobj = document.layers[name];
  	if ( testobj )
  		{
   	this.obj = document.layers[name];
   	this.style = document.layers[name].style;
 	 	}
 	else
		{
		this.obj = document.layers["h-fldError"];
		this.style = document.layers["h-fldError"].style; 
 		}
  }
}