/************
    USER FORM PRIVATE MESSAGES
************/

var inviteFormDiv = null;

function inviteForm(e, type, skipAuthCheck)
{
    if(!skipAuthCheck) var skipAuthCheck = false;

    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   
         if ((e.layerX || e.x)<450) {
                el.style.left=450;
	 } else {
	        el.style.left = Math.abs(e.layerX || e.x) + document.body.scrollLeft;
	 }
        el.style.top = (e.layerY || e.y + document.body.scrollTop) - 20;    
//        if (el.style.left) el.style.left=450;
        initEmailDiv(el);
    }
    
   
    var o2 = document.getElementById('inviteFormContent');
    
    
    if(checkAuth(true) || skipAuthCheck)
	{
		if(!type)
		o2.innerHTML = '<div style="text-align:center;padding-top:10px" class="big3 txtb">Invite your Friends Now!</div>' + 
		               "<div class=\"txtb big txt5\" style=\"padding:20 0 0 35\">"+
		                    "<div><div style=\"float:left;padding-top:6px;padding-right:5px\"><img src=\"/img/site/user/buzz-bull.gif\" align=\"absmiddle\"/></div> <a href=\"#\" onclick=\"inviteForm(null,2,"+skipAuthCheck+")\" class=\"txt5\">USE YOUR AOL, Yahoo, Gmail, Hotmail<br/> or MSN Account to send from your Address Book</a></div>"+
							"<div style=\"padding:15 0 5 0\">Or enter emails manually separated by commas:"+
							    "<div style=\"padding-top:5px;width:180px\"><input type=\"text\" class=\"b\" style=\"width:180px\" name=\"email_addresses\"/><div style=\"text-align:right;padding-top:3px\"><input type=\"button\" class=\"btn5_ cur\" onclick=\"sendEmail()\" value=\"SEND\"/></div></div>"+
							"</div>"+
					   "</div>"+
					   "<div style=\"position:absolute;bottom:0px;right:1px\"><a href=\"#\" onclick=\"sendEmail()\"><img src=\"/img/site/user/email-btn.png\" border=\"0\"/></a></div>";
		else		
		o2.innerHTML = "<div class=\"txtb big txt5\" style=\"padding:20 0 0 35\">Secure Access. No information is stored."+
							"<div style=\"font-weight:normal;padding-top:5px;\"><div style=\"float:left;width:70px\">login </div><div style=\"float:left\"><input type=\"text\" name=\"elogin\" class=\"b\" style=\"width:70px\"/></div> "+
								"<div style=\"float:left;width:50px\"><div style=\"padding-left:5px\">domain</div> </div> <div style=\"float:left\"><select name=\"edomain\" class=\"b\" style=\"width:100px\"><option value=\"aol\">aol</option><option value=\"gmail\">gmail</option><option value=\"hotmail\">hotmail</option><option value=\"msn\">msn</option><option value=\"yahoo\">yahoo</option></select></div><br clear=\"left\"/>"+
								"<div style=\"float:left;width:70px\">password </div> <div style=\"float:left\"><input type=\"password\" name=\"epass\" class=\"b\" style=\"width:70px\"/></div><div style=\"float:left;padding-left:50px\"><input type=\"button\" class=\"btn5_ cur\" onclick=\"sendEmail2()\" value=\"ENTER\"/></div>"+
							"</div>"+
							"<div style=\"position:absolute;bottom:0px;right:1px\"><a href=\"#\" onclick=\"sendEmail2()\"><img src=\"/img/site/user/email-btn2.png\" border=\"0\"/></a></div>"+
					   "</div>";
					   
	}				   
	else
	{
		o2.innerHTML = "<div class=\"txt5 big txtb\" style=\"width:300px;margin-left:25px;margin-top:50px\">"+
						 "<div style=\"float:left;text-align:right;\">To Email this headline to<br/> your friends please:</div>"+
						 "<div style=\"float:right;padding-top:10px\"><img src=\"/img/site/user/buzz-bull.gif\" align=\"absmiddle\"/> <a href=\""+GlobalDocumentAbsPath+"/login\" class=\"txt5\">Sign Up</a> <span style=\"padding-left:10px;padding-right:10px\">or</span> <img src=\"/img/site/user/buzz-bull.gif\" align=\"absmiddle\"/> <a href=\""+GlobalDocumentAbsPath+"/login\" class=\"txt5\">Log In</a></div>"+
		               "</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 a = new ajax();
	
	a.callback = eCall;
	var post = "etype=1&toemails="+emails;
	a.postData(GlobalDocumentAbsPath+'/members/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+'/members/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+'/members/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;
}