String.prototype.trim = function(){return
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}



var captchaURL = "http://dev.texvet2.cms.tamhsc.edu/assets/style-1/php/captcha_text.php";
var postURL = "";

function jCaptcha(captchaId,targetFormId,successMessageOrUrl){   

   // prepare the form when the DOM is ready 
	$(document).ready(function() { 
		var options = { 
			target:        '#' + captchaId,   // target element(s) to be updated with server response 
			beforeSubmit:  validate,  // pre-submit callback 
			success:       showResponse,  // post-submit callback 
	 
			// other available options: 
			//url:       captchaURL,        // override for form's 'action' attribute 
			type:      'post'        // 'get' or 'post', override for form's 'method' attribute 
			//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
			//clearForm: true        // clear all form fields after successful submit 
			//resetForm: true        // reset the form after successful submit 
	 
			// $.ajax options can be used here too, for example: 
			//timeout:   3000 
		}; 
	 

                $("#" + targetFormId).validate();
                
                if(successMessageOrUrl.startsWith("#")){
                   $(successMessageOrUrl).hide();
                }


		$('#' + captchaId).load(captchaURL);
                postURL = $('#' + targetFormId ).attr('action');
                $('#' + targetFormId ).attr('action',captchaURL);
		$('#' + targetFormId ).ajaxForm(options); 
		
	}); 

	 
	// pre-submit callback 
	function validate(formData, jqForm, options) { 
 
            if ($("input[name='hidden_captcha_text'").val() == "" ) { 
                return false; 
            } 

            return $('#' + targetFormId).valid();
	} 
	 
	// post-submit callback 
	function showResponse(responseText, statusText)  { 
		// for normal html responses, the first argument to the success callback 
		// is the XMLHttpRequest object's responseText property 
	 
		// if the ajaxForm method was passed an Options Object with the dataType 
		// property set to 'xml' then the first argument to the success callback 
		// is the XMLHttpRequest object's responseXML property 
	 
		// if the ajaxForm method was passed an Options Object with the dataType 
		// property set to 'json' then the first argument to the success callback 
		// is the json data object returned by the server 
	 

                if(responseText.indexOf("SUCCESS") != -1 ){
                     
                     $('#' + targetFormId).attr('action',postURL);
                     $('#' + targetFormId).trigger('submit');

                     if(successMessageOrUrl.startsWith("http://") || successMessageOrUrl.startsWith("https://") ) {
                       this.location.href = successMessageOrUrl;
                     }
                     else if(successMessageOrUrl.startsWith("#")){
                       $('#' + targetFormId).replaceWith($(successMessageOrUrl).show("normal"));

                     } else {
                       $('#' + targetFormId).html(successMessageOrUrl);
                     }
                }
	} 

}