// SPEC ENTITIES

function assign_spec_entity(eid, sid, checked) {
	sendRequest(ADMIN + "ajax.php?task=add_spec_entity&eid=" + eid + "&sid=" + sid + "&checked=" + checked);
}

function assign_spec_primary(eid, sid) {
	sendRequest(ADMIN + "ajax.php?task=assign_spec_primary&eid=" + eid + "&sid=" + sid);
}

function assign_sponsor_entity(eid, sid, checked) {
	sendRequest(ADMIN + "ajax.php?task=add_sponsor_entity&eid=" + eid + "&sid=" + sid + "&checked=" + checked);
}

function assign_rfp_entity(eid, rfp_id, checked) {
	sendRequest(ADMIN + "ajax.php?task=add_rfp_entity&eid=" + eid + "&rfp_id=" + rfp_id + "&checked=" + checked);
}

function assign_cust_entity(eid, cust_id, checked) {
	sendRequest(ADMIN + "ajax.php?task=add_cust_entity&eid=" + eid + "&cust_id=" + cust_id + "&checked=" + checked);
}

function get_kid_entities(eid, sid, level) {
	// clear cols after one displayed
	for( i=level; i<=4; i++ ) {
		this_lvl = $("e_lv_" + i);
		if( this_lvl )
			this_lvl.innerHTML = "";
	}
	// END clear cols

	sendRequest(ADMIN + "ajax.php?task=get_kids&eid=" + eid + "&sid=" + sid + "&lv=" + level);
	
	$("e_lv_" + level).innerHTML = http.responseText;
}

function get_sponsor_kid_entities(eid, sid, level) {
	// clear cols after one displayed
	for( i=level; i<=4; i++ ) {
		this_lvl = $("e_lv_" + i);
		if( this_lvl )
			this_lvl.innerHTML = "";
	}
	// END clear cols

	sendRequest(ADMIN + "ajax.php?task=get_spon_kids&eid=" + eid + "&sid=" + sid + "&lv=" + level);
	
	$("e_lv_" + level).innerHTML = http.responseText;
}

function get_rfp_kid_entities(eid, rfp_id, level) {
	// clear cols after one displayed
	for( i=level; i<=4; i++ ) {
		this_lvl = $("e_lv_" + i);
		if( this_lvl )
			this_lvl.innerHTML = "";
	}
	// END clear cols

	sendRequest(ADMIN + "ajax.php?task=get_rfp_kids&eid=" + eid + "&rfp_id=" + rfp_id + "&lv=" + level);
	
	$("e_lv_" + level).innerHTML = http.responseText;
}

function get_cust_kid_entities(eid, cust_id, level) {
	// clear cols after one displayed
	for( i=level; i<=4; i++ ) {
		this_lvl = $("e_lv_" + i);
		if( this_lvl )
			this_lvl.innerHTML = "";
	}
	// END clear cols

	sendRequest(ADMIN + "ajax.php?task=get_cust_kids&eid=" + eid + "&cust_id=" + cust_id + "&lv=" + level);
	
	$("e_lv_" + level).innerHTML = http.responseText;
}

function get_kid_entities_to_sponsor(eid) {
	$("kid_sponsor_list").innerHTML = "<p><img src=\"/images/ajax_circles.gif\" align=\"abscenter\" /> <b>Loading...</b></p>";

	http.onreadystatechange = process_get_kid_entities_to_sponsor;
	http.open("get", ROOT + "account-action.php?task=get_kids_to_spon&eid=" + eid, true);
	http.send(null);
}

function process_get_kid_entities_to_sponsor() {
	// $("kid_sponsor_list").innerHTML = response;
	if (http.readyState == 4 && http.status == 200) {
		$("kid_sponsor_list").innerHTML = http.responseText;
	}
}


function get_kid_entities_dropdown(parent_id) {
	var url = ADMIN + "ajax.php?task=get_kids_dropdown&eid=" + parent_id;
	sendRequest(url);
	var results = http.responseText;

	var kid_area_dropdown = $("kid_entity_area_dropdown");
	kid_area_dropdown.innerHTML = "";

	var new_option = document.createElement("option");
	new_option.value = "";
	var new_option_txt = document.createTextNode("- Choose a City -");
	new_option.appendChild(new_option_txt);
	kid_area_dropdown.appendChild(new_option);		

	var new_option = document.createElement("option");
	new_option.value = "";
	kid_area_dropdown.appendChild(new_option);		

	var row_arr = results.split("\n");
	for( var i = 0; i < row_arr.length; i++ ) {
		var row_vals = row_arr[i].split("|");
		var eid = row_vals[0];
		var entity_name = row_vals[1];
		
		var new_option = document.createElement("option");
		new_option.value = eid;
		var new_option_txt = document.createTextNode(entity_name);

		new_option.appendChild(new_option_txt);
		kid_area_dropdown.appendChild(new_option);		
	}

	$("kid_entity_area").className = "";
	// $("kid_entity_area").innerHTML = http.responseText;
}


// ENTITY NAV FUNCTIONS
function get_other_entities(eid, e_type, root) {
	var url = root + "account-action.php?task=get_other_entities&eid=" + eid + "&e_type=" + e_type;
	sendRequest(url);

	$("entity_type_" + e_type).innerHTML = http.responseText;
}


// CUSTOMER ACCOUNT FUNCTIONS
function get_security_ques() {
	// show "working" graphic

	var lname = $('forgot_lname').value;
	var eml = $('forgot_eml').value;

	var url = ROOT + "account-action.php?task=get_security_ques&lname=" + lname + "&eml=" + eml;
	sendRequest(url);
	
	var responseArr = http.responseText.split("||");

	switch(responseArr[0]) {
		case "success":
			$("forgot_pass_form").innerHTML = responseArr[1];
			$("forgot_ans").focus();
			break;

		case "error":
			alert(responseArr[1]);
			break;
	}
}

function set_new_password() {
	var lname = $('forgot_lname').value;
	var eml = $('forgot_eml').value;
	var ans = $('forgot_ans').value;
	
	var url = ROOT + "account-action.php?task=set_new_password&lname=" + lname + "&eml=" + eml + "&ans=" + ans;
	sendRequest(url);

	var responseArr = http.responseText.split("||");

	switch(responseArr[0]) {
		case "success":
			$("forgot_pass_form").innerHTML = responseArr[1];
			break;

		case "error":
			alert(responseArr[1]);
			break;
	}
}

function save_spec(sid) {
	var url = ROOT + "account-action.php?task=save_spec&sid=" + sid;
	sendRequest(url);

	var responseArr = http.responseText.split("||");
	switch(responseArr[0]) {
		case "success":
			$("save_" + sid).innerHTML = responseArr[1];
			break;
	}
}

function remove_saved_spec(sid) {
	if( confirm("You are about to remove this spec from your MySpecs list. Click OK to continue.\n\nNote: This action cannot be undone.") ) {
		// remove from db
		var url = ROOT + "account-action.php?task=remove_saved_spec&sid=" + sid;
		sendRequest(url);

		var response = http.responseText;
		if( response == "success" ) { // remove element from page
			var spec_row = $("spec_" + sid);
			var row_parent = spec_row.parentNode;
			row_parent.removeChild(spec_row);
		}
	}
}

function remove_spec_project() {
	return confirm('You are about to remove this project and its saved specs. Click OK to continue.\n\nNote: This action cannot be undone.');
}


function taskQuickAdd(entity_id) {
	// add in quick add area
	if( !$("quickadd_shade") && !$("quickadd_toolbox") ) {
		var pageBody = document.getElementsByTagName("body")[0];
		var firstNode = pageBody.firstChild;

		var shadeDiv = document.createElement("div");
		shadeDiv.id = "quickadd_shade";
		shadeDiv.style.height = pageBody.scrollHeight;

		var toolBox = document.createElement("div");
		toolBox.id = "quickadd_toolbox";

		toggleSelectVis();
		pageBody.appendChild(shadeDiv);
		pageBody.appendChild(toolBox);
	}
	
	var url = ADMIN + "ajax.php?task=task_quick_add" + ( entity_id != "" ? "&eid=" + entity_id : "" );
	sendRequest(url);
	$("quickadd_toolbox").innerHTML = http.responseText;
	$("quick_add_details").focus();

	init(); // re-init FAYT
}

function submitTaskQuickAdd() {
	// round up all the form data
	var task_details = $("quick_add_details").value;
	task_details = task_details.replace(/\n/g, "\\n");
	var task_assign = $("quick_add_assign").value;
	var task_due = $("quick_add_due").value;
	var task_entity = ( $("fayt_data_entity_quick") ? $("fayt_data_entity_quick").value : "" );

	var url = ADMIN + "ajax.php?task=task_quick_add_submit&details=" + task_details + "&assign_to=" + task_assign + "&due=" + task_due + "&entity_id=" + task_entity;
	sendRequest(url);

	if( http.responseText == "done" ) { // reload quick add form
		var url = ADMIN + "ajax.php?task=task_quick_add" + ( task_entity != "" ? "&eid=" + task_entity : "" );
		sendRequest(url);
		$("quickadd_toolbox").innerHTML = http.responseText;
		$("quick_add_details").focus();

		init(); // re-init FAYT
	}

	// refresh user task count
	if( $("task_counter") ) {
		sendRequest(ADMIN + "ajax.php?task=task_user_count");

		var task_count = http.responseText;
		$("task_counter").innerHTML = ( task_count > 0 ? "[ " + task_count + " ]" : "" );
	}
}

function closeTaskQuickAdd() {
	var pageBody = document.getElementsByTagName("body")[0];
	var shadeDiv = $("quickadd_shade");
	var toolBox = $("quickadd_toolbox");

	if( shadeDiv && toolBox) {
		// remove quick add from page
		pageBody.removeChild(toolBox);
		pageBody.removeChild(shadeDiv);

		// show <select> drop-downs on page
		toggleSelectVis();
	}
}

function emailSpecNotify(this_checkbox) {
	var notify_status = ( this_checkbox.checked ? 1 : 0 );

	var url = ROOT + "account-action.php?task=notify_update&spec_change_notify=" + notify_status;
	sendRequest(url);
}

function emailRFPNotify(this_checkbox) {
	var notify_status = ( this_checkbox.checked ? 1 : 0 );

	var url = ROOT + "account-action.php?task=notify_update&rfp_change_notify=" + notify_status;
	sendRequest(url);
}

function taskComplete(this_checkbox, task_id) {
	var task_id = task_id.split("_");
	task_id = task_id[1];

	// check if task checked or unchecked
	var task_status = this_checkbox.checked;

	if( task_status ) { // tasked checked as done
		// mark task as "done" in db
		var url = ADMIN + "ajax.php?task=task_done&tid=" + task_id;
		sendRequest(url);
		
		// if successful, remove task row from screen
		var response_arr = http.responseText.split("|");
		var response = response_arr[0];
		var task_count = response_arr[1];
		if( response == "success" ) {
			var task_row = $("task_" + task_id);

			var row_parent = task_row.parentNode;
			row_parent.removeChild(task_row);
			
			// update task counter in menu
			if( $("task_counter") ) {
				$("task_counter").innerHTML = ( task_count > 0 ? "[ " +  task_count + " ]" : "" );
			}

			// reload completed task table
			sendRequest(ADMIN + "ajax.php?task=task_refresh_completed");
			var task_complete_list = http.responseText;
			$("completed_tasks").innerHTML = task_complete_list;
		}
	} else {
	}
}

function taskDelete(task_id) {
	if( confirm("You are about to delete this task from your list. Click OK to continue.\n\nNote: This action cannot be undone.") ) {
		// remove from db
		var url = ADMIN + "ajax.php?task=task_delete&tid=" + task_id;
		sendRequest(url);

		var response_arr = http.responseText.split("|");
		var response = response_arr[0];
		var task_count = response_arr[1];
		if( response == "success" ) { // remove element from page
			var task_row = $("task_" + task_id);

			var row_parent = task_row.parentNode;
			row_parent.removeChild(task_row);

			// update task counter in menu
			if( $("task_counter") ) {
				$("task_counter").innerHTML = ( task_count > 0 ? "[ " +  task_count + " ]" : "" );
			}

		}
	}
}


function newContactHistoryItem() {
	// check for required fields
	if( $("contact_history_note").value != "" ) {
		var new_note = $("contact_history_note").value;

		// add note to db
		var url = ADMIN + "ajax.php?task=new_contact_history_item&note=" + $("contact_history_note").value + "&eid=" + $("ctc_hist_entity_id").value;
		sendRequest(url);

		// refresh this entity's history list
		sendRequest(ADMIN + "ajax.php?task=refresh_contact_history&eid=" + $("ctc_hist_entity_id").value + "&all=" + $("ctc_hist_all").value);
		$("entity_contact_history").innerHTML = http.responseText;

		init();
	} else { // show error message
		alert("Please enter a contact note.");
	}

	return false;
}

function contactHistoryPage(eid, pg_num) {
	var url = ADMIN + "ajax.php?task=contact_history_page&eid=" + eid + "&pg=" + pg_num;
	sendRequest(url);
	$("entity_contact_history").innerHTML = http.responseText;
}

function contactHistoryAll(eid) {
	var url = ADMIN + "ajax.php?task=contact_history_all&eid=" + eid;
	sendRequest(url);
	$("entity_contact_history").innerHTML = http.responseText;
}


function remove_rfp_entity_track(id) {
	// remove from db
	var url = ROOT + "account-action.php?task=remove_rfp_entity_track&id=" + id;
	sendRequest(url);

	var response = http.responseText;
	if( response == "success" ) { // remove element from page
		var entity_row = $("rec_" + id);
		entity_row.style.display = "none";
	}
}


function show_rfp_remind_edit(remind_id) {
	var url = ROOT + "account-action.php?task=show_rfp_remind_edit&remind_id=" + remind_id;
	sendRequest(url);
	$("remind_edit_" + remind_id).innerHTML = http.responseText;
	hide_show("remind_edit_" + remind_id);
}


function geo_location_lookup(contractor_id) {
	var radius = $("geo_radius").value;
	var zip = $("zip").value;

	if( radius != "" && zip != "" ) {
		var url = ADMIN + "ajax.php?task=geo_location_lookup&radius=" + radius + "&zip=" + zip + "&contractor_id=" + contractor_id;
		sendRequest(url)
	
		$("geo_listing").innerHTML = http.responseText;
		if( $("view_selected_link") )
			$("view_selected_link").innerHTML = "<a href=\"javascript: toggle_vis('service_area|view_selected_link');\">View Selected Cities</a>";
	} else {
		$("geo_listing").innerHTML = "";
		if( zip == "" )
			if( $("view_selected_link") ) $("view_selected_link").innerHTML = "";
			$("geo_listing").innerHTML = "<i class=\"lowlite\">(please enter a zip code above)</i>";
	}
}



// HELPER FUNCTIONS

if($ === undefined) {
	var $ = function(element_id) {
		return document.getElementById(element_id);
	}
}

var http = createRequest();

function createRequest() {
	var obj;

	if(window.XMLHttpRequest) {
  		obj = new XMLHttpRequest();
  	}
  	else if(window.ActiveXObject) { // for IE
		try {
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		}
  	}

    return obj;
}

function sendRequest(page) {
	http.open("get", page, false);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function handleResponse() {
	if(http.readyState == 4 && http.status == 200){
		// this is the content of the called page
		var response = http.responseText;
		// alert(response);
    }
}

function toggleSelectVis() {
	var allSelects = document.getElementsByTagName("select");
	if( allSelects.length > 0 ) {
		for( var i=0; i<allSelects.length; i++) {
			var thisSelect = allSelects[i];

			if( thisSelect.className.indexOf("no_vis") == -1 )
				thisSelect.className += " no_vis";
			else
				thisSelect.className = thisSelect.className.replace(/no_vis/i, "");
		}
	}
}
