// Replace the normal jQuery getScript function with one that supports
// debugging and which references the script files as external resources
// rather than inline.

jQuery.extend({
   getScript: function(url, callback) {
      var head = document.getElementsByTagName("head")[0];
      var script = document.createElement("script");
      script.src = url;

      // Handle Script loading
      {
         var done = false;

         // Attach handlers for all browsers
         script.onload = script.onreadystatechange = function(){
            if ( !done && (!this.readyState ||
                  this.readyState == "loaded" || this.readyState == "complete") ) {
               done = true;
               if (callback)
                  callback();

               // Handle memory leak in IE
               script.onload = script.onreadystatechange = null;
            }
         };
      }

      head.appendChild(script);

      // We handle everything using the script element injection
      return undefined;
   }
});

// thanks to JQUERY HOWTO for this snippet to decode url params
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
    // add array.indexOf function for IE
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}

if (typeof(XPRIZE) == "undefined") {
    XPRIZE = {};
}
XPRIZE.login = function() {
    function validate(str) {
        var tot=0;
	for (var i=0; i<str.length; i++) {
	    tot += str.charCodeAt(i);
        }
	var x = tot ^ 1365;
	return (x == 1860);
    }
    return {
        jumpsTheShark : function() {
	    var jts = $("#xprize-login input:password").val();
	    if (!validate(jts)) return false;

	    // save to cookie
	    $.cookies.set("xprize_test", jts);
	    return true;
	},
	alreadyLoggedIn: function() {
	    // check cookie
	     var tst = $.cookies.get("xprize_test");
	     if (tst && validate(tst)) return true;
	     return false;
	}
    }
}();


