if (!window.ccs.ugc) { ccs.ugc = {}; // package declaration } ccs.ugc.path = '/shared/content/ugc_content/'; ccs.ugc.jsonpath = ccs.contextPath + "/json/" + ccs.appName + "/" + ccs.languageCode + "/content/ugc_content/"; ccs.ugc.capturejsonpath = ccs.ugc.jsonpath + "capture/"; ccs.ugc.Capture = function() { var self; var umessages; //store custom user messages per instance var formValidationErrors; //store custom validation messages per instance, object Hash where oid is the key and an array of errors the value var preValidationState; //object to store the state of the input form before it was validated and styles were changed. This is so that it can reset the form var multiSubs; var sectionId; var divToShowOnCapture; var hideMeOnCapture; // constructor function function constructorFn() { self = this; umessages = new Object(); //registerOnLoad(self.init); formValidationErrors = new Object(); preValidationState = new Object(); } // init function called once document is loaded constructorFn.prototype.init = function(_multiSubs, _sectionId) { self.multiSubs = _multiSubs; self.sectionId = _sectionId; } //clear any previous validation highlights/states etc constructorFn.prototype.clearValidationState = function(oid) { var preValidationStateArr = preValidationState[oid+""]; if (preValidationStateArr != null) { for (var i = 0; i < preValidationStateArr.length; i++) { var preObj = preValidationStateArr[i]; //$("ugcAttrRow_" + preObj.formFieldId).style.backgroundColor = preObj.bgcolor; $("ugcValidationMsg_" + preObj.formFieldId).innerHTML = ""; } preValidationState[oid+""] = new Array(); } } //validate input, return true if no errors else false //for now we just simply highlight the error and display an inline message //we can make it more dunamic and configurable per app if need be constructorFn.prototype.validate = function(oid) { self.clearValidationState(oid); var noErrors = true; var arrErrors = formValidationErrors[oid + ""]; if (arrErrors != null && arrErrors.length > 0) { //highlight errors etc alert("Please check the form for errors."); var errs = ""; var preValidationStateArr = preValidationState[oid+""]; if (preValidationStateArr == null) { preValidationStateArr = new Array(); preValidationState[oid+""] = preValidationStateArr; } for (i = 0; i < arrErrors.length; i++) { errs += "\n" + arrErrors[i].fieldName; var preObj = new Object(); preObj.bgcolor = $("ugcAttrRow_" + arrErrors[i].formFieldId).style.backgroundColor; preObj.formFieldId = arrErrors[i].formFieldId; //$("ugcAttrRow_" + arrErrors[i].formFieldId).style.backgroundColor = "yellow"; if (arrErrors[i].errorSuffix) { $("ugcValidationMsg_" + arrErrors[i].formFieldId).innerHTML = "* " + arrErrors[i].errorSuffix; } preValidationStateArr.push(preObj); } formValidationErrors[oid + ""] = new Array(); noErrors = false; } return noErrors; } //makes an ajax request to check if the user us authenitcated or not constructorFn.prototype.checkAuth = function (oid, jsonUpMapping) { new Ajax.Request(ccs.ugc.jsonpath + "isauth.json", { method:'post', onSuccess: function(transport) { var res = transport.responseText.evalJSON(); if (res.authed) { $("ugcCaptureFormAuthed_" + oid).style.display = "block"; //now we fetch the additional user info we may need self.userInfo(oid, jsonUpMapping); } else { if ($("ugcCaptureFormNotAuthed_" + oid)) { $("ugcCaptureFormNotAuthed_" + oid).style.display = "block"; } } }, onFailure: function(transport) { var err = transport.responseText.evalJSON(); if (err != null && err.error) { alert(err.message); } else { alert("Error speaking to server:" + transport.responseText); } } }); } //makes an ajax request to fetch the session user's info and prepopulated the capture fields constructorFn.prototype.userInfo = function (oid, jsonUpMapping) { new Ajax.Request(ccs.ugc.jsonpath + "user_info.json", { method:'post', onSuccess: function(transport) { var res = transport.responseText.evalJSON(); if (res.error) { alert("Error:" + res.message); } //now we fetch the additional user info we may need //atempt to popuplate the fields fields = res.fields; for (var i=0; i 0) { try {eval(validation);} catch (e) { alert(e); } } if (self.validate(oid)) { self.showLoader(oid); var pars = new Object(); pars = Form.serializeElements(Form.getElements(theForm), true); if (isCookiesEnabled()) { var rec = getCookie("ugc_" + oid); pars["ugc_rec"] = rec; pars["section_id"] = self.sectionId; new Ajax.Request(ccs.ugc.capturejsonpath + "capture.json", { method:'post', parameters: pars, onSuccess: function(transport) { self.showSubmit(oid); var ret = transport.responseText.evalJSON(); if (ret.error) { alert(ret.message); } else { self.captured(pars, oid, theForm, hideForm, publishImmediately); if (ret.msg.length > 0) { //non live site debug msg alert(ret.msg); } } }, onFailure: function(transport) { self.showSubmit(oid); var err = transport.responseText.evalJSON(); if (err != null && err.error) { alert(err.message); } else { alert("Error speaking to server:" + transport.responseText); } } }); } else { $("ugcMessage_" + oid).innerHTML = "Sorry, Your browser does not support cookies. " + "We use cookies as part of a mechanism to control certain spam bots"; } } } //called after the form is submitted. // We need to now either constructorFn.prototype.captured = function (pars, oid, theForm, hideForm, publishImmediately) { if (hideForm) { //we replace the form contents with the thankyou message contents $("ugcCaptureForm_" + oid).innerHTML = self.getMessageThanks(oid); } else { $("ugcMessage_" + oid).innerHTML = self.getMessageThanks(oid); } var period; if (self.multiSubs=='\'\'' || self.multiSubs=="always") { //not sure what causes the '' period = -10000; } else if (self.multiSubs=="never") { period = 300 * 24 * 60 * 60 * 1000; } else if (self.multiSubs=="session") { } else if (self.multiSubs=="day") { period = 1 * 24 * 60 * 60 * 1000; } else if (self.multiSubs=="week") { period = 7 * 24 * 60 * 60 * 1000; } var expire = null; if (self.multiSubs!="session") { expire = new Date(); var dateBuffy = expire.getTime() + period; expire.setTime(dateBuffy); } setCookie("ugc_" + oid, "ugc", expire); //if we have an add template we display the item if ($("ugcAddTemplate_" + oid)) { var d = new Date(); var curr_hour = d.getHours(); var curr_min = d.getMinutes(); pars["added_at"] = curr_hour + " : " + curr_min; t = cambrient.processTemplate($("ugcAddTemplate_" + oid).value , pars); //add a div to the placeholder - we can't simply replace as they may add multiple newDiv = document.createElement("DIV"); newDiv.innerHTML = t; $("ugcContentAdd_" + oid).appendChild(newDiv); } //call backs to refresh data on either graph or listing //this could be done in a more oo way, this aint too pretty //but the problem here is that the capture tag could have appeared before the ugc tag and vice versa //which means I can't assume one exists to hook into... but this could be fixed with a third "callback manager" type class // we only bother to refresh the list IF it is a moderated list if ($("ugcGraphData_" + oid)) { eval("ccs.ugc.graph" + oid + ".loadGraphData()"); } if (publishImmediately) { if ($("ugcList_" + oid)) { eval("ccs.ugc.list" + oid + ".refreshList()"); } } if (divToShowOnCapture != null && divToShowOnCapture.length > 0) { $(divToShowOnCapture).style.display = "block"; $(divToShowOnCapture).style.visibility = "visible"; } if (hideMeOnCapture) { $("ugcOuterCapture_" + oid).style.display = "none"; } theForm.reset(); } constructorFn.prototype.checkCanSubmit = function (oid, theForm) { var c = getCookie("ugc_" + oid); if (c==null) { return true; } else { return false; } } constructorFn.prototype.showSubmit = function (oid) { //hide loader $("ugcLoader_" + oid).style.visibility = "hidden"; $("ugcLoader_" + oid).style.display = "none"; //show submit again $("ugcSubmit_" + oid).style.display = "block"; $("ugcSubmit_" + oid).style.visibility = "visible"; } constructorFn.prototype.addMessageThanks = function (oid, msg) { umessages["instance_" + oid] = msg; } constructorFn.prototype.showDivOnCapture = function (divId) { divToShowOnCapture = divId; } constructorFn.prototype.hideMeOnCapture = function (hideMe) { hideMeOnCapture = hideMe; } constructorFn.prototype.getMessageThanks = function (oid) { return umessages["instance_" + oid]; } constructorFn.prototype.showLoader = function (oid) { //hide loader $("ugcLoader_" + oid).style.visibility = "visible"; $("ugcLoader_" + oid).style.display = "block"; //show submit again $("ugcSubmit_" + oid).style.display = "none"; $("ugcSubmit_" + oid).style.visibility = "hidden"; } constructorFn.prototype.addValidationError = function (oid, formFieldName, formFieldId, errorSuffix) { //hide loader var myErr = new Object(); myErr.fieldName = formFieldName; myErr.formFieldId = formFieldId; myErr.errorSuffix = errorSuffix; //is mandatory / is not a valid email address etc var arrErrors = formValidationErrors[oid+""]; if (arrErrors == null) { arrErrors = new Array(); } arrErrors.push(myErr); formValidationErrors[oid+""] = arrErrors; } return new constructorFn(); }