var kCode;
var newVal;
var obj;

var captureKeys = function(ev) {
	ev = ev || window.event;             // gets the event in ie or ns
	kCode = ev.keyCode || ev.which;   // gets the keycode in ie or ns	
	if(kCode ==9)
		{

     	document.getElementById(obj.id).value = newVal;
		if(obj.id == "to"){
			document.getElementById('from').focus();
		}else if(obj.id =='from'){
			document.getElementById('subject').focus();
		}else if(obj.id == 'subject'){
			document.getElementById('textArea').focus();
		}
		return false;		
		}
	
}

function inject_txt(){
    document.getElementById(obj.id).value = newVal+'.';
    document.getElementById('floating').innerHTML = '';
    document.getElementById('textArea').focus();
}

var	topVal = 270;
var	thresh = 146;
	
function update_bod(text) {
obj = document.getElementById('textArea');
textVal = document.getElementById('textArea');
new Ajax.Request("refresh/body/",
  {
	asynchronous: true,
	method: 'post',
   	parameters: {sen: textVal.value},
    onSuccess: function(transport){
    	var response = transport.responseText;
	if(response != "null"){
	 temp = new Array();
	 txt = textVal.value;
     temp = txt.split(' ')
	 lengthW = temp.length
	 word = temp[(lengthW-1)]
	 index = response.indexOf(word)
	 tempResponse = response.substring(index);
	leftAmount = document.getElementById('textArea').value.length;
	document.getElementById("floating").innerHTML= tempResponse.substring(1)//+" "+leftAmount;
	document.getElementById("floating").style.left = "850px"
	if((leftAmount > thresh)&&(leftAmount < 3000)){
		topVal +=10;
		thresh += thresh;
	}
	document.getElementById('floating').style.top  = (topVal-45)+"px"
	 newVal = txt+tempResponse.substring(1);
//  	 textVal.innerHTML = tempResponse;
   }else{
	 document.getElementById("txt").innerHTML= "";
   }
    },
    onFailure: function(){ //alert('Something went wrong...') 
	}
  });
}

function update_sub() {
obj = document.getElementById('subject');
textValb = document.getElementById('subject');
new Ajax.Request("refresh/subject/",
  {
	asynchronous: true,
	method: 'post',
   	parameters: {sub: textValb.value},
    onSuccess: function(transport){
    var response = transport.responseText;
	if(response != "null"){
	 temp = new Array();
	 txt = textValb.value;
     temp = txt.split(' ')
	 lengthW = temp.length
	 word = temp[(lengthW-1)]
	 index = response.indexOf(word)
	 tempResponse = response.substring(index);
//	 document.getElementById("txt").innerHTML= tempResponse;
	leftAmount = document.getElementById('textArea').value.length;
	document.getElementById("floating").innerHTML= tempResponse.substring(1)//+" "+leftAmount;
	document.getElementById("floating").style.left = "850px"
	if((leftAmount > thresh)&&(leftAmount < 3000)){
		topVal +=10;
		thresh += thresh;
	}
	document.getElementById('floating').style.top  = (topVal-90)+"px"
	 newVal = tempResponse;

//  	 textVal.innerHTML = tempResponse;
   }else{
	 document.getElementById("txt").innerHTML= "";
   }
    },
    onFailure: function(){// alert('Something went wrong...') 
	}
  });
}

function update_from() {
obj = document.getElementById('from');
textValb = document.getElementById('from');
new Ajax.Request("refresh/from/",
  {
	asynchronous: true,
	method: 'post',
   	parameters: {from: textValb.value},
    onSuccess: function(transport){
    var response = transport.responseText;
	if(response != "null"){
	 temp = new Array();
	 txt = textValb.value;
     temp = txt.split(' ')
	 lengthW = temp.length
	 word = temp[(lengthW-1)]
	 index = response.indexOf(word)
	 tempResponse = response.substring(index);
//	 document.getElementById("txt").innerHTML= tempResponse;
	leftAmount = document.getElementById('textArea').value.length;
	document.getElementById("floating").innerHTML= tempResponse.substring(1)//+" "+leftAmount;
	document.getElementById("floating").style.left = "850px"
	if((leftAmount > thresh)&&(leftAmount < 3000)){
		topVal +=10;
		thresh += thresh;
	}
	document.getElementById('floating').style.top  = (topVal-133)+"px"
	 newVal = tempResponse;

//  	 textVal.innerHTML = tempResponse;
   }else{
	 document.getElementById("txt").innerHTML= "";
   }
    },
    onFailure: function(){ alert('Something went wrong...') 
	}
  });
}

function update_to() {
obj = document.getElementById('to');
textValb = document.getElementById('to');
new Ajax.Request("refresh/to/",
  {
	asynchronous: true,
	method: 'post',
   	parameters: {to: textValb.value},
    onSuccess: function(transport){
    var response = transport.responseText;
	if(response != "null"){
	 temp = new Array();
	 txt = textValb.value;
     temp = txt.split(' ')
	 lengthW = temp.length
	 word = temp[(lengthW-1)]
	 index = response.indexOf(word)
	 tempResponse = response.substring(index);
	 document.getElementById("txt").innerHTML= tempResponse;
	 newVal = tempResponse;
//  	 textVal.innerHTML = tempResponse;
   }else{
	 document.getElementById("txt").innerHTML= "";
   }
    },
    onFailure: function(){ alert('Something went wrong...') 
	}
  });
}

function valCheck(val){
return document.getElementById(val).value
}

function sendEmail(){
	document.getElementById('sendEmailConf').innerHTML = "sending...";
	frm	= valCheck('from')
	subj 	= valCheck('subject')
	text	= valCheck('textArea')
	error = false
	
	if((frm != '')&&(frm.indexOf('@') != -1)){
		//pass
	}else{
		document.getElementById('error2').innerHTML = " Email address required."
		document.getElementById('from').style.backgroundColor="#FF6666"
		error = true;
	}
	if(subj != ''){
		//pass
	}else{
		document.getElementById('error3').innerHTML = " Subject line required."
		document.getElementById('subject').style.backgroundColor="#FF6666"
		error = true;
	}
	if(text != ''){
		// pass
	}else{
		document.getElementById('error4').innerHTML = " Body content required."
		document.getElementById('textArea').style.backgroundColor="#FF6666"
		error = true;
	}
	if(!error){

	new Ajax.Updater('profile',"send/",
	  {
		asynchronous: true,
		method: 'post',
	   	parameters: {efrom: frm, esubj: subj, body: text},
	    onSuccess: function(transport){
//	    response = transport.responseText;
//		if(response != 'Invalid header found.'){
		document.getElementById('from').value = ""
		document.getElementById('subject').value = ""
		document.getElementById('textArea').value = ""
		document.getElementById('msg').innerHTML = "<span style='color:#5B8853;'> Email Successfully sent!</span>"
		document.getElementById('sendEmailConf').innerHTML = "";
//		}else{
//		document.getElementById('msg').innerHTML = "<span style='color:#FF6666;'>Invalid header found. </span>"			
//		}
		
	    },
	    onFailure: function(){ 
			document.getElementById('msg').innerHTML = "<span style='color:#FF6666;'> Email unsuccessfully Sent!  Error in submission.</span>" 
		}
	  });	
	}
}


function add(){
	new Ajax.Updater('profile',"add/",
	  {
		asynchronous: true,
	    onSuccess: function(transport){
	
	    },
	    onFailure: function(){ 
		}
	  });
}

var last_a = 0;

function loader(a_id,_id){
    new Ajax.Request("loader/",
	  {
		asynchronous: true,
		method: 'post',
	   	parameters: {id: _id,rev:revisions},
	    onSuccess: function(transport){
        var response = transport.responseText;
        if(last_a != 0){
            document.getElementById(last_a).style.textDecoration = 'none';
            document.getElementById(last_a).style.color = '#000';
        }
	    document.getElementById('email-block').innerHTML = response;
        document.getElementById(a_id).style.textDecoration = 'underline';	  
        document.getElementById(a_id).style.color = '#D84747';        
        last_a = a_id;  
    
	    },
	    onFailure: function(){ 
	    document.getElementById('email-block').innerHTML = "Error encountered\n\n please try this again later.";
		}
	  });
}




