function pre_view(f) {
    var place = document.getElementById('msg');
    
    var fsize = f.fsize.options[f.fsize.selectedIndex].value;
    place.style.fontSize = fsize;

    var fcolor = f.fcolor.options[f.fcolor.selectedIndex].value;
    place.style.color = fcolor;
    
    var text = f.text.value;
    var rExp = /\n/gi;
    var html_text = text.replace(rExp, "<br>");
    place.innerHTML = html_text;
}

function pre_submit(f) {
    var errors = '';
    var to = f.recipient_email.value;
    var from = f.sender_email.value;
    if (f.recipient_email.value=='') {
        errors+="Please enter recipient's email address.\n";
    }
    if (to != '' && ! to.match(/[\w\.\-]+@[\w\.\-]+/)) {
        errors+="Recipient's email address doesn't look like correct one\n";
    }
    if (from != '' && ! from.match(/[\w\.\-]+@[\w\.\-]+/)) {
        errors+="Sender's email address doesn't look like correct one\n";
    }
    if (errors!='') {
        alert(errors);
        return false;
    }
    return true;
}

function zoom(imageSrc,w,h) {
     _CONTENT = "";
     _CONTENT+="<HTML>\n";
     _CONTENT+="<head>\n";
     _CONTENT+="</head>\n";
     _CONTENT+="<BODY LEFTMARGIN=0 TOPMARGIN=20>\n";
     _CONTENT+="<"+"SCRIPT>\n";
     _CONTENT+="document.write('<center>');\n";
     _CONTENT+="document.write('<IMG SRC=\\'"+imageSrc+"\\' style=\\'border: solid 1px black\\'>');\n";
     _CONTENT+="document.write('<p><a href=# onClick=\\'javascript: opener.location.href=\"/ecards/send.shtml?"+imageSrc+"\"; window.close()\\'><img border=0 src=\\'/ecards/images/send_big.gif\\'></a>');\n";
     _CONTENT+="document.write('</center>');\n";
     _CONTENT+="</"+"SCRIPT>\n";
     _CONTENT+="</BODY>\n";
     _CONTENT+="</HTML>\n";

     options='status=no,toolbar=no,menubar=no';
     
     if (w>1000) {
       w=1000;
       options+=',scrollbars=yes';
     }

     if (h>700) {
       h=700;
       options+=',scrollbars=yes';
     }
     
     NewWin= open("",'map','width='+w+',height='+h+ options);
     NewWin.document.open();
     NewWin.document.write(_CONTENT);
     NewWin.document.close();
     NewWin.focus();
    return false;
}

function getRand (count) {
    count = count || 32;
    var i = 0;
    var str = '';
    var arr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'];
    while (i < count) {
        var r = Math.round(Math.random() * 61);
        str = str+arr[r];
        i = i + 1;
    }
    return str;
}

function check_code() {
    var icode = document.getElementById('icode');
    var code = document.getElementById('code');
    var send_button = document.getElementById('send_button');
    send_button.value='Please wait!';

    if (!code.value) {
        alert ("Field Security Code must be completed!");
        send_button.value='Looks great. Send it! >>';
        return false;
    }
    if (code.value.length!=6) {
        alert ("Error in Security Code!");
        send_button.value='Looks great. Send it! >>';
        return false;
    }
    send_button.disabled=true;

    var formData = "c="+encodeURIComponent(code.value);
    var xmlhttp = null;
    if (document.all) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    if (xmlhttp) {
        xmlhttp.open("POST","/cgi-bin/ecards/checkcode.cgi",false);
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlhttp.send(formData);
        var otvet = xmlhttp.status;
        if (otvet==200) {
            document.forms[0].submit();
        } else {
            icode.src="/cgi-bin/ecards/code.cgi?nc="+getRand();
            send_button.disabled=false;
            alert ("Error in Code field. Try again.");

            send_button.value='Looks great. Send it! >>';
            return false;
        }
    }
}

