var iErrors = 0;
var bProcessing = false;
var theFormObj;

function SubmitBioForm(formObj)
{
	if (!bProcessing)
	{
		bProcessing = true;
		document.getElementById('btnSubmit').enabled = false;
		theFormObj = formObj;
		setTimeout('SubmitBioForm2()', 500);
	}
}

function SubmitBioForm2()
{
	//document.getElementById('btnSubmit').enabled = false;
	var strAlert = ValidateForm1() + ValidateForm2();
	
	if(strAlert == ""){
	var selectedOffices = GetSelectedValues('selectedOffices', '|', false);
	var selectedLangs = GetSelectedValues('selectedLangs', '|', false);
	var selectedPracs = GetSelectedValues('selectedPracs', '|', false);
	
	var hiddenOffices = document.getElementById('hselectedOffices');
	var hiddenPracs = document.getElementById('hselectedLangs');
	var hiddenLangs = document.getElementById('hselectedPracs');
	
	
	//education stuff
	var cmEducationVals = GetEducationVals('eduVals', 'cm');
	var customEducationVals = GetEducationVals('eduVals', 'custom');
	
	var hiddenCMEducations = document.getElementById('hCMEducations');
	var hiddenCustomEducations = document.getElementById('hCustomEducations');
	hiddenCustomEducations.value = customEducationVals;
	hiddenCMEducations.value = cmEducationVals;
	
	
	hiddenOffices.value = selectedOffices;
	hiddenPracs.value = selectedLangs;
	hiddenLangs.value = selectedPracs;
	
	theFormObj.submit();
	}
	else{
		alert(strAlert);
	}
}

function validateEmail(eVal)
{
	try{
		var r1 = new RegExp("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$");
		//var r1 = new RegExp("[\w\.\-0-9]@[\w\-0-9]{2,}\.[\.\w\-0-9]+"); //my beautiful reg exp that works in c#

		var x = eVal.match(r1);
		if(x != null)
			return true;
		else return false;
	}
	catch(e)
	{
		alert(e.message);
		return false;
	}
}

function ValidateForm1()
{ //This function is called to validate the first half of the form.
/*
Name				ID
Level				level
First Name			txtFirstName
Last Name			txtLastName
Email				txtEmail
Office				selectedOffices
Language			selectedLangs
Practice Groups		selectedPracs
*/

	var strAlert = "";
	var level = document.getElementById('level').options[document.getElementById('level').selectedIndex].text;
	var txtFirstName = document.getElementById('txtFirstName').value;
	var txtLastName = document.getElementById('txtLastName').value;
	var txtEmail = document.getElementById('txtEmail').value;
	var selectedOffices = GetSelectedValues('selectedOffices', '|', false);
	var selectedLangs = GetSelectedValues('selectedLangs', '|', false);
	var selectedPracs = GetSelectedValues('selectedPracs', '|', false);

	if(level == "")
		strAlert += ++iErrors + ". Level is required.\n";

	if(txtFirstName == "")
		strAlert += ++iErrors + ". First Name is required.\n";

	if(txtLastName == "")
		strAlert += ++iErrors + ". Last Name is required.\n";

	if(txtEmail == "")
		strAlert += ++iErrors + ". Email is required.\n";
	else
	{
		if(!validateEmail(txtEmail))
			strAlert += ++iErrors + ". Email format is invalid.\n";
	}
		
	if(selectedOffices == "")
		strAlert += ++iErrors + ". Offices are required.\n";

	if(selectedPracs == "")
		strAlert += ++iErrors + ". Practice groups are required.\n";

	if(selectedLangs == "")
		strAlert += ++iErrors + ". Languages are required.\n";

	return strAlert;
}


function ValidateForm2()
{ //This function is called to validate the second half of the form.
	/*
	Education			eduVals
	*/
	var bValidated = true;
	var strAlert = "";
	var txtEducation = document.getElementById('eduVals').value;

	if(txtEducation == "")
	{
		strAlert += ++iErrors + ". Education details are required.\n";
		bValidated = false;
	}

	if(bValidated)
		return "";
	else
		return strAlert;
}


function ShowConfirmationWindow()
{
	try
	{
	
	var strAlert = ValidateForm1() + ValidateForm2();
	
	if(strAlert == ""){
	//get all the variables we're gonna pass
	var level = document.getElementById('level').options[document.getElementById('level').selectedIndex].text;
	var txtPrefix = document.getElementById('txtPrefix').value;
	var txtFirstName = document.getElementById('txtFirstName').value;
	var txtMiddleName = document.getElementById('txtMiddleName').value;
	var txtLastName = document.getElementById('txtLastName').value;
	var txtSuffix = document.getElementById('txtSuffix').value;
	var txtEnglishFirstName = document.getElementById('txtEnglishFirstName').value;
	var txtEnglishLastName = document.getElementById('txtEnglishLastName').value;
	var txtEmail = document.getElementById('txtEmail').value;

	/*
	var selectedOffices = document.getElementById('selectedOffices').value;
	var selectedLangs = document.getElementById('selectedLangs').value;
	*/
	
	var selectedOffices = GetSelectedValues('selectedOffices', '|', true);
	var selectedLangs = GetSelectedValues('selectedLangs', '|', true);
	
	var txtDirectPhone = document.getElementById('txtDirectPhone').value;
	var txtOtherPhone = document.getElementById('txtOtherPhone').value;
	var hiddendoc = document.getElementById('hiddendoc').value;
	
	//education is diffn't, we just want the table
	
	var eduVals = document.getElementById('tblEducation').innerHTML;

	var hiddenpic = document.getElementById('hiddenpic').value;
	var hPhotoConsent = document.getElementById('hPhotoConsent').value;
	
	var intranetPhoto = "0";
	if(document.getElementById('chkIntranetPhoto').checked)
	{
		intranetPhoto = "1";
	}

	var deleteBio = "0";
	if(document.getElementById('deleteBio') && document.getElementById('deleteBio').checked)
		deleteBio = "1";
	var deleteBioOption = "0";
	if(document.getElementById('deleteBioOption'))
		deleteBioOption = "1";
	
	var deletePhoto = "0";
	if(document.getElementById('deletePhoto') && document.getElementById('deletePhoto').checked)
		deletePhoto = "1";
	var deletePhotoOption = "0";
	if(document.getElementById('deletePhotoOption'))
		deletePhotoOption = "1";

	var strPhotoConsent = "";
	if(hPhotoConsent == "0")
	{
		strPhotoConsent = "No";
	}
	else
	{
		strPhotoConsent = "Yes";
	}
	
	var selectedPracs = GetSelectedValues('selectedPracs', '|', true);
	var tokyo = document.getElementById('tokyo').options[document.getElementById('tokyo').selectedIndex].text;
	
	var strGetString = "";
	
	strGetString += 'level=' + encodeURIComponent(level);
	strGetString += '&txtPrefix=' + encodeURIComponent(txtPrefix);
	strGetString += '&txtFirstName=' + encodeURIComponent(txtFirstName);
	strGetString += '&txtMiddleName=' + encodeURIComponent(txtMiddleName);
	strGetString += '&txtLastName=' + encodeURIComponent(txtLastName);
	strGetString += '&txtSuffix=' + encodeURIComponent(txtSuffix);
	strGetString += '&txtEnglishFirstName=' + encodeURIComponent(txtEnglishFirstName);
	strGetString += '&txtEnglishLastName=' + encodeURIComponent(txtEnglishLastName);
	
	strGetString += '&txtEmail=' + encodeURIComponent(txtEmail);
	strGetString += '&selectedOffices=' + encodeURIComponent(selectedOffices);
	strGetString += '&selectedLangs=' + encodeURIComponent(selectedLangs);
	strGetString += '&txtDirectPhone=' + encodeURIComponent(txtDirectPhone);
	strGetString += '&txtOtherPhone=' + encodeURIComponent(txtOtherPhone);
	strGetString += '&hiddendoc=' + encodeURIComponent(hiddendoc);
	strGetString += '&eduVals=' + encodeURIComponent(eduVals);
	strGetString += '&hiddenpic=' + encodeURIComponent(hiddenpic);
	strGetString += '&chkIntranetPhoto=' + intranetPhoto;
	strGetString += '&deleteBio=' + deleteBio;
	strGetString += '&deleteBioOption=' + deleteBioOption;
	strGetString += '&deletePhoto=' + deletePhoto;
	strGetString += '&deletePhotoOption=' + deletePhotoOption;
	strGetString += '&rdPhotoConsent=' + hPhotoConsent;
	strGetString += '&selectedPracs=' + encodeURIComponent(selectedPracs);
	strGetString += '&tokyo=' + encodeURIComponent(tokyo);

	window.open('confirm.aspx?' + strGetString, 'confirmWindow','width=750,height=550,scrollBars=yes');
	}
	else{
		alert("The following errors were found:\n" + strAlert);
		iErrors = 0;
	}
	}
	catch(e)
	{
		alert(e.message);
	}
}

function CheckForm1AndProceed()
{
	var strAlerts = ValidateForm1();
	if (strAlerts!="")
	{
		alert("The following errors were found:\n" + strAlerts);
		iErrors = 0;
	}
	else
	{
		toggleBioSteps();
		window.scroll(0,0);
	}
}

function GetEducationVals(eduID, sType)
{ 

/*
edu entried seperated by colon
names, degrees, honors text are escaped
each field is seperated by a | char
=========
example
=========
Northwestern University|6404|BA|624|1920||1|;
Northwestern University|6404|MBA|627|1921|honors%20text%27s%20desc|1|;
mustafa%27s%20school|-1|MBA|627|1921|honors%20text%27s%20desc|1|;
mustafa%27s%20school|-1|mustafa%27s%20deg|-1|1921|honors%20text%27s%20desc|1|;
Northwestern University|6404|mustafa%27s%20deg|-1|1921|honors%20text%27s%20desc|1|;
*/

	var eduVals = document.getElementById(eduID).value;
	//var eduVals = "Northwestern University|6404|BA|624|1920||1|;Northwestern University|6404|MBA|627|1921|honors%20text%27s%20desc|1|;mustafa%27s%20school|-1|MBA|627|1921|honors%20text%27s%20desc|1|;mustafa%27s%20school|-1|mustafa%27s%20deg|-1|1921|honors%20text%27s%20desc|1|;Northwestern University|6404|mustafa%27s%20deg|-1|1921|honors%20text%27s%20desc|1|;";
	var educationEntries = eduVals.split(';');
	var strReturn = '';

	for(var iCurrEd = 0; iCurrEd < educationEntries.length - 1; iCurrEd++)
	{
		var currDetails = educationEntries[iCurrEd].split('|');
		
		var strCustomSchool = '';
		var strCustomDegree = '';
		
		//0 = school name (escaped)
		//1 = school id, -1 if custom
		//2 = degree name (escaped)
		//3 = degree id, -1 if custom
		//4 = year graduated
		//5 = honors text (escaped)
		//6 = bLawSchool (1 = true, 0 = false)		   
		if(sType == 'custom')
		{
			if(currDetails[1] == '-1' || currDetails[3] == '-1') //this is custom
			{
				if(strReturn != '')
				{
					strReturn += ';'; //this is not the first				
				}
				
				if(currDetails[1] == '-1')
				{
					strCustomSchool = '^1';
				}
				else
				{
					strCustomSchool = '^0';
				}
				
				if(currDetails[3] == '-1')
				{
					strCustomDegree = '^1';
				}
				else
				{
					strCustomDegree = '^0';
				}
				
				strReturn += currDetails[0] + strCustomSchool + '|' + currDetails[2] + strCustomDegree + '|' + currDetails[4] + '|' + currDetails[5] + '|' + currDetails[6];				
			}
		}
		else //cm
		{
			if(currDetails[1] != '-1' && currDetails[3] != '-1')
			{
				if(strReturn != '')
				{																									    
					strReturn += ';'; //this is not the first				
				}
				
				strReturn += currDetails[1] + '|' + currDetails[3] + '|' + currDetails[4] + '|' + currDetails[5] + '|' + currDetails[6];
			}
		}
		
	}
	
	return strReturn;
}



//returns [delim] seperated values of the text or 
//values for a select box (if bText = true, then it is text otherwise it is vals)
function GetSelectedValues(listObjID, delim, bText)
{
	var objSel = document.getElementById(listObjID);
	
	var arr;
	var value;
	arr = objSel.options;        
	
	var strReturn = "";
	
	for(var i = 0; i < arr.length; i++)
	{
		arr[i].selected = true;
		
		if(bText){
			strReturn += arr[i].text;
		}
		else{
			strReturn += arr[i].value;
		}
		strReturn += delim;
	}
	
	return strReturn;
	
}

function Confirm()
{
	
	try{
	var myPar = window.opener;
	
	var oldBtn = myPar.document.getElementById('btnContinue3');
	oldBtn.style.display = "none";

	var finishBtn = myPar.document.getElementById('btnSubmit');
	finishBtn.style.display = "";
	
	window.close();
	}
	catch(e)
	{
	alert(e.message);
	}
	
}
