// js handling the login procedures

// constants
var NORMAL_STATE = 4;
var LOGIN_PREFIX = 'index.php';

// variables
var http = getHTTPObject(); // We create the HTTP Object
	
function okForXMLHTTPREQUEST(){

	if (!http) return false;
	return true;
}

_initGenericDescription_div = function(div_name) {

	if (!okForXMLHTTPREQUEST()) return false; 
	
	var div = document.getElementById(div_name);
	div.title = 'Click to send recipe';
	div.originalText = div.innerHTML;
	div.style.display = 'inline';

	div.getExtra = function() {
		return '';
	}
	div.saveChanges = function(form) {
	
		this.innerHTML = '<i>sending ...</i>&nbsp;'; // the nbsp is to ensure the correct lineheight in windows
		document.body.style.cursor = "wait";
		
		username = encodeURI(document.forms.RecipeSendToFriend.username.value);
		useremail = encodeURI(document.forms.RecipeSendToFriend.useremail.value);
		friendemail = encodeURI(document.forms.RecipeSendToFriend.friendemail.value);
		recipeId = encodeURI(document.forms.RecipeSendToFriend.recipeId.value);


		http.open('GET', 'index.php?module_id=3&module_cmd=email_to_friend&recipe_id='+recipeId+'&from_email='+useremail+'&to_email='+friendemail+'&sender_name='+username, false);

		// http.onreadystatechange = handleHttpGetSeed;
		http.send(null);		
		document.body.style.cursor='auto';
		this.endEditing();
		if (http.responseText != "Done."){
			alert("Email was not sent!\nCheck the data you entered.");
		}else{
			// display email send and after a while set back the originial text
			this.innerHTML = '<span class="redlink">email sent</span>';
			this.hideTimer = setTimeout('var el = document.getElementById("'+this.id+'"); if (el) el.endEditing()', 2500)
		}
		
		// here somes the send recipe ajax

	}

	div.startEditing = function() {

		if (window.should_I_ignore_stuff_because_of_editable_div_action || window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_button_action) return;
		window.should_I_ignore_stuff_because_of_editable_div_action = 1;
		this.isEditing = true;
		this.unhighlight();
		//this.style.display = 'none';

		var emailFriend = document.getElementById('emailFriend');
		//emailFriend.style.display = 'none';		

		var sendForm = document.getElementById('sendForm');
		sendForm.style.display = 'inline';
		sendForm.style.position = 'absolute';
		sendForm.style.left = '40';

		var username = document.getElementById('SendToFriendUserName');
		username.focus();

	}
	div.endEditing = function() {
		window.should_I_ignore_stuff_because_of_editable_div_action = 0;		
		this.isEditing = false;
		
		var sendForm = document.getElementById('emailFriend');
		sendForm.innerHTML = div.originalText;
		sendForm.style.display = 'inline';

		var sendForm = document.getElementById('sendForm');
		sendForm.style.display = 'none';
		
		
		
		// this.style.display = 'none';
	}
	div.onclick = div.startEditing;	
	
	div.onmouseover = function() {
		if (window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_button_action) return;
		this.highlight();
	}
	
	div.onmouseout = function() {
		if (this.hideTimer) clearTimeout(this.hideTimer);
		this.hideTimer = setTimeout('var el = document.getElementById("'+this.id+'"); if (el) el.unhighlight()', 1000)
	}
	
	div.highlight = function() {
		if (window.should_I_ignore_stuff_because_of_editable_div_action) return;
		if (this.hideTimer) clearTimeout(this.hideTimer);
		div.style.backgroundColor = '#ffffd3';
		if (this.emptyText && (div.innerHTML=='&nbsp;' || div.innerHTML==' ' || div.innerHTML.charCodeAt(0) == 160)) {
			div.style.color = '#888';	
			div.innerHTML = this.emptyText;
		}
	}
	
	div.unhighlight = function() {
		if (this.hideTimer) clearTimeout(this.hideTimer);
		div.style.backgroundColor = '';
		if (this.emptyText && div.innerHTML.toUpperCase()==this.emptyText.toUpperCase()) {
			div.innerHTML = '&nbsp;';
			div.style.color = '#000';
		}
	}
	
}

// check if form is filled
function checkFriendSubmit(formName){
		var error = '';
		if (formName.username.value == '' || formName.username.value == ' '){
			error = error + "User Name\n";
		}
		if (formName.useremail.value == '' || formName.useremail.value == ' '){
			error = error + "User Email\n";
		}
		if (formName.useremail.value == '' || formName.useremail.value == ' '){
			error = error + "Friends Email\n";
		}
		if (error != ''){
			alert('Please fill all mandatory fields:\n'+error);
			return false;
		}else{
			return true;
		}
}
