ajax = function() { this.ajax_file = "/admin/includes/ajax_processing.php"; //this.SITE_RELATIVE_WEB_ROOT = _SITE_RELATIVE_WEB_ROOT; //this.SITE_ROOT = _SITE_ROOT; this.BASE_PATH = ""; this.CONTENT_PATH = ""; 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 = ""; this.table_def = new Array(); this.message_ids = 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 the function that performs the ajax operations */ ajax.prototype.connect = function(callback, url, loading_id) { //dhtml.show_loading(); xmlhttp = this.getxml_obj(this.ajax_file); xmlhttp.onreadystatechange = function () { if (window.xmlhttp) { if (xmlhttp.readyState == 4) { if (callback.handleSuccess != undefined) { callback.handleSuccess(xmlhttp); } //dhtml.hide_loading(); loading_id = document.getElementById(loading_id) if (loading_id) { loading_id.style.display = "none"; } } } } loadind_id = document.getElementById(loading_id); if (loading_id) { loading_id.style.display = "block"; } url += "&running_ajax=yes"; xmlhttp.send(url); } /* 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 will call a function from inside the CMS class then return the data from that function in html format class_name = the class the function is in func_name = the function you are calling params = an array of parameters to pass to the function required = an array of functions that this function needs */ ajax.prototype.get_function_data = function(class_name, func_name, params, required, callback) { url = "action=get_function_data&class_name="+class_name+"&func_name="+func_name+"&func_params="+params+"&required="+required+"&html_data=true"; this.connect(callback, url); } /* This function is used to retrieve data from the database */ ajax.prototype.get_data = function(table, func, id, url_add) { url = "id="+id+"&table="+table+"&get_data=yes"; if (url_add != undefined) { url += url_add; } this.connect(func, url); } /* This function is used to retrieve data from the database */ ajax.prototype.get_data_set = function(table, func, id, id_name, url_add) { if (id_name == "" || id_name == undefined) { id_name = "id"; } url = "id="+id+"&table="+table+"&get_data_set=yes&id_name="+id_name; if (url_add != undefined) { url += url_add; } this.connect(func, url); } /* This function is for getting the data from the given field */ ajax.prototype.get_field_data = function(field, type) { if (type == "date") { month = document.getElementById("month_"+field).value; day = document.getElementById("day_"+field).value; year = document.getElementById("year_"+field).value; return year+"-"+month+"-"+day; } else if (type == "editor") { try { fck = document.getElementById(field+"___Frame"); doc = fck.contentWindow.FCK.EditorWindow.document; return doc.body.innerHTML; } catch(e) { return false; } } else if (type == "list") { select = document.getElementById(field); index = select.selectedIndex; text = select.options[index].value; return text; } else { return document.getElementById(field).value; } } /* Get the xmlhttp object for ajax processing */ ajax.prototype.getxml_obj = function(file) { if (window.XMLHttpRequest){ var xmlhttp = new XMLHttpRequest(); } else if(window.ActiveXObject) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { } } } xmlhttp.open('POST', file, true); xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); return xmlhttp; } /* This function inserts data into the given database using ajax values has the form values = name=location,ordering=5 .... */ ajax.prototype.insert_data = function(table, values, callback, url_add) { values = this.arr_to_str(values, "", "", ""); url="table="+table+"&insert_data=yes&values="+values; if (url_add != undefined) { url_add = this.arr_to_str(url_add, "&", "=", "&"); url += url_add; } this.connect(callback, url); } /* This function get the fo */ ajax.prototype.get_folders = function() { } //End the loader ajax.prototype.load_end = function() { YAHOO.example.container.wait.hide(); } //Start the loader ajax.prototype.load_start = function() { YAHOO.example.container.wait.show(); } /* This function will continuouly retrieve the given messages from the database */ ajax.prototype.get_message_track = function(message_key, container, self) { if (self == undefined) { self = this; } conditions = new Array(); conditions["message_key"] = message_key; conditions = self.arr_to_str(conditions); url = "id=all&table=MESSAGE_TRACK&get_data_set=yes&data_filter="+conditions; callback = { handleSuccess:function(xmlhttp) { arr = self.xml_set_to_array(xmlhttp); for (x in arr) { id = arr[x]["id"]; if (self.message_ids[id] == undefined) { message = arr[x]["message"]; if (message != undefined) { dhtml.alert(arr[x]["message"], "", {"rows":50, "cols":130}); } self.message_ids[id] = ""; } } } }; self.connect(callback, url); if (self.interval == undefined) { self.interval = setInterval(self.get_message_track, 10000, message_key, container, self); } } /* This function populates all of the given fields */ ajax.prototype.populate_fields = function(o) { list = this.xml_to_array(o); for (field in this.table_def) { type = this.table_def[field]["type"] ; data = list[field]; if (!this.populate_field(field, data, type)) { this.reset_form(); break; } } this.editor_state = "done"; } ajax.prototype.populate_fields_from_db = function(table, table_def, id, id_name) { } /* This function populates all of the given fields */ ajax.prototype.populate_field = function(field, data, type) { if (data == undefined) { return false; } data = data.toString(); if (type == "editor") { try { fck = document.getElementById(field+"___Frame"); doc = fck.contentWindow.FCK.EditorWindow.document; doc.body.innerHTML = data; } catch(e) { if (this.editor_state != "loading") { alert("Editor Not Fully Loaded\nPlease wait until done") } this.editor_state = "loading"; return false; } } else if (type == "list") { select = document.getElementById(field); if (data == "" || data == undefined) { select.selectedIndex = 0; } else { found = false; for (x=0; x 20) { data = data.substr(0, 20)+"..."; } //cell.innerHTML = data; }*/ return true; } /* This function uses ajax to Save data */ 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; } 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 object handles the ajax for making a get request */ ajax.prototype.get_ajax_obj = { ajax:this, handleSuccess:function(o){ // This member handles the success response // and passes the response object o to AjaxObject's // processResult member. this.ajax.populate_fields(o); this.ajax.process_state = "populate_done"; this.ajax.check_state(); this.ajax.set_fck_image_path(); }, handleFailure:function(o){ // Failure handler alert("failed"); }, startRequest:function(id) { this.ajax.process_state = "populating"; this.ajax.check_state(); this.ajax.save_id = id; url = "get_data=true&running_ajax=true&table="+this.ajax.table+"&id="+this.ajax.save_id; this.ajax.image_base_path = "/giveaways/images/"; this.ajax.image_path = this.ajax.image_base_path+this.ajax.save_id+"/"; url += "&file_path_action=create_folder&base_path="+this.ajax.SITE_ROOT+"/"+this.ajax.image_base_path+"&main_folder="+this.ajax.save_id /* * This is an example callback object with success * and failure members defined inline. The argument * property demonstrates how to pass multiple values * to the callback as an array. */ callback = { success: this.ajax.get_ajax_obj.handleSuccess, failure: this.ajax.get_ajax_obj.handleFailure, argument: ["one", "two", "three"] } YAHOO.util.Connect.asyncRequest('POST', '/admin/includes/ajax_processing.php', callback, url); } } /* This object handles the ajax for Saving data */ ajax.prototype.create_ajax_obj = { ajax:this, handleSuccess:function(o){ // This member handles the success response // and passes the response object o to AjaxObject's // processResult member. this.ajax.populate_fields(o) }, handleFailure:function(o){ // Failure handler }, startRequest:function() { this.ajax.process_state = "creating"; this.ajax.check_state(); url = "insert_data=true&running_ajax=true&table="+this.ajax.table; vals = ""; for (field in this.ajax.table_def) { type = this.ajax.table_def[field]["type"]; if (vals != "") { vals += ",,,,,,,"; } data = this.ajax.get_field_data(field, type); data = encodeURIComponent(data); //data = escape(data); if (type == "folder") { str = "&file_path_action=create_folder&main_folder="+data+"&sub_folders=images&base_path="+this.ajax.BASE_PATH+"&filename=index.php&content_path="+this.ajax.CONTENT_PATH; url += str; } vals += field+"="+data; } callback = { success: this.ajax.save_ajax_obj.handleSuccess, failure: this.ajax.save_ajax_obj.handleFailure, argument: ["one", "two", "three"] } url += "&values="+vals; if (this.ajax.check_form()) { YAHOO.util.Connect.asyncRequest('POST', this.ajax.SITE_RELATIVE_WEB_ROOT+'admin/includes/ajax_processing.php', callback, url); } else { this.ajax.process_state = "create_error"; this.ajax.check_state(); } } } 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 object handles the ajax for Saving data */ ajax.prototype.save_ajax_obj = { ajax:this, handleSuccess:function(o){ // This member handles the success response // and passes the response object o to AjaxObject's // processResult member. xml = this.ajax.xml_to_array(o); if (xml["complete"] == "sucess") { this.ajax.process_state = "save_done"; this.ajax.check_state(); this.ajax.populate_fields(o) } else { this.ajax.save_ajax_obj.handleFailure(o); } }, handleFailure:function(o){ // Failure handler //alert(o.responseText); this.ajax.process_state = "save_error"; this.ajax.check_state(); }, startRequest:function() { this.ajax.process_state = "saving"; this.ajax.check_state(); url = "update_field=true&running_ajax=true&table="+this.ajax.table+"&id="+this.ajax.save_id; vals = ""; for (field in this.ajax.table_def) { type = this.ajax.table_def[field]["type"]; if (vals != "") { vals += ",,,,,,,"; } data = this.ajax.get_field_data(field, type); data = encodeURIComponent(data); //data = escape(data); if (type == "folder") { url += "&file_path_action=update_folder&main_folder="+data+"&sub_folders=images&base_path="+this.ajax.BASE_PATH; url += "&id="+this.ajax.save_id+"&table="+this.ajax.table+"&field="+field+"&filename=index.php&content_path="+this.ajax.CONTENT_PATH; } //data = data.replace(/(\%u[0-9A-Z]+)/g, encodeURI("$1")); vals += field+"="+data; } callback = { success: this.ajax.save_ajax_obj.handleSuccess, failure: this.ajax.save_ajax_obj.handleFailure, argument: ["one", "two", "three"] } url += "&values="+vals; url += "&test=yes"; if (this.ajax.check_form()) { YAHOO.util.Connect.asyncRequest('POST', this.ajax.SITE_RELATIVE_WEB_ROOT+'admin/includes/ajax_processing.php', callback, url); } else { this.ajax.process_state = "save_error"; this.ajax.check_state(); } } } /* 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 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