ajax = function()
{
this.folder = new Array();
this.folder["original"] = "";
this.folder["new"] = "";
this.folder["field"] = "";
this.process_state = "initial";
this.editor_state = "initial";
this.save_id = "NONE";
this.table_def = new Array();
this.message_ids = new Array();
this.xmlhttp_arrays = new Array();
this.xmlhttp_count = 0;
this.ajax_cache_track = new Array();
};
ajax.prototype.init = function() {
this.save_bttn = document.getElementById("save_bttn");
this.create_bttn = document.getElementById("create_bttn");
this.clear_bttn = document.getElementById("clear_bttn");
this.create_bttn.ajax = this;
this.create_bttn.onclick = function() {
this.ajax.create_ajax_obj.startRequest()
}
this.save_bttn.ajax = this;
this.save_bttn.onclick = function() {
this.ajax.save_ajax_obj.startRequest()
}
this.clear_bttn.ajax = this;
this.clear_bttn.onclick = function() {
this.ajax.reset_form();
this.ajax.check_state();
}
this.check_state();
}
//This is run when ajax is called
ajax.prototype.ajax_start = function() {
$("#loading").show();
$(document).mousemove(function(e){
$("#loading").css({
top: e.pageY + "px",
left: e.pageX + "px"
});
});
}
//This is run when ajax is called
ajax.prototype.ajax_stop = function() {
$(document).unbind("mousemove");
$("#loading").hide();
}
//This function returns the node given the id
ajax.prototype.byid = function(id) {
if (document.getElementById(id)) {
return document.getElementById(id);
}
}
/*
This function will call one of your class functions that simply returns true or false on completion
class_name = the class the function is in
func_name = the function in the class
attributes = a comma separated list of attributes to pass
data_attributes = an string of the actual data attribues
ex. if attributes = "id,table"
data_attributes = "&id=34&table=your table
class_variables = these are variables declared in classes. Here is the format to use to get table def from the home page class and set it to table_def
class_variables = "table_def=home_page->main_flash_feature_table_def",
callback = object to handle the ajax requests
*/
ajax.prototype.call_process_function = function(class_name, func_name, attributes, data_attributes, class_variables, callback) {
url = "ajax_action=call_process_function&class_name="+class_name+"&func_name="+func_name+"&attributes="+attributes+"&class_variables="+class_variables;
if (data_attributes != "" && data_attributes != undefined) {
url += "&"+data_attributes;
}
if (callback == undefined || callback == "") {
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
alert(xmlhttp.responseText);
}
}
this.connect(callback, url);
}
ajax.prototype.change_state = function(id, id_name, callback) {
}
/*
This function uses ajax to change states. This will also update the current viewable state
*/
ajax.prototype.change_active_state = function(table, id, name, db_id, callback, url_add) {
if (byid("processing_"+id)) {
byid("processing_"+id).style.display = "";
}
if (callback == undefined || callback == "") {
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
results = ajax.xml_to_array(xmlhttp);
//alert(xmlhttp.responseText);
if (results["complete"] == "error") {
alert("Status not changed!!\nPlease reload the page and try again");
return false;
}
else {
if (byid("processing_"+id)) {
byid("processing_"+id).style.display = "none";
}
alert("Status changed!!");
return true;
}
if (byid("processing_"+id)) {
byid("processing_"+id).style.display = "none";
}
return false;
}
}
if (name == "" || name == undefined) {
name = "published";
}
url="table="+table+"&id="+id+"&name="+name+"&change_active_state=yes";
if (db_id == "" || db_id == undefined) {
db_id='id';
}
url += "&db_id="+db_id;
if (url_add != "" && url_add != undefined) {
url += url_add;
}
this.connect(callback, url);
return false;
}
/*
This function uses ajax to change states from a dropdown.
*/
ajax.prototype.change_active_drop_down_state = function(table, id, id_name, field_name, field_value, values_order, high_order_value, db, callback, conditions) {
if (callback == undefined || callback == "") {
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
alert(xmlhttp.responseText);
}
}
url = "action=change_active_drop_down_state&table="+table+"&id="+id+"&field_name="+field_name+"&field_value="+field_value+"&values_order="+values_order;
if (db != undefined) {
url += "&db="+db;
}
if (id_name != undefined) {
url += "&id_name="+id_name;
}
else {
url += "&id_name=id";
}
if (high_order_value != undefined) {
url += "&high_order_value="+high_order_value;
}
else {
url += "&id_name=id";
}
if (conditions != undefined) {
url += "&conditions="+conditions;
}
this.connect(callback, url);
}
/*
This is the function that performs the ajax operations
*/
ajax.prototype.connect = function(callback, url, loading_id, cache) {
if (ajax.ajax_cache_track[url] == undefined) {
ajax.ajax_cache_track[url] = new Object();
}
ajax.ajax_cache_track[url].success = function(self) {
//ajax.ajax_cache_track[url].xmlhttp = self;
//alert(xmlhttp.responseText);
if (callback.handleSuccess != undefined) {
callback.handleSuccess(ajax.ajax_cache_track[url].xmlhttp);
}
//dhtml.hide_loading();
loading_id = document.getElementById(loading_id)
if (loading_id) {
loading_id.style.display = "none";
}
ajax.ajax_stop();
};
//dhtml.show_loading();
ajax.ajax_cache_track[url].xmlhttp = this.getxml_obj(this.ajax_file);
this.ajax_start();
ajax.ajax_cache_track[url].xmlhttp.onreadystatechange = function () {
//if (window.xmlhttp) {
if (ajax.ajax_cache_track[url].xmlhttp.readyState == 4) {
ajax.ajax_cache_track[url].success(this)
ajax.ajax_cache_track[url].cache = true;
}
//}
}
loadind_id = document.getElementById(loading_id);
if (loading_id) {
loading_id.style.display = "block";
}
this.current_url = url;
if (cache) {
if (this.ajax_cache_track[url].cache == undefined) {
ajax.ajax_cache_track[url].xmlhttp.send(url);
}
else {
ajax.ajax_cache_track[url].success(this.ajax_cache_track[url].xmlhttp);
}
}
else {
ajax.ajax_cache_track[url].xmlhttp.send(url);
}
}
/*
This function will take add an item to a relational secodary table
*/
ajax.prototype.add_secondary_item = function(table, primary_id, primary_field, table_id, form_items, insert_pos, sort, url_add, container_id) {
form_array = cms.convert_str_array(form_items);
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
//alert(xmlhttp.responseText);
if (container_id != undefined) {
if (container_id != undefined) {
//$("#"+container_id).html(xmlhttp.responseText);
my_container = byid(container_id);
my_container.innerHTML = xmlhttp.responseText;
}
forms.clear_display_form(table, form_items);
}
else {
results = this.xml_to_array(xmlhttp);
//html = ajax_get_html_data_list(db, form_items)
tbl = byid(table_id);
if (insert_pos == "top") {
tr = tbl.insertRow(1);
}
else {
tr = document.createElement("tr");
}
new_id = results["id"];
tr.id = table+"_row_"+new_id;
for (field in form_array) {
field_id = form_array[field]["form_id"];
type = form_array[field]["type"];
td = document.createElement("td");
td.align = "center";
td.innerHTML = results[field];
tr.appendChild(td);
if (type == "text" || type=="date" || type == "link" || type == "youtube") {
byid(field_id).value = "";
}
}
td = document.createElement("td");
td.align = "center";
td.innerHTML = "Edit";
tr.appendChild(td)
td = document.createElement("td");
td.align = "center";
td.innerHTML = "remove";
tr.appendChild(td)
if (sort != "date") {
td = document.createElement("td");
td.align = "center";
td.innerHTML = "up";
td.innerHTML += "
down";
tr.appendChild(td)
}
if (insert_pos == "bottom") {
tbl.appendChild(tr);
}
}
}
//id = byid("youtube_id").value;
values = "";
//values = "youtube_id="+id+",,,,,,,id="+id;
errors = "";
for (field in form_array) {
field_id = form_array[field]["form_id"];
type = form_array[field]["type"];
name = form_array[field]["name"];
required = form_array[field]["required"];
if (type == "constant") {
}
else if (!byid(field_id)) {
continue;
}
if (values != "") {
values += ",,,,,,,";
}
if (type == "constant") {
values += field+"="+form_array[field]["value"];
}
else if ( type == "youtube" || type == "text" || type=="date" || type=="link" || type=="region" || type=="list" || byid(field_id).type=="select-multiple") {
if (byid(field_id).type == "select-one") {
temp_val = byid(field_id).options[byid(field_id).selectedIndex].value;
}
else if (byid(field_id).type == "select-multiple") {
temp_val = forms.get_multiple_select_vals(byid(field_id), ",");
}
else {
temp_val = unescape(byid(field_id).value);
}
temp_val = unescape(temp_val);
temp_val = escape(temp_val);
values += field+"="+temp_val;
//alert(field+"="+temp_val+", required="+required);
if (byid(field_id).type == "select-one" && byid(field_id).selectedIndex == 0 && required == "yes") {
errors += "Please enter a value for \""+name+'"\n';
}
else if (byid(field_id).value == "" && required == "yes") {
errors += "Please enter a value for \""+name+'"\n';
}
}
}
if (errors != "") {
alert(errors);
return false;
}
if (primary_field != "") {
values += ",,,,,,,"+primary_field+"="+primary_id;
}
if (url_add != undefined) {
values += url_add;
}
values += "&form_track="+form_items;
this.insert_data(table, values, callback);
return false;
}
/*
This function will convert a string to an array to be used in the corresponding php
*/
ajax.prototype.arr_to_str = function(arr, separator, equal, lead_off) {
if (typeof(arr) == "string") {
return arr;
}
if (separator == undefined || separator == "") {
separator = ",,,,,,,";
}
if (equal == undefined || equal == "") {
equal = "=";
}
values_str = "";
for (field in arr) {
if (values_str != "") {
values_str += separator;
}
else if (lead_off != undefined && lead_off != "") {
values_str += lead_off;
}
val = arr[field];
//val = val.replace(/(\n|\r)/g, "
");
//val = val.replace(/"/, escape('"'));
//val = val.replace(/'/, escape("'"));
//val = val.replace(/&/, escape("&"));
values_str += field+equal+encodeURIComponent(val);
}
return values_str;
}
/*
This function checks to make sure the given form has everything that it needs to continue
*/
ajax.prototype.check_form = function() {
str = "";
for (field in this.table_def) {
type = this.table_def[field]["type"] ;
name = this.table_def[field]["name"];
required = this.table_def[field]["required"];
data = this.get_field_data(field, type);
if (data == "" && required != "") {
str += "Please Enter "+name+"\n";
}
}
if (str != "") {
alert(str);
return false;
}
else {
return true;
}
}
/*
This function allows you to perform operations based on the current state
*/
ajax.prototype.check_state = function() {
if (this.process_state == "saving") {
this.save_bttn.value = "Saving...";
this.save_bttn.disabled = true;
this.load_start();
}
else if (this.process_state == "save_error") {
this.save_bttn.value = "Save";
this.save_bttn.disabled = false;
alert("Error Saving!!\nPlease try again!")
this.load_end();
}
else if (this.process_state == "save_done") {
this.save_bttn.value = "Save";
this.save_bttn.disabled = false;
this.load_end();
}
else if (this.process_state == "creating") {
this.create_bttn.value = "Creating...";
this.create_bttn.disabled = true;
this.load_start();
}
else if (this.process_state == "create_error") {
this.create_bttn.value = "Create";
this.create_bttn.disabled = false;
alert("Error Creating!!\nPlease try again!")
this.reset_form();
this.load_end();
}
else if (this.process_state == "create_done") {
this.create_bttn.value = "Done";
this.create_bttn.disabled = true;
this.load_end();
}
else if (this.process_state == "populating") {
this.load_start();
}
else if (this.process_state == "populate_done") {
this.load_end();
this.save_bttn.value = "Save";
this.save_bttn.disabled = false;
this.create_bttn.value = "Create";
this.create_bttn.disabled = true;
}
else {
this.save_bttn.value = "Save";
this.save_bttn.disabled = true;
this.create_bttn.value = "Create";
this.create_bttn.disabled = false;
}
}
/*
This function edits an item in the database list
*/
ajax.prototype.edit_item = function(table, id, field_str, url_add, container_id) {
forms.clear_display_form(table, field_str);
var val_arr = cms.convert_str_array(field_str);
var val_array = val_arr;
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
//alert(xmlhttp.responseText);
xml_arr = ajax.xml_to_array(xmlhttp);
for (field in val_arr) {
if (val_arr[field]["type"] == "constant") {
continue;
}
if (byid(table+"_add")) {
byid(table+"_add").style.display = "none";
}
if (byid(table+"_save")) {
values = "";
byid(table+"_save").style.display = "";
byid(table+"_save").onclick = function() {
for (x in val_arr) {
//Do not need to access database values for constants
if (val_arr[x]["type"] == "constant") {
continue;
}
form_id = val_arr[x]["form_id"];
value = byid(val_arr[x]["form_id"]).value;
if (byid(val_arr[x]["form_id"]).type == "select-multiple") {
value = forms.get_multiple_select_vals(byid(val_arr[x]["form_id"]), ",");
}
if (values != "") {
values += ",,,,,,,";
}
values += x+"="+escape(value);
}
ajax.save_data(table, id, values, "", url_add+"&html_data=true&data_refresh=true", container_id)
forms.clear_display_form(table, field_str);
}
}
if (val_array[field]["type"] == "region" || byid(val_array[field]["form_id"]) == "select-one") {
for (y=0; y 20) {
data = data.substr(0, 20)+"...";
}
//cell.innerHTML = data;
}*/
return true;
}
/*
This function takes an id and removes the value from the database
it will also remove the row if the id is given
*/
ajax.prototype.remove_field = function(table, id, node_id, callback, type, url_add, container_id) {
if (callback == undefined || callback == "") {
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
//alert("id="+container_id+":"+xmlhttp.responseText);
if (container_id != undefined) {
//$("#"+container_id).html("");
//$("#"+container_id).html(xmlhttp.responseText);
var my_cont = "";
my_cont = document.getElementById(container_id);
my_cont.innerHTML = xmlhttp.responseText;
}
else {
node = document.getElementById(node_id);
if (type == "" || type == undefined) {
ajax.remove_row(node_id);
}
else {
ajax.remove_node(node);
}
}
}
}
url="table="+table+"&id="+id+"&remove_row=yes";
if (url_add != undefined) {
url += url_add;
}
if (confirm("Are you sure you want to Remove this item?")) {
this.connect(callback, url);
}
return false;
}
/*
This function will remove most nodes
*/
ajax.prototype.remove_node = function(node) {
node.parentNode.removeChild(node);
}
/*
Give this function a table row id and it will remove it
*/
ajax.prototype.remove_row = function(id) {
row = document.getElementById(id);
rowindex = row.rowIndex;
table = row.parentNode;
table.deleteRow(rowindex);
}
/*
This function reorders the items in the table database
and in the actual table
*/
ajax.prototype.reorder = function(table, id, direction, row_id, callback, conditions, url_add, container_id) {
if (callback == undefined || callback == "") {
callback = new Object();
callback.handleSuccess = function(xmlhttp){
//alert(xmlhttp.responseText);
if (container_id != undefined) {
//$("#"+container_id).html(xmlhttp.responseText);
my_container = byid(container_id);
my_container.innerHTML = xmlhttp.responseText;
}
else {
xml = ajax.xml_to_array(xmlhttp);
if (xml["result"] == "success") {
if (byid(row_id)) {
row = byid(row_id);
table = row.parentNode;
clone = row.cloneNode(true);
if (direction == "up") {
//insert_point = row.previousSibling.rowIndex;
insert_point = row.rowIndex-1;
if (insert_point < 0) {
return false;
}
}
else {
if (!row.nextSibling) {
return false;
}
//insert_point = row.nextSibling.rowIndex;
insert_point = row.rowIndex+1;
}
if (insert_point == undefined) {
return false;
}
current_row = row;
new_row = table.rows[insert_point];
table.innerHTML = "";
alert("1");
return true;
table.deleteRow(row.rowIndex);
new_row = table.insertRow(insert_point);
new_row.innerHTML = clone.innerHTML;
new_row.id = clone.id;
new_row.style.backgroundColor = "#FFFFFF";
}
}
else {
//alert(xmlhttp.responseText);
//alert("Ordering not complete!!");
}
}
}
}
url="table="+table+"&id="+id+"&direction="+direction+"&reorder=yes&conditions="+conditions;
if (url_add != undefined) {
url += url_add;
}
this.connect(callback, url);
return false;
}
/*
This function reorders the items in the table database
and in the actual table
*/
ajax.prototype.reorder_from_string = function(table, order_string, main_name, main_id, url_add) {
url="table="+table+"&order_string="+order_string+"&reorder_from_string=yes&main_name="+main_name+"&main_id="+main_id;
if (url_add != undefined && url_add != "") {
url += url_add;
}
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
xml = ajax.xml_to_array(xmlhttp);
if (xml["result"] != "success") {
//alert(xmlhttp.responseText);
alert("ordering not complete");
}
}
this.connect(callback, url);
return false;
}
/*
This function uses ajax to Save data
table = the table you are making changes to
id = the id you are updating
callback functions to handle the ajax processing.
.handleSuccess
*/
ajax.prototype.save_data = function(table, id, values, callback, url_add, container_id, id_name, loading_id) {
values_str = this.arr_to_str(values, "", "", "");
url="table="+table+"&id="+id+"&values="+values_str+"&update_field=yes";
if (url_add != undefined) {
url_add = this.arr_to_str(url_add, "&", "=", "&");
url += url_add;
}
if (id_name != undefined && id_name != "") {
url += "&id_name="+id_name;
}
if (callback == undefined || callback == "") {
callback = new Object();
callback.handleSuccess = function(xmlhttp){
//alert(xmlhttp.responseText);
if (container_id != undefined && container_id != "") {
//$("#"+container_id).html(xmlhttp.responseText);
my_container = ajax.byid(container_id);
my_container.innerHTML = xmlhttp.responseText;
}
//alert("Save Complete!!");
}
}
this.connect(callback, url, loading_id);
}
/*
This function uses ajax to Save data
*/
ajax.prototype.save_data_set = function(table, id, update_field, values, callback, conditions, url_add, container_id, id_name, loading_id) {
values_str = this.arr_to_str(values, "", "", "");
url="table="+table+"&id="+id+"&values="+values_str+"&action=save_data_set&update_field="+update_field+"&conditions="+conditions;
if (url_add != undefined) {
url_add = this.arr_to_str(url_add, "&", "=", "&");
url += url_add;
}
if (id_name != undefined && id_name != "") {
url += "&id_name="+id_name;
}
this.connect(callback, url, loading_id);
}
/*
This function updates the image path for the fck editors
*/
ajax.prototype.set_fck_image_path = function() {
for (field in this.table_def) {
type = this.table_def[field]["type"];
//Check to see if this is an fckeditor field
if (type == "editor") {
fck = document.getElementById(field+"___Frame");
oEditor = fck.contentWindow.FCK;
this.image_path = "/giveaways/images/"+this.save_id+"/";
oEditor.BasePath = "/admin/mambots/editors/fckeditor/";
oEditor.Config["LinkBrowserURL"] = oEditor.BasePath + "editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&FolderPath="+this.image_path+"&ServerPath="+this.image_path;
oEditor.Config["ImageBrowserURL"] = oEditor.BasePath + "editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&FolderPath="+this.image_path+"&ServerPath="+this.image_path;
}
}
}
/*
This function allows you to set session variables through javascript
*/
ajax.prototype.set_session_var = function(name, value, callback) {
if (callback == undefined) {
callback = new Object();
callback.handleSuccess = function(data) {
alert(data.responseText);
}
}
url = "action=set_session_var&name="+name+"&value="+value;
this.connect(callback, url);
}
ajax.prototype.reset_form = function() {
this.save_id = "NONE";
this.process_state = "initial";
type = this.table_def[field]["type"];
for (field in this.table_def) {
this.populate_field(field, "", this.table_def[field]["type"]);
}
return true;
}
/*
This function gives you the ability to basically communicate with another window using the
database as the base to store the data
*/
ajax.prototype.track_message = function(message_key, message, db) {
arr = new Array();
arr["message_key"] = message_key;
arr["message"] = message;
value = this.arr_to_str(arr);
url = "table=MESSAGE_TRACK&action=insert_data&values="+values;
callback = {};
this.connect(callback, url);
}
/*
This function takes a container and replaces it with the given results
*/
ajax.prototype.container_reload = function(container_id, url) {
//container = document.getElementById(container_id);
callback = new Object();
callback.handleSuccess = function(xmlhttp) {
//alert(xmlhttp.responseText);
//alert("container id = "+container_id);
my_container = ajax.byid(container_id);
my_container.innerHTML = xmlhttp.responseText;
//$("#"+container_id).html(xmlhttp.responseText);
if (run_cleanup_script != undefined) {
run_cleanup_script();
}
}
//container.innerHTML = "";
this.connect(callback, url);
}
/*
This function takes care of cleaning up the page after an image upload
removes the iframe, refreshes the tables
*/
ajax.prototype.upload_cleanup = function(replace_id, display_btn_id, container_id, url_add) {
if (byid(replace_id) && byid("img_placeholder")) {
src = $("#img_placeholder").attr("src");
//Remove previous instance of stamp
src = src.replace(/\?stamp=.+/, "");
//Use a time stamp to make sure browsers reload the image
dtime = new Date()
src += "?stamp="+dtime.getTime();
//img = new Image();
//img.src = byid("img_placeholder").src;
byid(replace_id).style.display = "block";
//byid(replace_id).src = byid("img_placeholder").src;
$("#"+replace_id).attr({"src":src});
this.remove_node(byid("img_placeholder"));
}
if (byid(display_btn_id)) {
byid(display_btn_id).style.display = "";
}
if (container_id != "") {
this.container_reload(container_id, url_add);
}
return false;
}
/*
This function takes the given xml data and converts it to an array
*/
ajax.prototype.xml_to_array = function(xmlhttp) {
xml_array = new Array();
xml = xmlhttp.responseXML.getElementsByTagName('result')[0];
if (xml == null) {
return Array();
}
for (x=0; x