function init_email(d) {
    GEvent.addDomListener(d, "mouseover", why_email);
    GEvent.addDomListener(d, "mouseout", why_email);
    GEvent.addDomListener(d, "click", why_email);
    GEvent.addDomListener($('email_explain'), "click", function(){this.style.display="none";this.setByClick=false;});
}

function why_email(e){
    function _up(d, e)  {
	d.style.top = (e.clientY+10) + "px";
	d.style.left = (e.clientX+10) + "px";
	d.style.position = "absolute";
	d.style.display="block";
    }
    function _down(d, e) {
	d.style.display="none";
	d.setByClick = false;
    }
    var d = $('email_explain');
    if (e.type == "mouseover") {
	_up(d, e);
    } else if (e.type == "mouseout") {
	if (d.setByClick != true) {
	    _down(d,e);
	}
    } else if (e.type == "click") {
	// we'll get mouseover first, so 
	if (d.setByClick == true) {
	    _down(d,e);
	} else {
	    _up(d,e)
	    d.setByClick = true;
	}
    } else  {
	GLog.write(e.type);
    }
}

function validate_email(value){
    // consider valid emails only if contain '@' before '.'
     apos=value.indexOf("@");
     dotpos=value.lastIndexOf(".");
     if (apos<1||dotpos-apos<2) {
	 return false;
     } else {
	 return true;
     }
 }
function email_status(charger, str) {
    d = $('email_div');
    if (str == null || str == ""){
	$('email_title').innerHTML = charger.name + " Comment";
	$('email_statusUpdate').value = "Comment";
	str = "";
    } else {
	$('email_title').innerHTML = charger.name + " Status Change to "+str;
	$('email_statusUpdate').value = "Status Change to "+str;
    }

    var siteid = $('email_siteid');
    siteid.charger = charger;
    $('email_siteid').value = charger.id
    $('email_sitename').value = charger.name;
    $('email_city').value = charger.city;
    var now = new Date();
    $('email_date').value = (now.getMonth()+1) + "/" + now.getDate() + "/" + now.getFullYear();
    $('email_comment').value = str;
    d.style.display = "block";
}

function submitEmail(form) {
    if(validate_form(form)) {
	alert("submitting email...");
	var siteID = form.email_siteid.value;
	var email_city = form.email_city.value;
	var email = form.email.value;
	var email_name = email.replace( /@.*$/, "");
	var email_sitename = form.email_sitename.value;
	var etext = email_name + " reports: " + form.comment.value;
	var btext = email_name + " reports: " + form.comment.value;
        var correction = form.correction.checked;
	var date = form.email_date.value;
	var statusUpdate = form.email_statusUpdate.value;
	
	// Email
	var file = "/cgi/mail?SiteID="+siteID+
	    "&correction="+correction+"&t="+encodeMyHtml(etext)+"&email="+email+"&status="+statusUpdate+
	    "&city="+encodeMyHtml(email_city)+
	    "&name="+encodeMyHtml(form.email_sitename.value)+
	    "&date="+encodeMyHtml(date);
	GDownloadUrl(file, function(data, responseCode){
		// Need to check if response is obtained (in case of error)
		if(Debug){$('email_result').innerHTML = data;}
	});


	// Add this also as a comment for this site
	file = "/cgi/blog?SiteID="+siteID+"&t="+encodeMyHtml(btext)+"&d="+encodeMyHtml(date);
	GDownloadUrl(file, function(data, responseCode){
		// Need to check if response is obtained (in case of error)
		if(Debug){$('blog_result').innerHTML = data;}
		form.email_siteid.charger.onClick(); //update the site details in right column
	});
	d = $('email_div');
	d.style.display = "none";
	$('email_explain').style.display = "none";
	$('email_explain').setByClick = false;
    }
}

function validate_form(form) {
    // return True if form appears valid, false otherwise.
    form.email.value = TrimString(form.email.value);
    form.email2.value = TrimString(form.email2.value);
     if (form.email.value == "") {
	 alert("Please provide your email in both email fields, in case I need to contact you.");
	 form.email.focus();
	 return false;
     }
     if (form.email2.value == "") {
	 alert("Please provide your email in both email fields, in case I need to contact you.");
	 form.email2.focus();
	 return false;
     }
     if (form.email.value != form.email2.value) {
	 alert ("Two email fields don't match -- typo?");
	 if (validate_email(form.email.value) && !validate_email(form.email2.value)){
	     // one looks good, two doesn't, so set focus to two, otherwise set focus to one
	     form.email2.focus();
	 } else {
	     form.email.focus();
	 }
	 return false;
     }
     if (form.comment.value == "") {
	 alert("Comment field is empty\n\nPlease provide details (charger type, circumstances)");
	 form.comment.focus();
	 return false;
     }

     if (!validate_email(form.email.value)) {
	 var answer = confirm("Email address doesn't appear to be valid.\n\nSubmit anyway?");
	 if (! answer){
	     form.email.focus();
	     return false;
	 }
     }
     return true;
 }

function encodeMyHtml(txt) {
    var encodedHtml = escape(txt);
    encodedHtml = encodedHtml.replace(/\//g,"%2F");
    encodedHtml = encodedHtml.replace(/\?/g,"%3F");
    encodedHtml = encodedHtml.replace(/=/g,"%3D");
    encodedHtml = encodedHtml.replace(/&/g,"%26");
    encodedHtml = encodedHtml.replace(/@/g,"%40");
    return encodedHtml;
} 

