// site.js
// part of mysql_data_manager
// this script contains the main/general javascript functions

// very basic check on login form that both fields have been filled
function check_login() {
	if (document.forms['login_form'].elements['email_address'].value=="" || document.forms['login_form'].elements['password'].value==""){
		alert("Please enter both your email address and password to log in");
		return false;	
	}
return true;
}

// function for showing and hiding divs on the multi option pages
function show_hide_pagedivs(visible_id){
	for (i=1;i<8;i++){
	if (i != visible_id){
	eval('document.getElementById("page' + i + '").style.visibility="hidden"')
	} else {
	eval('document.getElementById("page' + i + '").style.visibility="visible"')
	}
	}
}

// extra precaution when deleting rows
function deleterow(rowid,child_records){
	continueDelete=0;
	if(confirm("Are you sure you want to delete this record?\n\nThis action is not undoable.")){
		if (child_records){
			if(confirm("Would you like to delete all child records of this master record?")){
				continueDelete=1;
			} else {
				continueDelete=0;
			}
		} else {
				continueDelete=1;	
		}
		if (continueDelete){
			document.forms['deleterow'].elements['deleteID'].value=rowid;
			document.forms['deleterow'].submit();	
		}
	}
	continueDelete=0;
}

// toggle the tinyMCE Rich Text Editor to act on and not act on a textarea. Call this function with the id of a textarea
function toggleEditor(id) {
if (!tinyMCE.get(id))
tinyMCE.execCommand('mceAddControl', false, id);
else
tinyMCE.execCommand('mceRemoveControl', false, id);
}

// the old editor toggle function for tiny mce version 2.x
var tinyMCEmode = true; // older code for tinyMCE2.0
function toggleEditorMode(sEditorID) {
    try {
        if(tinyMCEmode) {
            tinyMCE.removeMCEControl(tinyMCE.getEditorId(sEditorID));
            tinyMCEmode = false;
	    rewriteDiv = "toggle_" + sEditorID;
	    rewriteUrl = "<a style=\"font-size:9px;\" href=\"Javascript:toggleEditorMode('" + sEditorID + "')\">Style Editor</a>";
	    dynamiccontentNS6(rewriteDiv,rewriteUrl);
        } else {
            tinyMCE.addMCEControl(document.getElementById(sEditorID), sEditorID);
            tinyMCEmode = true;
	    rewriteDiv = "toggle_" + sEditorID;
	    rewriteUrl = "<a style=\"font-size:9px;\" href=\"Javascript:toggleEditorMode('" + sEditorID + "')\">Source Editor</a>";
	    dynamiccontentNS6(rewriteDiv,rewriteUrl);
        }
    } catch(e) {
       	alert("An error has occured: " + e); 
    }
}

function dynamiccontentNS6(elementid,content){
if (document.getElementById && !document.all){
rng = document.createRange();
el = document.getElementById(elementid);
rng.setStartBefore(el);
htmlFrag = rng.createContextualFragment(content);
while (el.hasChildNodes())
el.removeChild(el.lastChild);
el.appendChild(htmlFrag);
}
}

// Ajax methods
function ajaxFunction(ajaxVar,ajaxVarVal,fieldsInto){
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4)
      {
      returnText = parseAjaxResponse(xmlHttp.responseText,fieldsInto);
      } else {
	returnText="";
	}
    }
  
  urlStr = "ajax.php?" + ajaxVar + "=" + ajaxVarVal
  xmlHttp.open("GET",urlStr,false);
  xmlHttp.send(null);
  }

function ajaxLoadTableFields(fieldsInto,initial_select_element){
	selectedTable=document.forms['query_builder'].elements[initial_select_element].value
	ajaxResult=ajaxFunction("table",selectedTable,fieldsInto);
	//alert("Table fields loaded. Press ok to continue");
	return ajaxResult;
}

function parseAjaxResponse(returnedText,fieldsInto){
	//alert(returnedText + " into " + fieldsInto);
	document.forms[0].elements[fieldsInto].value=returnedText;
}

function options_from_csv(allCsvs,selectFieldName){
	document.query_builder.elements[selectFieldName].options.length=0;
	returnString="";
	var splitList=allCsvs.split(":");
	for (i=0;i<splitList.length;i++){
		document.query_builder.elements[selectFieldName].options[i]=new Option(splitList[i], splitList[i], true, false)
	}
}

function query_builder_select_options(destination_element,source_element,initial_select_element){
	ajaxLoadTableFields(source_element,initial_select_element);
	var csvVars="";
	do {
	csvVars=document.forms['query_builder'].elements[source_element].value;
	//if (!csvVars){
		alert("Loading.. please click to continue");
	//}
	} while (csvVars=="");
	options_from_csv(csvVars,destination_element);	
	document.forms['query_builder'].elements[source_element].value="";
}

function ajaxGetDependents(which_field){
	//alert(which_field);
	var ajaxResult=ajaxFunction("getDependentFields",which_field,"carrier_field");
	//alert(ajaxResult);
}

function next_page(){
//	document.forms['list_records_filter'].elements['dbf_next'].value=eval((document.forms['list_records_filter'].elements['dbf_next'].value)+10);
	document.forms['list_records_filter'].elements['dbf_direction'].value="Up";
	document.forms['list_records_filter'].target="_self";
	document.forms['list_records_filter'].submit();
}

function previous_page(){
	document.forms['list_records_filter'].elements['dbf_direction'].value="Down";
	document.forms['list_records_filter'].target="_self";
	document.forms['list_records_filter'].submit();
}

function display_items() {
	document.forms['list_records_filter'].elements['dbf_direction'].value="Static";
	document.forms['list_records_filter'].elements['dbf_output_type'].value="";
	document.forms['list_records_filter'].target="_self";
	document.forms['list_records_filter'].submit();
}

function items_as_excel() {
	document.forms['list_records_filter'].elements['dbf_direction'].value="Static";
	document.forms['list_records_filter'].elements['dbf_output_type'].value="excel";
	document.forms['list_records_filter'].target="_blank";
	document.forms['list_records_filter'].submit();
}

function show_sql(){
	if (sql != ""){
		alert("This recordset was generated with the following sql:\n\n" + sql);
	}
}
function search_data(){
	document.forms['list_records_filter'].elements['dbf_direction'].value="";
	document.forms['list_records_filter'].elements['dbf_next'].value="";
	document.forms['list_records_filter'].elements['dbf_output_type'].value="";
	document.forms['list_records_filter'].target="_self";
	document.forms['list_records_filter'].submit();
}

function clear_all_filtering(){	
	if(confirm("Set all search and paging filtering to default values?")){
	document.forms['list_records_filter'].elements['dbf_direction'].value="Static";
	document.forms['list_records_filter'].elements['dbf_search_fields'].value="";
	if (document.forms['list_records_filter'].elements['dbf_data_filter_field']){
	document.forms['list_records_filter'].elements['dbf_data_filter_field'].value="";
	}
	if (document.forms['list_records_filter'].elements['dbf_data_filter_value']){
	document.forms['list_records_filter'].elements['dbf_data_filter_value'].value="";
	}
	if (document.forms['list_records_filter'].elements['dbf_data_filter_operator']){
	document.forms['list_records_filter'].elements['dbf_data_filter_operator'].value="";
	}
	document.forms['list_records_filter'].elements['dbf_search_for'].value="";
	document.forms['list_records_filter'].submit();	
	}
}

function edit_current_recordset(edit_recordset_url){
	document.forms['list_records_filter'].action=edit_recordset_url;
	document.forms['list_records_filter'].elements['dbf_direction'].value="Static";
	document.forms['list_records_filter'].submit();
}

var interfaceItem = new Array();
var interfaceDefs = new Array();
var interfaceTypeAssocs = new Array();
var interfaceValues = new Array();
var interfaceSelectedValue = new Array();

function clearField(filter_id, fieldname){
	newHTML="";
	document.getElementById(fieldname).innerHTML=newHTML;
	document.getElementById(fieldname).style.display="inline";
}

function showNextField(filter_id, fieldname, div_to_update, formFieldName){
	
	//alert("showNextField called with\n\n" + filter_id + "\n" + fieldname + "\n" + div_to_update + "\n" + formFieldName);
	
	var interfaceArray = new Array();
	interfaceArray = interfaceItem[filter_id];
	newHTML="<table class=\"form_table\" style=\"background-color:#f1f1f1\" bgcolor=\"#f1f1f1\">";
	origFormFieldName=formFieldName;
	formFieldName=fieldname + "_-_" + formFieldName; 
	for (var i=0; i<interfaceArray.length; i++){

		if (interfaceTypeAssocs[filter_id][i] == document.forms['add_interface_form'].elements[formFieldName].value || document.forms['add_interface_form'].elements[formFieldName].value=="ALL"){


			// does it have a value
			getVarName="existing___" + fieldname + "___" + origFormFieldName;
				//alert("Lets see if theres a var called " + getVarName);
			if (eval("typeof("+getVarName+")") != "undefined"){
				//alert(getVarName + " EXISTS");
			}
			if (!interfaceValues[filter_id][i]){
				newHTML += "<tr><td align=\"right\">" + interfaceArray[i] + ": </td><td><input type=\"text\" name=\"" + fieldname + "_-_" + interfaceArray[i] + "\" value = \"\"></td><td><span class=\"helptip\">" + interfaceDefs[filter_id][i] + "</span></td></tr>";
			} else {
				newHTML += "<tr><td align=\"right\">" + interfaceArray[i] + ": </td><td><select name name=\"" + fieldname + "_-_" + interfaceArray[i] + "\" >";

				var splitList=interfaceValues[filter_id][i].split(",");
				for (n=0;n<splitList.length;n++){
					listvalue=splitList[n];
					listtext=splitList[n];
					if (splitList[n].match(";;")){
						var splitOption=splitList[n].split(";;");
						listvalue=splitOption[0];
						listtext=splitOption[1];
					}
					newHTML += "<option value=\""+listvalue+"\">"+listtext+"</option>";	
				}
				
				newHTML += "</option></select></td><td><span class=\"helptip\">" + interfaceDefs[filter_id][i] + "</span></td></tr>";

			}	
		} else {
		}
	}
	newHTML += "</table>";
	//newHTML = "<select name=\"\">";
	//newHTML += "<option value=\"\">";
	//newHTML += "</option>";
	//newHTML += "</select>";	
	document.getElementById(div_to_update).innerHTML=newHTML;
	document.getElementById(div_to_update).style.display="inline";
}

// function which uses the javascript location to call a new page, written as i was already using too many quotes (" and ') and adding the location inline was getting too confusing
function goToUrl(toWhichURL){
	location=toWhichURL;
}

function showPreviewBlock(){
	document.getElementById('section_preview').style.display="block";
}



function deletePhoto(sFile,sDirectory,sDisplayOptions,sOptionsPosition){
	if(confirm("Are you sure you want to delete this photograph?")){
		locstring="administrator.php?action=file_browser&dt=" + sFile + "&d=" + sDirectory + " & display_options=" + sDisplayOptions + "&options_position=" + sOptionsPosition;
		location=locstring;
	}
}

function checkSurvey(){

  var radio_choice = false;

  for (counter = 0; counter < document.forms['surveyform'].response.length; counter++){
  if (document.forms['surveyform'].response[counter].checked)
  radio_choice = true; 
  }

  if (!radio_choice){
  alert("Please select a result before submitting your result - thanks!");
  } else {
  document.forms['surveyform'].submit();
  }
}

function fillOfficeFields(){
	selectedOffice=document.forms['add_product_to_cart'].elements['selectoffice'].options[document.forms.add_product_to_cart.elements.selectoffice.selectedIndex].value;
	officeData=offices_matrix[selectedOffice];
	officeDataArray=officeData.split("|");
	document.forms['add_product_to_cart'].elements['Business_Name'].value=officeDataArray[2];
	document.forms['add_product_to_cart'].elements['Division'].value=officeDataArray[3];
	document.forms['add_product_to_cart'].elements['Building_Name'].value=officeDataArray[4];
	document.forms['add_product_to_cart'].elements['Address'].value=officeDataArray[5];
	document.forms['add_product_to_cart'].elements['Town'].value=officeDataArray[6];
	document.forms['add_product_to_cart'].elements['Zip/Postcode'].value=officeDataArray[7];
	document.forms['add_product_to_cart'].elements['Country'].value=officeDataArray[8];
	document.forms['add_product_to_cart'].elements['Telephone'].value=officeDataArray[9];
	document.forms['add_product_to_cart'].elements['Direct_Telephone'].value=officeDataArray[12];
	document.forms['add_product_to_cart'].elements['Fax'].value=officeDataArray[10];
	document.forms['add_product_to_cart'].elements['Mobile'].value=officeDataArray[13];
	document.forms['add_product_to_cart'].elements['Email_Address'].value=officeDataArray[14];
	document.forms['add_product_to_cart'].elements['Web_Address'].value=officeDataArray[11];
	document.forms['add_product_to_cart'].elements['Job_Title'].value=officeDataArray[16];
	document.forms['add_product_to_cart'].elements['Name'].value=officeDataArray[15];
}

function deleteOfficeAssociation(o_id,u_id,lookup){
	if(confirm("Are you sure you want to remove this office from this users list?")){
		document.forms['remove_user_from_office'].elements['office_user'].value=lookup;	
		document.forms['remove_user_from_office'].elements['user_id'].value=u_id;	
		document.forms['remove_user_from_office'].elements['office_id'].value=o_id	
		document.forms['remove_user_from_office'].submit();
	}
}

function popUpDate(){
	document.forms['list_records_filter'].elements['active_date_filter'].value=1;
	document.getElementById("dateSearchPopup").style.display="block";
}

function popUpAZ(){
	document.forms['list_records_filter'].elements['active_az_filter'].value=1;
	document.getElementById("azSearchPopup").style.display="block";
}

function ajax_populate_field(fieldname,currentValue,currentName,querytype,rowid,filterid){
	//alert("Populating field " + fieldname + " from " + currentName);
	var populate_field_options=new Array();
	var populate_field_values=new Array();
	var url="administrator.php?action=ajax_generate_options_list&top_value=" + currentValue + "&jx=1&querytype=" + querytype + "&fieldname=" + fieldname + "&filter=" + filterid;
	ajax_list_options=new sack();
	ajax_list_options.requestFile=url;
	ajax_list_options.onCompletion=function(){ 
		// line below needs to differentiate between add and edit, so test for new_ or id_x to work out the correct field name (will need to use the value of x if it is edit)
		if (currentName.match("new_")){
			fieldname = "new_" + fieldname;	
		}
		if (currentName.match("id_")){
			old_fieldname=fieldname;
			fieldname = "id_" + rowid + "_";
			fieldname = fieldname + old_fieldname; 
		}
		//alert(ajax_list_options.response);
		var splitList=ajax_list_options.response.split(":::");
		// remove options
		  var elSel = document.getElementById(fieldname);
		  var i;
		  for (i = elSel.length - 1; i>=0; i--) {
		      elSel.remove(i);
		  }
		splitList[0]=splitList[0].replace("<p><b></b></p>","");
		for (i=0;i<splitList.length;i++){
			document.forms['update_table'].elements[fieldname].options[i]=new Option(splitList[i], splitList[i], true, false)
		}


	}	
	ajax_list_options.runAJAX();
	//ajax_showOptions(this,'t=$pass_table&idf=$pass_id_field&kf=$pass_key_field&jx=1&ajax_populate_dynamic_list',event)	
}

