//<?php//
/**
 * ajax.js - AJAX Callback functions
 *
 * This file contains AJAX Callback functions
 *
 * @filesource
 */
 
/**
 * function refresh_drop_down_cb()
 *
 * This functions refreshes the the specified drop down
 *
 * - drop_down HTML markup of the new drop down
 * - id HTML element id in which the drop_down resides
 * - opener Boolean, true - if the drop_down exists in the opening window, false - if the drop_down exists in the current window
 * @param string Delimited as drop_down§id§opener
 * @see refreshDropDown()
 */

function refresh_drop_down_cb(args){
	
	var array = args.split("§");
	
	var drop_down = array[0];
	var id = array[1];
	var opener = array[2];
	
	if(opener = 'true'){
		window.opener.document.getElementById(id).innerHTML = drop_down;
	}else{
		document.getElementById(id).innerHTML = drop_down;
	}
}

function add_link_cb(html){
	var tbl = document.getElementById('link_table');
	lastRow = tbl.rows.length;
	// Get the last row
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	var row = tbl.insertRow(lastRow);
	var td = document.createElement("td");
	
	document.getElementById('link_' + lastRow_id).appendChild(td);
	document.getElementById('link_' + lastRow_id).firstChild.innerHTML = html;
	row.setAttribute('id', 'link_' + nextRow_id);
}

function add_link(){
	// Get the last row
	var tbl = document.getElementById('link_table');
	lastRow = tbl.rows.length;
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	x_addLink(lastRow_id,add_link_cb);	
}

function remove_link(i){
  
  var tbl = document.getElementById('link_table');
  var row = document.getElementById('link_' + i);
  var rowIndex = row.sectionRowIndex;
  
  if(i > 1){
  	tbl.deleteRow(rowIndex);
  }
}

function inline_module_form(name,module,action){
	show_progress(module);
	//
	selected_id = document.getElementById(name).value;
	//
	x_inlineModuleForm(module,selected_id,action,inline_module_form_cb);
}

function inline_module_form_cb(args){
	name = args[0];
	module = args[1];
	html = args[2];
	//
	if(html == 'delete'){
		x_saveInlineModuleForm(module,'','','delete',save_inline_module_form_cb);
	}else{
		document.getElementById(module + '_div_in_place').innerHTML = html;
	}
}

function save_inline_module_form(name,module,selected_id,value,action){
	show_progress(module);
	//
	x_saveInlineModuleForm(module,selected_id,value,action,save_inline_module_form_cb);
}

function save_inline_module_form_cb(args){
	name = args[0];
	module = args[1];
	html = args[2];
	status = args[3];
	//
	document.getElementById(module + '_div').innerHTML = html;
	//
	hide_progress(module);
}

function filter_mls(value){
	show_progress('mls');
	x_filterMLS(value,filter_mls_cb);
}

function filter_mls_cb(args){
	value = args[0];
	status = args[1];
	//
	if(status == true){
		document.getElementById('mls_div_in_place').innerHTML = 'Match Found';	
		$(document.getElementById('mls_row')).highlightFade({color:'#99CC99',end:'#E2F9E3',speed:1000,iterator:'sinusoidal'})
		document.getElementById('listings_id').value = value;
	}else{	
		document.getElementById('mls_div_in_place').innerHTML = 'No Match Found';
		$(document.getElementById('mls_row')).highlightFade({color:'#CC9999',end:'#FFCCCC',speed:1000,iterator:'sinusoidal'})
	}
}

function show_progress(module){
	document.getElementById(module + '_div_in_place').innerHTML = '<img src="/admin/images/progress_blue.gif" />';	
}

function hide_progress(module){
	document.getElementById(module + '_div_in_place').innerHTML = '';	
}

function add_document(){
	// Get the last row
	var tbl = document.getElementById('document_table');
	lastRow = tbl.rows.length;
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	x_addDocument(lastRow_id,add_document_cb);	
}

function add_document_cb(html){
	var tbl = document.getElementById('document_table');
	lastRow = tbl.rows.length;
	// Get the last row
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	var row = tbl.insertRow(lastRow);
	var td = document.createElement("td");
	
	document.getElementById('document_' + lastRow_id).appendChild(td);
	document.getElementById('document_' + lastRow_id).firstChild.innerHTML = html;
	row.setAttribute('id', 'document_' + nextRow_id);
}

function add_image(){
	// Get the last row
	var tbl = document.getElementById('image_table');
	lastRow = tbl.rows.length;
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	x_addImage(lastRow_id,add_image_cb);	
}

function add_image_cb(html){
	var tbl = document.getElementById('image_table');
	lastRow = tbl.rows.length;
	// Get the last row
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	var row = tbl.insertRow(lastRow);
	var td = document.createElement("td");
	
	document.getElementById('image_' + lastRow_id).appendChild(td);
	document.getElementById('image_' + lastRow_id).firstChild.innerHTML = html;
	row.setAttribute('id', 'image_' + nextRow_id);
}

function add_file(){
	// Get the last row
	var tbl = document.getElementById('file_table');
	lastRow = tbl.rows.length;
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	x_addFile(lastRow_id,add_file_cb);	
}

function add_file_cb(html){
	var tbl = document.getElementById('file_table');
	lastRow = tbl.rows.length;
	// Get the last row
	var finalRow = tbl.rows[lastRow - 1];
	finalRow_id = finalRow.getAttribute('id');
	finalRow_array = finalRow_id.split("_");
	lastRow_id = finalRow_array[1];
	nextRow_id = parseInt(lastRow_id) + 1;
	//
	var row = tbl.insertRow(lastRow);
	var td = document.createElement("td");
	
	document.getElementById('file_' + lastRow_id).appendChild(td);
	document.getElementById('file_' + lastRow_id).firstChild.innerHTML = html;
	row.setAttribute('id', 'file_' + nextRow_id);
}
//?>//