var shown = false;

function show_print_dropdown(obj) {
  if (shown) {
    Element.remove($('print_dropdown'));
    shown = false;
  } else {
    o = $(obj);
    height = o.getHeight();
    offset = o.viewportOffset();
    
    var pdf_url = '';
    if (typeof(pdf_print_url) != "undefined") pdf_url = pdf_print_url;
    
    var rtf_url = '';
    if (typeof(pdf_print_url) != "undefined") rtf_url = rtf_print_url;
    
	var WP_url = '';
	if (rtf_url != '')
	
	{
		WP_url=rtf_url.replace(/type=rtf/gi, "type=WP");
	}
	
    dropdown = '<div"><a href="#" onclick="Element.remove($(\'print_dropdown\'));shown=false;window.print();return false;">Printer</a></div>';
    //if (pdf_url != '')      dropdown += '<div><a href="#" onclick="window.location=\'' + pdf_url + '\';Element.remove($(\'print_dropdown\'));shown=false;return false;">PDF</a></div>'
    //if (rtf_url != '')      dropdown += '<div><a href="#" onclick="window.location=\'' + rtf_url + '\';Element.remove($(\'print_dropdown\'));shown=false;return false;">Word</a></div>'
    //if (rtf_url != '')      dropdown += '<div><a href="#" onclick="window.location=\'' + WP_url + '\';Element.remove($(\'print_dropdown\'));shown=false;return false;">WordPerfect</a></div>'


    if (pdf_url != '')      dropdown += '<div><a href="#"  onclick="submitForm(\'pdf\');Element.remove($(\'print_dropdown\'));shown=false;return false;">PDF</a></div>'
    if (rtf_url != '')      dropdown += '<div><a href="#"  onclick="submitForm(\'rtf\');Element.remove($(\'print_dropdown\'));shown=false;return false;">Word</a></div>'
    if (rtf_url != '')      dropdown += '<div><a href="#"  onclick="submitForm(\'wp\');Element.remove($(\'print_dropdown\'));shown=false;return false;">WordPerfect</a></div>'

     if (print_action != '') dropdown += '<div><a href="#" onclick="fnInsertEmail(print_action);">Email</a>'
    dropdown += '<div><a href="#" onclick="Element.remove($(\'print_dropdown\'));shown=false;create_html_view();">HTML</a></div>'
    
    var my_div = document.createElement('div');

    $(my_div).insert(dropdown);
    
    my_div.id = 'print_dropdown';  
    my_div.style.backgroundColor = 'white';
    my_div.style.position = 'absolute';
    my_div.style.top = offset[1] + height;
    my_div.style.left = offset[0];
	if (rtf_url != '')
	{
	    my_div.style.width = '72px';
	}
	else
	{
		my_div.style.width = '48px';
	}
    my_div.style.border = '1px solid black';

    document.body.appendChild(my_div);
    
    shown = true;
  }
}

function create_html_view() {
  newWindow = window.open();

  html =  '<html><head><title>' + $$("title")[0].innerHTML + '</title>';
  html += '<link href="/stylesheets/layout.css" media="screen,print" rel="stylesheet" type="text/css" />';
  html += '</head><body>';
  html += $('wrapper').innerHTML;
  html += '</body></html>';
  html= html.replace(/\&para;/g, '¶')  
    
  newWindow.document.open();
  newWindow.document.write(html);
  newWindow.document.close();
}

function fnInsertEmail(strciFid)
{
    var strmail= prompt("Email", "");
    strmail=  GetPropermatt(strmail);
    if (validateEmail(strmail))
    {
      if (fnsendEmail(strmail, strciFid)==true)
      {
        alert('Email Sent');
      }
      else
      {
        alert('Email Not Sent');
      }  
      
    }
    else
    {
        alert ('Enter Proper EmailAddress');
        fnInsertEmail(strciFid);
    }
}

function GetPropermatt(strmatter)
{
	var repmatt= strmatter
	repmatt =repmatt.replace('+',' ');
	do 
	{
		repmatt = repmatt.replace('+',' ');
		
	}
	while (repmatt.indexOf('+')>= 0)
	
	repmatt =repmatt.replace('  ',' ');
	do 
	{
		repmatt = repmatt.replace('  ',' ');
	}
	while (repmatt.indexOf('  ')>= 0)
	
	
	if(repmatt != '')
	{	
		
		return repmatt
	}	
	else
	{
		return ''
	}
}

function validateEmail(elementValue)
{      
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue); 

}

function fnsendEmail(strEmailadd, strcId)
{
     try
	{
		var xmlHttpServer = new ActiveXObject("MSXML2.XMLHTTP");
	}
	catch (e1)
	{
		try
		{
			var xmlHttpServer = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		catch (e2)
		{
			try
			{
			    var xmlHttpServer = new XMLHttpRequest();
			}
			catch (e3)
			{
			    alert('Unable to Create Object, Please contact Administrator');
			    return false;
			}
		}
	}
    var strLink
	try
	{
	    strLink="../../EmailSend.aspx?eAdd="+ strEmailadd + "&cid="+ strcId;
	    xmlHttpServer.open("Post",strLink,false);
        try 
		{
			xmlHttpServer.send("");
		}
		catch(e6)
		{
			alert(e6.description);
			
		}
        if (xmlHttpServer.readyState==4)
        {
            var strReturn = xmlHttpServer.responseText;
            if(strReturn=="true")
                return true;
            else
                return false;
                
        } 
   
    }
    catch (e4)
	{
	    alert(e4.description);
	    return false;
	}
	
}
