
System.Behavior.add('LoginForm', {
	attach: function(el, settings){
		el.login = function(){
			if(el.tagName!="FORM")
				return;
			
			if(typeof(currentChallenge) == "undefined")
				throw new Error(23553, "currentChallenge variable not set");
				
			if(typeof(usePwdHashing) == "undefined")
				throw new Error(23554, "usePwdHashing variable not set");
				
			var hashedStorage = false; //TODO: pass bool to tell passwords are hashed in database. 
			
			var pwdInput = null;
			for(i=0; i<el.elements.length; i++){
				if(el.elements[i].type=="password"){
					pwdInput = el.elements[i]; break;
				}
			}
			if(pwdInput == null) throw new Error(23555, 'Pasword control not found');

			if(pwdInput.value.length == 0)
				return;
				
			var encryptOrHash = function(s, key){
				if(usePwdHashing){
					if(typeof(MD5) != "function") throw new Error(23556, 'MD5 function not found');
					return MD5(s + key);
				}
				else{
					if(typeof(base64Encode) != "function") throw new Error(23557, 'TEAencrypt function not found');
					return base64Encode(s + "__" + key);
				}
			}
			
	    var address = el.action, hash, passwd = pwdInput.value;
	    
	    if(usePwdHashing && hashedStorage) passwd=MD5(passwd).toUpperCase();
	      
	    if (address.indexOf("?")==-1) address += "?"; else address += "&"
	      
	    if (navigator.userAgent.indexOf("Mozilla/") == 0 && (parseInt(navigator.appVersion) >= 4)){
	      if(passwd) hash=encryptOrHash(passwd,currentChallenge); else hash="";
	      
	      var js = 0, n=0;
	
	      for(i=0; i<el.elements.length; i++){
	      	if(el.elements[i].tagName == "FIELDSET" || !el.elements[i].name)
	      		continue;
	      	
	        if(el.elements[i].name.length <=0 || el.elements[i].name.charAt(0) == "_") {
	          continue;
	        }
	        
	        if(n > 0) address += "&";
	        
	        address += el.elements[i].name;address += "=";
	        if(el.elements[i] == pwdInput)
	          address += hash;
	        else if (el.elements[i].type == "checkbox" && !el.elements[i].checked)
	          address += "";
	        else if (el.elements[i].type == "radio" && !el.elements[i].checked)
	          address += "";
	        else if (el.elements[i].name == ".save")
	          address += "1"; // "Sign in" causes problem with the space
	        else if (el.elements[i].name == ".js"){
	          js = 1; 
	          address += "1"; 
	        }
	        else
	          address += escape(el.elements[i].value);
	        n++;
	      }
	      
	      address += "&.hash=" + (usePwdHashing ? "1" : "0")  ;
	      if(js == 0)
	        address += "&.js=1";
	      
	      address += "&.md5=1";
	      
	      location.href = address;
	      
	      el.onsubmit=null;
	      return false;
	    }
	    return true;
		}	
		el.location = el.action;
		el.onsubmit = function(){return el.login();}
	},
	
	detach: function(el){	}
})

function base64Encode(str){
	var charBase64 = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
		'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/');
	
	var out = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0, len = str.length;
	do {
		chr1 = str.charCodeAt(i++); chr2 = str.charCodeAt(i++); chr3 = str.charCodeAt(i++);
		//enc1 = (chr1 & 0xFC) >> 2;
		enc1 = chr1 >> 2; enc2 = ((chr1 & 0x03) << 4) | (chr2 >> 4); enc3 = ((chr2 & 0x0F) << 2) | (chr3 >> 6); enc4 = chr3 & 0x3F;
		out += charBase64[enc1] + charBase64[enc2];
	
		if (isNaN(chr2))
			out += '==';
		else if (isNaN(chr3))
			out += charBase64[enc3] + '=';
		else
			out += charBase64[enc3] + charBase64[enc4];
	}
	while (i < len);
	return out;
}