function openWidow(url){
  window.open(url,'','width=800,height=600,scrollbars=yes')
}

var bgcolor_or;
var ftcolor_or;
function funcSetFocuse(obj,bgcolor){
	bgcolor_or = obj.style.backgroundColor;	
	obj.style.backgroundColor = bgcolor;
}

function funcSetFontColor(obj,ftcolor){
	ftcolor_or = obj.style.color;
	obj.style.color = ftcolor;
}

function funcLostFocuse(obj){
	obj.style.backgroundColor = bgcolor_or;
	obj.style.color = ftcolor_or;
}
function closePopup(){
	document.getElementById('popupMailPanel').style.display='none';
	document.getElementById('popupPanel').style.display='none';
	document.getElementById('popupBackground').style.display='none';
	document.getElementsByTagName('body')[0].style.position="relative";
	document.getElementsByTagName('body')[0].style.left = "0px";
	
	var html ='<form action="" method="POST">';
			html+='<div style="color:#035DF6; font-size:18px;padding:15px;padding-bottom:0px;">SEND TO A COLLEGUE</div>';
			html+='<div style="border:#035DF6 1px solid;margin:15px;margin-right:20px;padding:10px;">';
			html+='<font style="color:#F00;">*</font> Required<br /><br />';
			html+='<table>';
			html+='<tr><td align="right"><font style="color:#F00;">*</font>Your Name:</td>';
			html+='<td><input type="text" name="frm_sender_name" id="frm_sender_name" maxlength="50"/></td>';
			html+='</tr>';
			html+='<tr><td align="right"><font style="color:#F00;">*</font>Recipient&#39;s Name:</td>';
			html+='<td><input type="text" name="frm_recipient_name" id="frm_recipient_name" maxlength="50"/></td>';
			html+='</tr>';
			html+='<tr><td align="right"><font style="color:#F00;">*</font>Your E-mail Address:</td>';
			html+='<td><input type="text" name="frm_sender_mail" id="frm_sender_mail" maxlength="50"/></td>';
			html+='</tr>';
			html+='<tr><td align="right"><font style="color:#F00;">*</font>Recipient&#39;s E-mail:</td>';
			html+='<td><input type="text" name="frm_recipient_mail" id="frm_recipient_mail" maxlength="50"/></td>';
			html+='</tr>';
			html+='<tr><td align="right"><input type="button" value="Submit" onclick="sendMail();"/></td>';
			html+='<td><input type="reset" value="Cancel" onclick="document.getElementById(\'popupMailPanel\').style.display=\'none\';"/></td>';
			html+='</tr>';
			html+='</table>';
			html+='</div>';
			html+='</form>';
			document.getElementById('popupMailContent').innerHTML = html;
}
function displayNewsPopup(news_id){
	var panel = document.getElementById('popupPanel');
	var background = document.getElementById('popupBackground');
	var body = document.getElementsByTagName('body')[0];
	var viewportwidth;
  var viewportheight;

  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
	    viewportwidth = window.innerWidth,
	    viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
       && typeof document.documentElement.clientWidth !=
       'undefined' && document.documentElement.clientWidth != 0)
  {
			viewportwidth = document.documentElement.clientWidth,
      viewportheight = document.documentElement.clientHeight
  }

  // older versions of IE
  else
  {
      viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
      viewportheight = document.getElementsByTagName('body')[0].clientHeight
  }

  panel.style.left = (viewportwidth-605)/2 + "px";
  panel.style.top = (viewportheight-405)/2 + "px";
  panel.style.display = 'block';
  background.style.display = 'block';
  body.style.position="fixed";
  body.style.left = (viewportwidth-830-16)/2 + "px";
  
  document.getElementById('frm_news_id').value = news_id;
  document.getElementById('printNews').href = "index.php?page=content/news/print&newsid="+news_id;
  //alert('ajaxRead("popupContent",'+news_id+',Math.random(),1,1)');
  ajaxRead("popupContent",news_id,0,0,0,0,Math.random(),1,1);
}

function displayEmailPopup(){
	var panel = document.getElementById('popupMailPanel');
	var background = document.getElementById('popupBackground');
	var mailBackground = document.getElementById('popupMailBackground');
	var content = document.getElementById('popupMailContent');
	
	var viewportwidth;
  var viewportheight;

  // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
	    viewportwidth = window.innerWidth,
	    viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
       && typeof document.documentElement.clientWidth !=
       'undefined' && document.documentElement.clientWidth != 0)
  {
			viewportwidth = document.documentElement.clientWidth,
      viewportheight = document.documentElement.clientHeight
  }

  // older versions of IE
  else
  {
      viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
      viewportheight = document.getElementsByTagName('body')[0].clientHeight
  }

  panel.style.left = (viewportwidth-400)/2 + "px";
  panel.style.top = (viewportheight-300)/2 + "px";
  background.style.display = 'none';
  mailBackground.style.display = 'block';


	panel.style.display = 'block';
  //alert('ajaxRead("popupContent",'+news_id+',Math.random(),1,1)');
  //ajaxRead("popupContent",news_id,Math.random(),2,1);
}

function isValidEmail(str) {
   return (str.indexOf("@") > 0) && (str.indexOf(".",str.indexOf("@")) > 0);
}


function sendMail(){
	var news_id = document.getElementById('frm_news_id').value;
	var sender = document.getElementById('frm_sender_mail').value;
	var sender_name = document.getElementById('frm_sender_name').value;
	var recipient_name = document.getElementById('frm_recipient_name').value;
	var recipient = document.getElementById('frm_recipient_mail').value;

	var errormsg="";
	if (sender_name==""){
		errormsg += "- you must enter your name\n";
	}
	if (recipient_name==""){
		errormsg += "- you must enter the recipient's name\n";
	}
	if (sender==""){
	  errormsg += "- you must enter your email\n";
	}
	else if (!isValidEmail(sender)){
		errormsg += "- you must enter a valid email\n";
	}
	if (recipient==""){
	  errormsg += "- you must enter the recipient's email\n";
	}
	else if (!isValidEmail(recipient)){
		errormsg += "- you must enter a valid recipient's email\n";
	}
	
	if (errormsg==""){
		ajaxRead("popupMailContent",news_id,sender_name,recipient_name,sender,recipient,Math.random(),2,1);
	}else{
	  alert(errormsg);
	}
}


function ajaxRead(id,data1,data2,data3,data4,data5,data6,control,subControl)
{

	//XMLhttp variable will hold the XMLHttpRequest object
	var xmlObj = null;
	// If the user is using Mozilla/Firefox/Safari/etc
	if(window.XMLHttpRequest){
		xmlObj = new XMLHttpRequest();
        //Set the mime type
        //xmlObj.overrideMimeType('text/xml');
	}
	else if(window.ActiveXObject){
		xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		return;
	}

	switch (control){
		case 1:readNews(id,xmlObj,data1,control,subControl);break;
		case 2:submitMailForm(id,xmlObj,data1,data2,data3,data4,data5,control,subControl);break;
		default:
	}
  xmlObj.send (null);
}

function readNews(id,obj,data1,control, subControl)
{
//alert('HERE?'+document.getElementById(id));
        //Change the content of the "result" DIV to "Searching..."
        //This gives our user confidence that the script is working if it takes a moment for the result to be returned. However the user will likely never see this...
		document.getElementById(id).innerHTML = " <B> Loading...</B>";
        //This sets a variable with the URL (and query strings) to our PHP script
		url = 'index.php?page=content/news/details&newsid='+data1;
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		obj.open('GET', url, true);

        //Check that the ASP.NET script has finished sending us the result
		obj.onreadystatechange = function(){
			if(obj.readyState == 4 && obj.status == 200){
            //Replace the content of the "result" DIV with the result returned by the PHP script
				tempStr = obj.responseText;
				if(tempStr.length==0){
					document.getElementById(id).innerHTML = "Fail!";
				}
				else{
					document.getElementById(id).innerHTML =tempStr;
					//document.getElementById(id).innerText =tempStr;
				}
			}
			else{
				//If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				//document.getElementById(id).innerHTML = url;
			}
		};
}
function submitMailForm(id,obj,data1,data2,data3,data4,data5,control, subControl)
{
//alert('HERE?'+document.getElementById(id));
        //Change the content of the "result" DIV to "Searching..."
        //This gives our user confidence that the script is working if it takes a moment for the result to be returned. However the user will likely never see this...
		document.getElementById(id).innerHTML = " <B> Sending...</B>";
        //This sets a variable with the URL (and query strings) to our PHP script
		url = 'index.php?func=content/mail&newsid='+data1+'&sendername='+data2+'&recipientname='+data3+'&sender='+data4+'&recipient='+data5;
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		obj.open('GET', url, true);

        //Check that the ASP.NET script has finished sending us the result
		obj.onreadystatechange = function(){
			if(obj.readyState == 4 && obj.status == 200){
            //Replace the content of the "result" DIV with the result returned by the PHP script
				tempStr = obj.responseText;
				if(tempStr.length==0){
					document.getElementById(id).innerHTML = "Fail!";
				}
				else{
					document.getElementById(id).innerHTML =tempStr;
					//document.getElementById(id).innerText =tempStr;
				}
			}
			else{
				//If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				//document.getElementById(id).innerHTML = url;
				//document.getElementById(id).innerHTML = "Fail!";
			}
		};
}