var maxfriends = 99;

function tellafriend()
{
    var varstringz = "";
    var errors = 0;
    var emailnumstorun = 0;
    while(emailnumstorun <= totalfriends)
    {        
        if(do_checkemail(emailnumstorun)==true)
        {
            var thisemail = document.getElementById("friends_email_" + emailnumstorun).value;
            if(thisemail != "")
            {
                varstringz = varstringz + "&email_" + emailnumstorun + "=" + encodeURIComponent(thisemail);
            }
        }
        else
        {
            errors++;
        }
        emailnumstorun++;
    }
    if(do_checkname()==true)
    {
        varstringz = varstringz + "&name=" + encodeURIComponent(document.getElementById("your_name").value);
    }
    else
    {
        errors++;
    }
    if(do_checkmessage()==true)
    {
        varstringz = varstringz + "&message=" + encodeURIComponent(document.getElementById("your_message").value);
    }
    else
    {
        errors++;
    }
    if(errors == 0)
    {
        //alert(moviname);
        varstringz = '&thismovieID=' + thismovieID + '&thispagetitle=' + moviname + '&send_me_a_copy=' + document.getElementById("send_me_a_copy").checked +'&taf_numbazz=' + document.getElementById("taf_numbazz").value +varstringz;
        var urlz = "/xmltellafriend?" + varstringz;
        loadXMLDoctellafriend(urlz);
    }
}

var reqtellafriend;
function loadXMLDoctellafriend(url)
{
    if (window.XMLHttpRequest)
    {
        // branch for native XMLHttpRequest object
        reqtellafriend = new XMLHttpRequest();
        reqtellafriend.onreadystatechange = processReqcheckname;
        reqtellafriend.open("GET", url, true);
        reqtellafriend.send(null);
    }
    else if (window.ActiveXObject)
    {
        // branch for IE/Windows ActiveX version
        reqtellafriend = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqtellafriend)
        {
            reqtellafriend.onreadystatechange = processReqcheckname;
            reqtellafriend.open("GET", url, true);
            reqtellafriend.send();
        }
    }
}
function processReqcheckname()
{
    if (reqtellafriend.readyState == 4)
    {
        if (reqtellafriend.status == 200)
        {
            var response = reqtellafriend.responseXML.documentElement;
            var result = response.getElementsByTagName("result")[0].firstChild.data;
            if(result=='ok')
            {
                menupopulated = 0;
                totalfriends = 1;
                document.getElementById("tellafriend_menu").innerHTML = '';
                var to_string = response.getElementsByTagName("to_string")[0].firstChild.data;
                var showresult = 'Thank you for spreading the word. Your message has been sent to "' + to_string + '".';
                document.getElementById("tellafriend_menu").innerHTML = showresult;
            }
            else if(result=='bad img')
            {
                var resultdiv = document.getElementById("numbaz_result_div");
                resultdiv.innerHTML = '<img src="/common_files/pics/error.gif" /> the number you wrote does not match number in the picture';
            }
            else if(result=='last mail too young')
            {
                var timelefttowait = response.getElementsByTagName("timelefttowait")[0].firstChild.data;
                var showresult = 'please wait another ' + timelefttowait + ' seconds before using the Tell-a-Friend again.';
                alert(showresult);
            }
            else if(result=='data error')
            {
                var showresult = 'there was an error processing the data.';
                alert(showresult);
            }
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + reqtellafriend.statusText);
        }
    }
}


var menupopulated = 0;
var totalfriends = 1;
function add_friend()
{
    totalfriends++;
    if(totalfriends<=maxfriends)
    {
        var this_tellafriend_friends_email = getCookie('tellafriend_friends_email_' + totalfriends);
        var more_friends_div = document.getElementById("more_friends");
        //more_friends_div.innerHTML = more_friends_div.innerHTML + 'friend #' + totalfriends + ' <input type="text" id="friends_email_' + totalfriends + '" onchange="checkemail(' + totalfriends + ')" onkeydown="checkemail(' + totalfriends + ')" style="width:180px;" /><span id="friends_email_result_div_' + totalfriends + '"></span><br />';
        var divTag = document.createElement("div");
        divTag.id = "div" + totalfriends;
        divTag.innerHTML = 'friend #' + totalfriends + ' <input type="text" id="friends_email_' + totalfriends + '" onchange="checkemail(' + totalfriends + ')" onkeydown="checkemail(' + totalfriends + ')" style="width:180px;" value="' + decodeURIComponent(this_tellafriend_friends_email) + '" /><span id="friends_email_result_div_' + totalfriends + '"></span>';
        more_friends_div.appendChild( divTag );
        if(totalfriends==maxfriends)
        {
            var more_friends_link = document.getElementById("more_friends_link");
            more_friends_link.innerHTML = '';
        }
    }
}
function show_tellafriend_menu()
{
    if(menupopulated == 0)
    {
        var tellafriend_your_name = getCookie('tellafriend_your_name');
        var tellafriend_friends_email_0 = getCookie('tellafriend_friends_email_0');
        var tellafriend_friends_email_1 = getCookie('tellafriend_friends_email_1');
        var tellafriend_menu = '<div style="padding:10px;">' +
        '<h4>tell a friend about this page</h4>' +
        '<table><tr><td>' +
        'your friend email address: <span id="friends_email_result_div_1"></span><br />' +
        '<input type="text" id="friends_email_1" onchange="checkemail(1)" onkeydown="checkemail(1)" style="width:280px;" value="' + decodeURIComponent(tellafriend_friends_email_1) + '" /><br />' +
        '<div id="more_friends"><!-- --></div>' +
        '<div style="text-align:right;font-size:80%;"><a onclick="add_friend()" style="cursor:pointer;" id="more_friends_link">add more friends</a></div>' +
        'your name: <span id="your_name_result_div"></span><br />' +
        '<input type="text" id="your_name" onchange="checkname()" onkeydown="checkname()" style="width:280px;" value="' + decodeURIComponent(tellafriend_your_name) + '" /><br />' +
        'your email: <span id="friends_email_result_div_0"></span><br />' +
        '<input type="text" id="friends_email_0" onchange="checkemail(0)" onkeydown="checkemail(0)" style="width:280px;" value="' + decodeURIComponent(tellafriend_friends_email_0) + '" /><br />' +
        '<input type="checkbox" id="send_me_a_copy" />send me a copy too<br />' +
        'your message: <span id="your_message_result_div"></span><br />' +
        '<textarea style="width:280px;height:280px;" id="your_message" onchange="checkmessage()" onkeydown="checkmessage()">' +
        'Hey you have to check this out:\n\
' + thispagetitle + '\n\
' + 'on this page: http://' + myhappydomain + '/links/' + thismovieID + '/' + string_to_slug(moviname,100) + '\n\
' +'i think you will like it ' +
        '</textarea><br />' +
        'retype this number: <span id="numbaz_result_div"></span><br />' +
        '<img src="/image_verification?name=tellafriend_' + thismovieID + '&unifyier=' + Math.random()*1111111 + '" /> => <input type="text" id="taf_numbazz" />' +
        '</td></tr></table>' +
        '<input type="button" value="tell your friend" onclick="tellafriend()" />' +
        '</div>' +
        '<div style="width:100%;text-align:center;"><a onclick="close_tellafriend_menu()" style="cursor:pointer;">[close this window]</a></div>';
        document.getElementById("tellafriend_menu").innerHTML = tellafriend_menu;
        menupopulated = 1;
    }
    else
    {
        document.getElementById("tellafriend_menu").style.display = "block";
    }
}
function close_tellafriend_menu()
{
    document.getElementById("tellafriend_menu").style.display = "none";
}


function checkname()
{
    var t=setTimeout("do_checkname();",100);
}
function do_checkname()
{
    var fieldtocheck = document.getElementById("your_name");
    var resultdiv = document.getElementById("your_name_result_div");
    var currentvalue = fieldtocheck.value;
    if(currentvalue == '')
    {
        resultdiv.innerHTML = '<img src="/common_files/pics/error.gif" /> please enter your name';
        return false;
    }
    else
    {
        setCookie('tellafriend_your_name',encodeURIComponent(currentvalue),365);
        resultdiv.innerHTML = '<img src="/common_files/pics/success.gif" />';
        return true;
    }
}

function checkmessage()
{
    var t=setTimeout("do_checkmessage();",100);
}
function do_checkmessage()
{
    var fieldtocheck = document.getElementById("your_message");
    var resultdiv = document.getElementById("your_message_result_div");
    var currentvalue = fieldtocheck.value;
    if(currentvalue.length < 10)
    {
        resultdiv.innerHTML = '<img src="/common_files/pics/error.gif" /> message too short';
        return false;
    }
    else
    {
        resultdiv.innerHTML = '<img src="/common_files/pics/success.gif" />';
        return true;
    }
}

function checkemail(friendnum)
{
    var exec_string = "do_checkemail(" + friendnum + ");";
    var t=setTimeout(exec_string,100);
}
function do_checkemail(friendnum)
{
    var fieldname = "friends_email_" + friendnum;
    var fieldtocheck = document.getElementById(fieldname);
    var resultdivname = "friends_email_result_div_" + friendnum;
    var resultdiv = document.getElementById(resultdivname);
    var currentvalue = fieldtocheck.value;
    if(currentvalue == '' && friendnum != 0 && friendnum != 1)
    {
        setCookie('tellafriend_friends_email_' + friendnum,encodeURIComponent(currentvalue),-1);
        resultdiv.innerHTML = '';
        return true;
    }
    else
    {
        if(echeck(currentvalue) == true)
        {
            setCookie('tellafriend_friends_email_' + friendnum,encodeURIComponent(currentvalue),365);
            resultdiv.innerHTML = '<img src="/common_files/pics/success.gif" />';
            return true;
        }
        else
        {
            resultdiv.innerHTML = '<img src="/common_files/pics/error.gif" /> invalid email';
        }
    }
    return false;
}




/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    var lastdot=str.lastIndexOf(dot)
    if (str.indexOf(at)==-1){
        return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false
    }

    if (str.indexOf(at,(lat+1))!=-1){
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1){
        return false
    }

    if (str.indexOf(" ")!=-1){
        return false
    }

    if (str.substring(lastdot+1).length < 2){
        return false
    }

    return true
}


