
function output_stf_form(response) {
    response = eval("(" + response + ")");
    var el = document.getElementById("send_to_friend_form_container");
 	$("#send_to_friend_form_container").html(response.formHTML);
    //fix IE layout change bug
    //$(".footer").hide().show();

    //sIFR.replaceElement("h4", "swf/header.swf", "#848383", "#848383", "#cccccc", "#cccccc", 0, 0, 0, 0);

    //$('#send_to_friend_form').submit(function(){ MM_validateForm(); return false; });
}

function empty_stf_form() {
    if(document.getElementById("send_to_friend_form_container") != undefined){
        document.getElementById("send_to_friend_form_container").innerHTML = "";
        //$('#send_to_friend_form_container').fadeOut('normal');
    }
}

function write_stf_form() {
    if (
        (document.getElementById("send_to_friend_form_container").innerHTML != "")
        && !document.getElementById("send_to_friend_form_container").innerHTML.match(/The message has been sent/gi)
    ) {
        empty_stf_form();
        return false;
    }
	
	//current_photo = gallery.currentDetailImage;

    document.getElementById("send_to_friend_form_container").style.display = "block";
    document.getElementById("send_to_friend_form_container").innerHTML = "<img src='/www2/javascript/ajax_loading.gif' alt='loading...' />";

    var photo_query = 'command=form&page=' + gallery.currentPageNumber + '&section=' + gallery.currentSection.id + '&photo_id=' + gallery.currentDetailImage.id + '&refferer_page=media_gallery.php?photo_id=' + gallery.currentDetailImage.id;
    var url = 'photo_details_form.php?'+photo_query;
    var stf_pagecall = document.getElementById("stf_pagecall");//hidden form

    //document.getElementById("artist_id_stf_pagecall").value = "<?=$_REQUEST["artist_id"]?>";
    //document.getElementById("artist_name_stf_pagecall").value = current_photo.id;
    //document.getElementById("photo_id_stf_pagecall").value = current_photo.id;
    //document.getElementById("caption_stf_pagecall").value = (current_photo.length > 0)?current_photo.caption : 'View this <?=$this->getArtistName()?> photo';
	//var reqUrl = 'ajax_requests/gallery_next_or_previous.php';
	var onSuccessFunc = function() {
		
	}
	
	var req = AjaxRequest.post({'url':url, 'imageId':gallery.currentDetailImage.id, 'artist_id':gallery.artistId, 'onSuccess': onSuccessFunc });

    $.get(url, output_stf_form);
}

function send_form()
{
    var theform = document.getElementById("send_to_friend_form");
    //alert('theform: '+theform.action)
    var status = AjaxRequest.submit(
    theform
    ,{
        'onSuccess':function(req){ send_to_friend_succes(req.responseText); }
    }
    );
    return status;
}

function send_to_friend_succes(req)
{
    var ob = eval("(" + req + ")");
    document.getElementById("send_to_friend_form_container").innerHTML = ob.reponseHTML;

    //fix IE layout change bug
    $(".footer").hide().show();
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
        return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
        return false;
    }
    return true;
}

function allValidChars(email) {
    var parsed = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i=0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1)
        continue;
        parsed = false;
        break;
    }
    return parsed;
}

function validateFormEmail(friendEmail) {
    var msg= '';
    if (friendEmail.match(/\,/gi)) {
        var emails = friendEmail.split(',');
        for(var i=0;i<emails.length;i++) {
            if (!isValidEmail(emails[i])) {
                msg +='  Email "'+emails[i]+'" is not a valid email.\n';
            }
        }
    } else{
        if (!isValidEmail(friendEmail)) {
            msg ='  Email "'+friendEmail+'" is not a valid email.\n';
        }
    }
    return msg;
}

function MM_validateForm() {
    document.getElementById('friendEmail').value = document.getElementById('friendEmail').value.replace(/\s/gi, '');
    document.getElementById('fromEmail').value = document.getElementById('fromEmail').value.replace(/\s/gi, '');
    //document.getElementById('friendName').value = document.getElementById('friendName').value.replace(/\s/gi, '');

    var fromName = document.getElementById('fromName').value;
    var fromEmail = document.getElementById('fromEmail').value;
    var friendEmail = document.getElementById('friendEmail').value;
    var friendName = document.getElementById('friendName').value;

    var content = document.getElementById('content').value;
    var msg = '';


    if (fromName == '') {
        msg = '  Your Name is empty.\n';
    }
    if (fromEmail == '') {
        msg += '  Your Email is empty.\n';
    } else{
        if (!isValidEmail(fromEmail)) {
            msg += '  Email "'+fromEmail+'" is not a valid email.\n';
        }

        //msg += validateFormEmail(fromEmail, msg);
    }

    if (friendName == '') {
        msg += '  Friend Name is empty.\n';
    }

    if (friendEmail == '') {
        msg += '  Friend Email is empty.\n';
    } else{
        msg += validateFormEmail(friendEmail, msg);
    }

    if (content == '') {
        msg += '  Message is empty.\n';
    }
    if (msg != '') {
        alert('The following error(s) occurred:\n'+msg);
    }
    if (msg != ''){
        return false;
    } else {
        send_form();
        return false;
    }

}
