
// AJAX Function Calls - start //
/////////////////////////////////

function ajaxDeleteProfile(pword, regno)
{
	var vals = new Array();
	vals[0] = pword;
	vals[1] = regno;
	showHideDiv('lyrLoading', 1);
	agent.call('', 'deleteProfile', 'ajaxConfirmDeleteProfile', vals);
}

function ajaxConfirmDeleteProfile(arr)
{
	showHideDiv('lyrLoading', 0);
	if (arr['err'] == "OK")
	{
		alert("Profile has been completely deleted.");
		document.location="phys_list.php";
	} else {
		alert(arr);
	}
}

function ajaxExport(func_name, type)
{
	// type = 'live' or 'hold' or ''.
	agent.call('', func_name, 'ajaxExportStop', type);
}

function ajaxExportBuilder()
{
	var frm = document.frmOne;
	var field_count = 0;
	var err = "";
	var fields = new Array();
	var tables = new Array();
	var vars = new Array();

	// Live or Staging data?
	var dataType = frm.extype.value;

	if (dataType == "staging")
	{
		dataType = "hold";
	} 

	// pp_live_physician, fPhys_table
	var cb_0 = returnMultipleSelectedValues('fPhys_table');
	fields[0] = cb_0;
	if (cb_0 == '')
	{
		//err += "Physician: please select at least 1 field.\n";
		tables[0] = "pp_"+dataType+"_physician";
	} else {
		field_count++;
		tables[0] = "pp_"+dataType+"_physician";
	}

	if (frm.fCb_1.checked)
	{
		// pp_live_address, fAddr_table
		var cb_1 = returnMultipleSelectedValues('fAddr_table');
		fields[1] = cb_1;

		if (cb_1 == '')
		{
			err += "Address: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[1] = "pp_"+dataType+"_address";
		}
	} else {
		fields[1] = '';
		tables[1] = '';
	}

	if (frm.fCb_2.checked)
	{
		// (pp_live_censures), (fCens_table)
		var cb_2 = returnMultipleSelectedValues('fCens_table');
		fields[2] = cb_2;
		if (cb_2 == '')
		{
			//err += "Censures: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[2] = "pp_"+dataType+"_censures";
		}

		if (cb_2 == '')
		{
			err += "Censures: please select at least 1 field.\n";
		}
	} else {
		fields[2] = '';
		tables[2] = '';
	}

	if (frm.fCb_3.checked)
	{
		// pp_live_certification, fCert_table
		var cb_3 = returnMultipleSelectedValues('fCert_table');
		fields[3] = cb_3;
		if (cb_3 == '')
		{
			err += "Certification: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[3] = "pp_"+dataType+"_certification";
		}
	} else {
		fields[3] = '';
		tables[3] = '';
	}

	if (frm.fCb_4.checked)
	{
		// pp_live_conviction, fConvict_table
		var cb_4 = returnMultipleSelectedValues('fConvict_table');
		fields[4] = cb_4;
		if (cb_4 == '')
		{
			err += "Criminal Conviction: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[4] = "pp_"+dataType+"_conviction";
		}
	} else {
		fields[4] = '';
		tables[4] = '';
	}

	if (frm.fCb_5.checked)
	{
		// pp_live_discipline, fFda_table
		var cb_5 = returnMultipleSelectedValues('fFda_table');
		fields[5] = cb_5;
		if (cb_5 == '')
		{
			err += "Final Disciplinary Action: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[5] = "pp_"+dataType+"_discipline";
		}
	} else {
		fields[5] = '';
		tables[5] = '';
	}

	if (frm.fCb_6.checked)
	{
		// pp_live_education, fMedu_table
		var cb_6 = returnMultipleSelectedValues('fMedu_table');
		fields[6] = cb_6;
		if (cb_6 == '')
		{
			err += "Medical Education: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[6] = "pp_"+dataType+"_education";
		}
	} else {
		fields[6] = '';
		tables[6] = '';
	}

	if (frm.fCb_7.checked)
	{
		// pp_live_field_practice, fFop_table
		var cb_7 = returnMultipleSelectedValues('fFop_table');
		fields[7] = cb_7;
		if (cb_7 == '')
		{
			err += "Field of Practice: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[7] = "pp_"+dataType+"_field_practice";
		}
	} else {
		fields[7] = '';
		tables[7] = '';
	}

	if (frm.fCb_8.checked)
	{
		// pp_live_malpractice, fMal_table
		var cb_8 = returnMultipleSelectedValues('fMal_table');
		fields[8] = cb_8;
		if (cb_8 == '')
		{
			err += "Malpractice Judgment: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[8] = "pp_"+dataType+"_malpractice";
		}
	} else {
		fields[8] = '';
		tables[8] = '';
	}

	if (frm.fCb_9.checked)
	{
		// pp_live_suspension, fSusp_table
		var cb_9 = returnMultipleSelectedValues('fSusp_table');
		fields[9] = cb_9;
		if (cb_9 == '')
		{
			err += "Suspension: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[9] = "pp_"+dataType+"_suspension";
		}
	} else {
		fields[9] = '';
		tables[9] = '';
	}

	if (frm.fCb_10.checked)
	{
		// pp_live_training, fPgt_table
		var cb_10 = returnMultipleSelectedValues('fPgt_table');
		fields[10] = cb_10;
		if (cb_10 == '')
		{
			err += "Postgraduate Training: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[10] = "pp_"+dataType+"_training";
		}
	} else {
		fields[10] = '';
		tables[10] = '';
	}

	if (frm.fCb_11.checked)
	{
		// (pp_live_disciplines), (fDisc_table)
		var cb_11 = returnMultipleSelectedValues('fDisc_table');
		fields[11] = cb_11;
		if (cb_11 == '')
		{
			//err += "Disciplines: please select at least 1 field.\n";
		} else {
			field_count++;
			tables[11] = "pp_"+dataType+"_disciplines";
		}

		if (cb_11 == '')
		{
			err += "Disciplines: please select at least 1 field.\n";
		}
	} else {
		fields[11] = '';
		tables[11] = '';
	}

	if (field_count < 1 && err == '')
	{
		err += "Select at least 1 field from any of the available tables.\n";
	}

	if (err > '')
	{
		alert(err);
	} else {
		// display 'loading' animation.
		showHideDiv('lyrLoading', 1);
		showHideDiv('lyrDownload', 0);

		// send parameters to the php function and wait for response.
		vars[0] = tables;
		vars[1] = fields;
		vars[2] = dataType;

/* for debugging script - comment out for LIVE mode */
//document.getElementById('lyrTest').innerHTML=vars+"<hr>"+vars['0']+"<hr>"+vars['1']+"<hr>"+vars['2'];
//showHideDiv('lyrTest', 1);

		agent.call('', 'exportBuilder', 'ajaxExportBuilderStop', vars);
	}
}

function ajaxExportBuilderStop(arr)
{
	// Display the link to document or error message.
	var msg = "";
	if (arr['error'] == "1") {
		msg = "There was an error creating the .csv file<br />Please try again.";
	} else if (arr['nodata'] == "1") {
		msg = "There was no data found for export.";
	} else if (arr['file'] > " ") {
		msg = "<a href='export/"+arr['file']+"'>Download CSV File</a><br /><br />It is recommended that you save this file to your hard drive.";
	} else {
		msg = "Sorry, could not process your reaquest at this time.";
	}

	document.getElementById('lyrDownload').innerHTML=msg;
	showHideDiv('lyrLoading', 0);
	showHideDiv('lyrDownload', 1);
}

function ajaxExportStop(arr)
{
	var msg = "";
	if (arr['error'] == "1") {
		msg = "There was an error creating the .csv file<br />Please try again.";
	} else if (arr['nodata'] == "1") {
		msg = "There was no data found for export.";
	} else if (arr['file'] > " ") {
		msg = "<a href='export/"+arr['file']+"'>Download CSV File</a><p>It is recommended that you save<br />this file to your hard drive.</p>";
	} else {
		msg = "Sorry, could not process your reaquest at this time.";
	}

	document.getElementById('lyrDownload').innerHTML=msg;
	showHideDiv('lyrLoading', 0);
	showHideDiv('lyrDownload', 1);
}

function convertStaging2Live(type)
{
	agent.call('', 'convertStagingStart', 'convertStagingStop', type);
}

function convertStagingStop(arr)
{
	document.getElementById("lyrLoading").style.display='none';
	document.getElementById("lyrComplete").style.display='block';

	var msg = "<table>";
	msg += "<tr style='font-weight:bold;'><td>Section Heading</td><td>Records Updated&nbsp;</td><td>Records Inserted</td></tr>";
	var x = 0;
	for (x in arr['update'])
	{
		msg += "<tr><td>"+arr['label'][x]+"&nbsp;</td><td>"+arr['update'][x]+"</td><td>"+arr['insert'][x]+"</td></tr>";	
	}
	msg += "</table>";

	document.getElementById('lyrCompleteMsg').innerHTML=msg;

}

function resetProvState(el)
{
	var country = el.value;
	agent.call('', 'getProvStateList', 'setProvStateList', country);
}

function setProvStateList(arr)
{
	var el = document.frmOne.fProv;
	var x = 0;

	// Delete existing options & insert the new ones.
	clearOptions(el);
	for (x in arr['abbr'])
	{
		el.options[x] = new Option(arr['full'][x], arr['abbr'][x]);
	}
}

/////////////////////////////////
// AJAX Function Calls - stop //





//   Project Specific - start   //
/////////////////////////////////

function adminExport(exp, type)
{
	if (exp == "phys_addr") {
		if (confirm('Do you wish to export "Address Information"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportPhys_address', type);
		}
	} else if (exp == "phys_aop") {
		if (confirm('Do you wish to export "Area of Practice Information"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportPhys_aop', type);
		}
	} else if (exp == "phys_cert") {
		if (confirm('Do you wish to export "Certification Information"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportPhys_cert', type);
		}
	} else if (exp == "phys_postgrad") {
		if (confirm('Do you wish to export "Postgraduate Information"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportPhys_postgrad', type);
		}
	} else if (exp == "system_aop") {
		if (confirm('Do you wish to export "System A.O.P. Settings"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportSystem_aop','');
		}
	} else if (exp == "system_cert") {
		if (confirm('Do you wish to export "System Certification Settings"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportSystem_cert','');
		}
	} else if (exp == "system_issuedby") {
		if (confirm('Do you wish to export "Issued By Settings"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportSystem_issuedby','');
		}
	} else if (exp == "system_lang") {
		if (confirm('Do you wish to export "System Language Settings"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportSystem_lang','');
		}
	} else if (exp == "system_pgt") {
		if (confirm('Do you wish to export "System Postgraduate Training Settings"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportSystem_pgt','');
		}
	} else if (exp == "system_school") {
		if (confirm('Do you wish to export "System School Settings"?'))
		{
			// show loading image and call ajax function.
			showHideDiv('lyrDownload', 0);
			showHideDiv('lyrLoading', 1);
			ajaxExport('exportSystem_school','');
		}
	} else {
		showHideDiv('lyrLoading', 0);
		alert("You have requested an invalid export type.");
	}

}

function confirmConvertXml2Db(compare_time)
{
	var conf = confirm("This action will update all related physician data in the 'Staging' tables.\n\nClick OK to proceed with the update.\n\nThe update process may take a minute or so, please do not\ninterupt the process and let it complete.");
	if (conf)
	{
		document.location="import_xml2db.php?conf="+compare_time;
	}
}

function confirmProfileDelete(regno)
{
	var pword = prompt("This action will completely remove this profle.\nEnter your password to complete the deletion.", "");
	if (pword > '')
	{
		// if a password was entered - call the ajax function to confirm password.
		// if confirmed - delete, if not - display error message.
		ajaxDeleteProfile(pword, regno);
	}
}

function deleteCensure(id, date, name, title)
{
	var conf = confirm("Do you wish to delete the following entry?\n\n- "+name+"\n- "+date+"\n- "+title+"\n\nClick OK to Delete.");
	if (conf)
	{
		var stamp = new Date().getMilliseconds();
		document.location="phys_censure.php?did="+id+"&jst="+stamp;
	}
}

function deleteSysSetting(id, name, type)
{
	var conf = confirm("Do you wish to delete the setting '"+name+"'?\n\nClick OK to Delete.");
	if (conf)
	{
		var stamp = new Date().getMilliseconds();
		document.location="sys_dropdown.php?ddltype="+type+"&did="+id+"&jst="+stamp;
	}
}

function showExportOptions(el, lyr, opt_el, opt_list)
{
//alert(el.name);
	var checkbox = new Array('fCb_1', 'fCb_2', 'fCb_3', 'fCb_4', 'fCb_5', 'fCb_6', 'fCb_7', 'fCb_8', 'fCb_9', 'fCb_10', 'fCb_11');
	var i;
	var el_name = "";

	if (el.checked)
	{
		showHideDiv(lyr,1)
		// uncheck all others.
		for (i=0; i<checkbox.length; i++)
		{
			if (el.name != checkbox[i])
			{
				unSelectTableOptions(el, opt_el, opt_list);
			}
		}
	} else {
		showHideDiv(lyr,0)

		// Then de-select options and clear the text layer.
		var selObj = document.getElementById(''+opt_el+'');
		
		for (i=0; i<selObj.options.length; i++) {
			selObj.options[i].selected = false;
		}
		document.getElementById(''+opt_list+'').innerHTML = "";
	}
}

function unSelectTableOptions(el, opt_el, opt_list)
{
	var checkbox = new Array('fCb_1', 'fCb_2', 'fCb_3', 'fCb_4', 'fCb_5', 'fCb_6', 'fCb_7', 'fCb_8', 'fCb_9', 'fCb_10', 'fCb_11');

	var lyr = new Array('lyrAddress', 'lyrCensures_cd', 'lyrCertifications', 'lyrConvictions', 'lyrDiscipline', 'lyrEducation', 'lyrFop', 'lyrMalpractice', 'lyrSuspension', 'lyrTraining', 'lyrDisciplines_cd');
	var i;
	var el_name = "";
	var lyr_name = "";
	var lyr_text = "";

	for (i=0; i<checkbox.length; i++)
	{
		if (el.name != checkbox[i])
		{
			el_name = checkbox[i];
			lyr_name = lyr[i];
			lyr_text = lyr_name+"_fields";
			document.getElementById(el_name).checked = false;
			showHideDiv(lyr_name,0);
		}
	}
}

function updateLive(label, type)
{
	var conf = confirm("Update 'LIVE' data records with '"+label+"' records.\n\n'Staging' data will be converted to 'Live' data.");
	if (conf)
	{
		var stamp = new Date().getMilliseconds();
		document.location="update_live.php?sect="+type+"&jst="+stamp;
	}
}

function updateSelectedFields(el, lyr)
{
	setMultipleSelectedLabels(el.name, lyr);
}

/////////////////////////////////
//   Project Specific - stop   //




//  General Functions - start  //
/////////////////////////////////

function clearOptions(el)
{
	el.options.length = 0;
}

function resetIframeSource(frameid, source, vis, height)
{
	parent.document.getElementById(''+frameid+'').src = source;
	parent.document.getElementById(''+frameid+'').style.display = vis;
	parent.document.getElementById(''+frameid+'').style.height = height;
}

function setIframeSource(frameid, source, vis, height)
{
	document.getElementById(''+frameid+'').src = source;
	document.getElementById(''+frameid+'').style.display = vis;
	document.getElementById(''+frameid+'').style.height = height;
}

function setParentIframeSource(frameid, source, vis, height)
{
	parent.document.getElementById(''+frameid+'').src = source;
	parent.document.getElementById(''+frameid+'').style.display = vis;
	parent.document.getElementById(''+frameid+'').style.height = height;
}

function showHideDiv(id,flagit)
{
    if (flagit == "1"){
		document.getElementById(''+id+'').style.display = "block";
	} else {
        document.getElementById(''+id+'').style.display = "none";
    }
}

function toggleDiv(id,flagit)
{
	if (flagit=="1")
	{
		if (document.layers) document.layers[''+id+''].visibility = "show"
		else if (document.all) document.all[''+id+''].style.visibility = "visible"
		else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
	}
	else if (flagit=="0")
	{
		if (document.layers) document.layers[''+id+''].visibility = "hide"
		else if (document.all) document.all[''+id+''].style.visibility = "hidden"
		else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
	} else { }
}


// Function to close help iframes.
function closeHelp(id)
{
    if (id == "PPL")
	{
		parent.document.getElementById('helpPPL').style.display="none";
	} else if (id == "PPL2") {
		parent.document.getElementById('helpPPL2').style.display="none";
    } else if (id == "NFNP") {
	    parent.document.getElementById('helpNFNP').style.display="none";
    } else if (id == "NFLP") {
	    parent.document.getElementById('helpNFLP').style.display="none";
    } else if (id == "NFRP") {
	    parent.document.getElementById('helpNFRP').style.display="none";
	} else {

	}
}

function getMultipleSelected(get_el, set_el)
{
	var selectedArray = new Array();
	var selObj = document.getElementById(''+get_el+'');
	var i;
	var count = 0;
	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			selectedArray[count] = selObj.options[i].value;
			count++;
		}
	}

	document.getElementById(''+set_el+'').value = selectedArray;
}

function returnMultipleSelectedValues(get_el)
{
	var selectedArray = new Array();
	var selObj = document.getElementById(''+get_el+'');
	var i;
	var count = 0;
	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			selectedArray[count] = selObj.options[i].value;
			count++;
		}
	}

	return selectedArray;
}

function setMultipleSelectedLabels(get_el, set_el)
{
	var selectedArray = new Array();
	var selObj = document.getElementById(''+get_el+'');
	var i;
	var count = 0;
	var val = "";
	var label = "";
	var selectAll = 0;
	var selectNone = 0;
	var err = "";

	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			val = selObj.options[i].value;
			label = selObj.options[i].text;
			if (val == '') { selectNone++; }
			if (val == '*') { selectAll++; }
			selectedArray[count] = " "+label;
			count++;
		}
	}

	if (count > 1)
	{
		if (selectAll > 0)
		{
			err += "When selecting the 'Select All Fields' option, do not select any others.\n";
		}
		if (selectNone > 0)
		{
			err += "When selecting the 'none' option, do not select any others.\n";
		}
	}

	if (err > '')
	{
		document.getElementById(''+set_el+'').innerHTML = '';
		alert(err);
	} else {
		if (selectedArray == ' none')
		{
			document.getElementById(''+set_el+'').innerHTML = "";
		} else {
			document.getElementById(''+set_el+'').innerHTML = selectedArray;
		}
	}
}

function setMultipleSelectedValues(get_el, set_el)
{
	var selectedArray = new Array();
	var selObj = document.getElementById(''+get_el+'');
	var i;
	var count = 0;
	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			selectedArray[count] = selObj.options[i].value;
			count++;
		}
	}

	document.getElementById(''+set_el+'').value = selectedArray;
}

// function to open help iframes.
function showHelp(id)
{
	if (id == "PPL")
	{
		document.getElementById('helpPPL').style.display="block";
	} else if (id == "PPL2") {
	    document.getElementById('helpPPL2').style.display="block";
	} else if (id == "NFNP") {
	    document.getElementById('helpNFNP').style.display="block";
    } else if (id == "NFLP") {
	    document.getElementById('helpNFLP').style.display="block";
    } else if (id == "NFRP") {
	    document.getElementById('helpNFRP').style.display="block";
	} else {
	
	}
}


function getCheckedValue(radioObj)
{
//alert(radioObj+":"+radioObj.value+":"+radioObj.length);
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function isNumeric(myVal)
{
	var validChars = "0123456789.";
   	var result = true;
   	var thisChar;

   	for (i = 0; i < myVal.length && result == true; i++)
    {
      	thisChar = myVal.charAt(i);
      	if (validChars.indexOf(thisChar) == -1)
        {
         	result = false;
        }
	}
   	return result;
}



function showErrors(err)
{
	alert(err);
}

function stopError()
{
	// supresses javascript errors.
  	return true;
}


function goToLast()
{
    document.location = history.back();
}

function goToPage(url)
{
	document.location = url;
	return false;
}


/////////////////////////////////
//  General Functions - stop   //
