/************
    USER FORM PRIVATE MESSAGES
************/

var inviteFormDiv = null;

function inviteForm(e, type)
{
    if(inviteFormDiv == null) // init form
    {
        var el = document.createElement('div');
        el.className = 'mesForm';
        document.body.appendChild(el);   
        inviteFormDiv = el;
    }
    else el = inviteFormDiv;

    if (e != null) 
    {
        if (!e)e=window.event;
        
        //var element = e.target || e.srcElement   
        el.style.left = e.layerX || e.x + document.body.scrollLeft;
        el.style.top = (e.layerY || e.y + document.body.scrollTop) - 20;    
        initEmailDiv(el);
    }
    
   
    var o2 = document.getElementById('inviteFormContent');
	o2.innerHTML = '<div style="text-align:center;padding-top:10px" class="big3 txtb">Send this page to a friend.</div>' + 
		               "<div class=\"txtb big txt5\" style=\"padding:10 0 0 35\">"+
		                    "<div><div style=\"float:left;padding-top:1px;padding-right:5px;\">Your Name:</div><div style=\"float:left\"><input type=\"text\" class=\"b\" style=\"width:100px\" name=\"set_your_name\"/></div></div>"+
		                    "<div><div style=\"float:left;padding-top:1px;padding-right:5px;padding-left:20px;\">Friend Name:</div><div style=\"float:left\"><input type=\"text\" class=\"b\" style=\"width:100px\" name=\"set_friend_name\"/></div></div><br clear=\"all\"/>"+
							"<div style=\"float:left;padding-top:6px;padding-right:5px;\">Friend Email:</div>"+
							    "<div style=\"float:left;padding-top:5px;\"><input type=\"text\" class=\"b\" style=\"width:210px\" name=\"email_addresses\"/></div>"+
							"<br clear=\"all\"/>"+
							"<div style=\"padding:5 0 10 0\">Enter additional text:"+
							    "<div style=\"padding-top:5px;\"><div style=\"float:left\"><textarea rows=\"4\" class=\"b\" style=\"width:210px\" name=\"set_email_text\"></textarea></div><div style=\"text-align:right;padding-top:35px;float:left;width:155px\"><input type=\"button\" class=\"btn5_ cur\" onclick=\"sendEmail()\" value=\"SEND\"/></div></div>"+
							"</div>"+
					   "</div>"+
					   "<div style=\"position:absolute;bottom:0px;right:1px\"></div>";

    
    objOn(el);                  
}


function initEmailDiv(o, isStaticPosition)
{
    isStaticPosition = !isStaticPosition ? false : true;
    if(!isStaticPosition) o.style.left = parseInt(o.style.left) - 450;
       
    o.style.width = '426px';
    o.style.height = '172px';
    o.innerHTML = '<form name="invite"><div style="position:absolute;z-index:1">'+
                       '<img src="/img/site/user/email-bg.png" width="426" height="172"/>'+
                  '</div>'+
                  '<div style="position:absolute;z-index:2;height:172px;width:426px">'+
                        '<div style="float:right;padding: 4 5 0 0">'+
                          '<a href="#" onclick="objOff(inviteFormDiv);">'+
                            '<img src="/img/site/user/close.png" border="0"/>'+
                          '</a>'+
                        '</div>'+                        
                        '<div id="inviteFormContent"></div>'+
                   '</div></form>';
}

function initEmailDiv2(o)
{
    o.style.left = parseInt(o.style.left) - 100;
    o.style.width = '426px';
    o.style.height = '217px';
    o.innerHTML = '<form name="invite"><div style="position:absolute;z-index:1">'+
                       '<img src="/img/site/user/email-bg2.png" width="606" height="217"/>'+
                  '</div>'+
                  '<div style="position:absolute;z-index:2;height:217px;width:606px">'+
                        '<div style="float:right;padding: 4 5 0 0">'+
                          '<a href="#" onclick="objOff(inviteFormDiv)">'+
                            '<img src="/img/site/user/close.png" border="0"/>'+
                          '</a>'+
                        '</div>'+
                        '<div id="inviteFormContent"></div>'+
                   '</div></form>';
}


function sendEmail()
{
	var f = document.forms['invite'];
	var emails = f.email_addresses.value;
	var your_name = f.set_your_name.value;
	var fr_name = f.set_friend_name.value;
	var etext = f.set_email_text.value;
	var a = new ajax();
	
	a.callback = eCall;
	var post = "etype=1&toemails="+emails+"&yname="+your_name+"&fname="+fr_name+"&etext="+etext;
	a.postData(GlobalDocumentAbsPath+'/invite/?xml=1',post,document.getElementById('inviteFormContent'));
}

function eCall(r)
{
    if(!checkXmlErrors(r, inviteForm))
    {   
		var o = document.getElementById('inviteFormContent');
		o.innerHTML = "<div style=\"padding:25 0 0 75;width:300px\" class=\"txt5 big txtb\">Thanks for Emailing your Friends!"+
						"<div style=\"padding-top:5px;padding-left:10px\"><input type=\"button\" value=\"Close this window\" class=\"btn23\" onclick=\"objOff(inviteFormDiv)\"/></div>"+
					  "</div>";
	}				  
	return false;
}

function sendEmail2()
{
	var f = document.forms['invite'];
	var a = new ajax();
	a.callback = eCall2;
	var post = "etype=2&elogin="+f.elogin.value+"&epass="+f.epass.value+"&edomain="+f.edomain.value;	
	a.postData(GlobalDocumentAbsPath+'/invite/?xml=1',post,document.getElementById('inviteFormContent'));
}

function sendEmail3()
{
    elist = '';
	var f = document.forms['invite'];
	for(i=0;i<f.length;i++)
	{
	    if(f[i].name == 'emails[]' && f[i].checked == true) elist += "&elist[]="+f[i].value;
	}
	
	if(elist != '')
	{	
	    elist += '&fromEmail='+f.fromEmail.value;
	    initEmailDiv(inviteFormDiv, true);
	    var a = new ajax();	    
	    a.callback = eCall;
	    var post = "etype=2"+elist;
	    a.postData(GlobalDocumentAbsPath+'/invite/?xml=1',post, document.getElementById('inviteFormContent'));
	}    
	else alert('Email addresses not selected');
}

var boolFakeAllSelected = false

function setFakeEmailAll(o)
{
    if(boolFakeAllSelected == false)
    {
        o.value = 'DESELECT ALL';
        boolFakeAllSelected = true;
    }
    else
    {
        o.value = 'SELECT ALL';
        boolFakeAllSelected = false;
    }

    var f = document.forms['invite'];
    for(var i=0;i<f.length;i++)
    {
        if(f[i].name == 'emails[]') f[i].checked = boolFakeAllSelected;
    }
}

function eCall2(r)
{
    if(!checkXmlErrors(r, inviteForm))
    {
        initEmailDiv2(inviteFormDiv);
        
		var o = document.getElementById('inviteFormContent');		
		var tmp = r.responseXML.getElementsByTagName('import-emails');
		if(tmp.length > 0)
		{
		    var fromEmail = tmp[0].attributes.getNamedItem('fromEmail').nodeValue;
		    txt = '';
		    tmp = tmp[0];
		    if(tmp.childNodes.length > 0)
		    {		                
		        txt = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr style=\"font-weight:bold\"><td>Name</td><td>Email</td><td align=\"center\" width=\"100\"><input type=\"button\" value=\"SELECT ALL\" class=\"btn cur\" onclick=\"setFakeEmailAll(this)\" style=\"width:99px;height:26px;background-image:url(/img/site/btn/btn6.jpg)\"/></td></tr><tr height=\"1\" class=\"bg23\"><td colspan=\"3\"></td></tr>";
		        for(i=0;i<tmp.childNodes.length;i++) 
		        {
		            name = (isAXObj == true ? tmp.childNodes[i].text : tmp.childNodes[i].textContent) + "\n";
		            a = tmp.childNodes[i].attributes;
		            email = a.getNamedItem('email').nodeValue;
		            txt += "<tr><td>"+name+"</td><td>"+email+"</td><td align=\"center\"><input type=\"checkbox\" name=\"emails[]\" value=\""+email+"\" style=\"margin:0px;padding:0px\"/></td></tr>";
		            txt += "<tr height=\"1\" class=\"bg23\"><td colspan=\"3\"></td></tr>";
		        }   
		        txt += "</table>";
		        o.innerHTML = "<div style=\"width:580px\">"+
		                        "<div style=\"padding:15 0 0 15\" class=\"txt5 big txtb\">"+
                                    "<div style=\"height:130px;overflow:auto;padding-right:10px\"><input type=\"hidden\" name=\"fromEmail\" value=\""+fromEmail+"\"/>"+txt+"</div>"+
                                    "<div style=\"text-align:right;padding-right:70px;padding-top:2px\"><input type=\"button\" value=\"SEND\" class=\"btn cur\" onclick=\"sendEmail3()\" style=\"width:99px;height:26px;background-image:url(/img/site/btn/btn7.jpg)\"/></div>"+
                                "</div>"+                                    
					          "</div>"+
					          "<div style=\"position:absolute;bottom:-20px;right:1px\"><a href=\"#\" onclick=\"sendEmail3()\"><img src=\"/img/site/user/email-btn.png\" border=\"0\"/></a></div>";;
			}		     
		}
	}				  
	return false;
}