
function storylink(storyUrl)
	{
		var screenWidth = screen.width;
		var screenHeight = screen.height;
		
		var popUpLeft = screenWidth / 2;
		var popUpTop = screenHeight / 2;
		
		storyLinkWindow = window.open(storyUrl,'name','height=200,width=300,left=' + popUpLeft + ',top=' + popUpTop);
		if (window.focus) 
		{
			storyLinkWindow.focus()
		}
	}

function showPopUp(el) {
	//var cvr = document.getElementById("grayOutBG")
	alert("Inside showPopUp");
	var dlg = document.getElementById(el)
	alert("DLG : " + dlg)
	//cvr.style.display = "block"
	dlg.style.display = "block"
	if (document.body.style.overflow = "hidden") {
	//cvr.style.width = "1024"
	//cvr.style.height = "100%"

	document.getElementById('flashcontent').style.visibility='hidden';
	document.getElementById('flashcontent1').style.visibility='hidden';
	}
}


function closePopUp(el) {
	//var cvr = document.getElementById("grayOutBG")
	var dlg = document.getElementById(el)
	//cvr.style.display = "none"
	dlg.style.display = "none"
	document.body.style.overflowY = "scroll"
	document.getElementById('flashcontent').style.visibility = 'visible';
	document.getElementById('flashcontent1').style.visibility='visible';
	clear();
}

function clear()
{
	document.getElementById('emailTo').value = "";
	
	document.getElementById('emailFrom').value = "";
	
	document.getElementById('sender').value = "";
	
	document.getElementById('mailMessage').value = "";
	
	document.getElementById('message').innerHTML = "";
}


function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object
 
function ajaxFunction(storyTitle,storyURL) {

  if(xmlhttp) { 
    var emailTo = document.getElementById('emailTo');
	
	var emailFrom = 'Admin@aboutmcdonalds.com';
	
	var sender = document.getElementById('sender');
	
	var subject = storyTitle;
	
	var validate = validateForm(emailTo,emailFrom,sender);

	if(validate == true)
	{

		var servletURL = "/mailapi/mailservice?emailTo="+emailTo.value+"&emailFrom="+emailFrom+"&sender="+sender.value+"&subject="+subject+"&storyTitle="+storyTitle+"&storyURL="+storyURL;

	    xmlhttp.open("GET",servletURL,true); //getname will be the servlet name

	    xmlhttp.onreadystatechange  = handleServerResponse;

	    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		xmlhttp.send("");
	}
	
  }
}
 
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById('message').innerHTML=xmlhttp.responseText; //Update the HTML Form element 
		
}
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}


function validateForm(emailTo,emailFrom,sender) 
{
   	if(emailTo.value == "")
	{
		alert("Please enter an email address");
		return false;
	}
	else if(emailTo.value != "")
	{
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   var emailAddress = emailTo.value;
	   if(!multiEmail(emailTo)) 
	   {
	      alert('Please enter valid email address');
		  return false;
	   }
	   else if(sender.value == "")
	   {
			alert("Please enter your name");
			return false;
	   }else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}

function multiEmail(emailTo) 
{
	var email = emailTo.value.split(',');
	if (email.length > 20){
		alert('Maxiumu number of allowed email addresses is 20');
		return false;
	}
	for (var i = 0; i < email.length; i++) 
	{
		if (!validateEmail(email[i], 1, 0)) 
		{
			return false;
		}
	}
return true;
} 

function validateEmail(email)
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) 
   {
      return false;
   }
   else
   {
		return true;
   }
	return true;
}
