/* * Name: * captchaLibrary.js * * Description: * Random generated a string of 5 char * * Pre-conditions: * * Post-conditions: * A random 5 char string is generted and display the flash image * * Log: * Ramu 06/22/2007 * - Creation * Kripa Shenai 07/19/2007 * - Made it into a common file - Adapted from Forum */ //Global variable for ajax request var captchaXMLHttpRequestObj = false; var captchaGlobalStnBoxId = ""; var onSuccessFunction = ""; // // following request will be generated from the calling file. // function callCaptcha(stnBoxId,successFunction) { // // get the object of the display div and text box. // objRandomImageAndTextDiv = document.getElementById("randomImageAndTextDiv_"+stnBoxId); objForumVerification = document.getElementById("varification_captcha_"+stnBoxId); // assign function to global function. onSuccessFunction = successFunction; captchaGlobalStnBoxId = stnBoxId; // If RandomImage And Text div's style is none change it to block and don't display Title And CommentDiv if(objRandomImageAndTextDiv.style.display == "none"){ // // generate query string and send to generate captcha code. // url="/custom_code/generateAndCheckCaptchaOnServer.php"; // create query string queryStr= "?stnBoxId="+escape(stnBoxId)+"&generateCode=1"; // define the operation. operation = "generateCode"; // // call for calling server side script. // requestForCaptcha(url,queryStr,operation); return false; }// end if, objRandomImageAndTextDiv display property checking // start else to check the varification code. else if(trim(objForumVerification.value) != '') { // // generate query string and send to generate captcha code. // url="/custom_code/generateAndCheckCaptchaOnServer.php"; // create query string queryStr= "?stnBoxId="+escape(stnBoxId)+"&validateCode=1&validator="+objForumVerification.value; // // change the operation name to varification. // operation = 'varification' // // call for calling server side script. // requestForCaptcha(url,queryStr,operation); return false; }// end else to check the varification code. else { alert("Please enter the code shown to complete the submit process"); return false; } }// end function callCaptcha /* * Name: * requestForCaptcha * * Description: * call php file for generating and validation captcha request. * define the handler for the captcha also. - if the operation is "generateCode" : handler just return string and will display captcha block - else if the operation is "varification" : handler -(on wrong varification) return false to user and display error message on error div + give new captcha string. -(on success varification) - just return true and call the front-end application user javascript : any script from the ( form, poll, image-gallary, poll...etc.) * Pre-conditions: * url - PHP file path * queryStr - Query String * * Post-conditions: * will call serverside script and call the handler. * * Functions: * * * Log: * * Dipak A.Basantani 09/11/2008 if the request is for generate code or varification code then change the handler of the object event "onreadystatechange" * - Creation * */ function requestForCaptcha(url,queryStr,operation){ // Mozilla, Safari, Opera... if (window.XMLHttpRequest) { captchaXMLHttpRequestObj = new XMLHttpRequest(); // Request type is xml if (captchaXMLHttpRequestObj.overrideMimeType) { captchaXMLHttpRequestObj.overrideMimeType('text/xml'); }// end if, request overrideMimeType checking // IE } else if (window.ActiveXObject) { // Above IE5.x try { captchaXMLHttpRequestObj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { // Under IE5.x try { captchaXMLHttpRequestObj = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} }// end if, IE }// end if, browser checking // If req is null then alert if (!captchaXMLHttpRequestObj) { alert('Giving up :( Cannot create an XMLHTTP instance'); return false; }// end if, req checking // Set a callback to be invoked when the ReadyState changes on the XMLHTTP object. if(operation == 'generateCode' || operation == 'varification') { captchaXMLHttpRequestObj.onreadystatechange = updateCaptchaDivWithRandomString; url = url +queryStr+"&sid="+Math.random(); captchaXMLHttpRequestObj.open("GET",url,true); captchaXMLHttpRequestObj.send(null); } }// END function - loadXML /* * Name: * updateCaptchaDivWithRandomString * * Description: * get the key from the server and display into flash. * * Pre-conditions: * * Post-conditions: * A random 5 char string is generted and display the flash image * * Log: * Dipak A Basantani 10/09/2008 * - Creation */ function updateCaptchaDivWithRandomString() { // only if req shows "complete" if (captchaXMLHttpRequestObj.readyState == 4){ // only if "OK" if (captchaXMLHttpRequestObj.status == 200){ response = captchaXMLHttpRequestObj.responseText; eval(response); if(outputString.operation != 'validateCode') { // to get div object which is supported by all browsers objRandomImageAndTextDiv = document.getElementById("randomImageAndTextDiv_"+captchaGlobalStnBoxId); // // display the div for entering captcha code. // objRandomImageAndTextDiv.style.display = "block"; if(outputString.error == 'true') { document.getElementById('errorDiv_'+captchaGlobalStnBoxId).display = ""; document.getElementById('errorDiv_'+captchaGlobalStnBoxId).innerHTML = 'Code entered is wrong.Enter Correct Code.'; } // // update the div with the flash of the captcha string. // displayCaptchaRandomString(outputString.randomStr); } else { // // after validation of code it comes to here and will go to insert and display the new forum comments in the forum // document.getElementById('errorDiv_'+captchaGlobalStnBoxId).innerHTML = ""; // // call the user stnBox library function on success validation. and // eval(onSuccessFunction+"()"); return false; } }// else alert else{ alert("There was a problem retrieving the XML data:\n" + captchaXMLHttpRequestObj.statusText); }// end if, status checking }// end if, readstate checking }// end function updateRandomString /* * Name: * displayCaptchaRandomString * * Description: * display captcha string into div object. * * Pre-conditions: * * Post-conditions: * * * Log: * Dipak A. Basantani 09/10/2008 * - Creation */ function displayCaptchaRandomString(str) { url = "http://static.intertechmedia.com/itm/captcha-8.swf?v1="+str.charAt(0)+"&v2="+str.charAt(1)+"&v3="+str.charAt(2)+"&v4="+str.charAt(3)+"&v5="+str.charAt(4); tmpStr = ''; tmpStr += "To complete your post, please enter the code below(What?).
"; tmpStr += '\ \ \ \ '; tmpStr += '
Enter the code above:
'; // Get the object of popup Flash Movie Div which is supported by all browsers randomImageDivObj = document.getElementById("randomImageDiv_"+captchaGlobalStnBoxId); // If flash div object then embed object inside div if(randomImageDivObj){ randomImageDivObj.innerHTML = ""; randomImageDivObj.innerHTML= tmpStr; }// end if, flash movie object }// end function displayRandomString