﻿	function closeAddressBook() 
	{
		document.getElementById(popup_address_bookID).style.display = 'none';
		document.getElementById(popup_address_book_headerID).style.display = 'none';
		document.getElementById(td_open_popup_address_book_ID).style.display = '';
	}
	function sendThisMovieClicked()
	{
		// get the appropriate values			
		var textEmailTo = document.getElementById("emailTo").value;
		var textFrom = document.getElementById(txtFromID).value; 
		var textMessage = document.getElementById(txtMessageID).value;  
		
		
		// Add here validation to text
		// no href !
		if ( (textMessage.indexOf("<") > -1) || (textMessage.indexOf(">") > -1) )
	    {
	        return;
	    }
    	
	    if ( (textMessage.indexOf("&lt;") > -1) || (textMessage.indexOf("&gt;") > -1) )
	    {
	        return;
	    }	
		var typeID;
		
		if(typeof(playerAnimationID)!="undefined")
			typeID = playerAnimationID;// player page
		else if(typeof(animatedImgUrl)!="undefined") 
		{
		    if(document.URL.indexOf("shapeshifter")>0)
		        pageTracker._trackPageview("shapeshifter/emailsent/virtual");
			typeID = animatedImgUrl;
		}
		else if (typeof(_user_id) !="undefined")
			typeID = _user_id;// zone page	
		else typeID =74080;		
	
		// validate that values
		if(validateMailForm(textEmailTo, textFrom) == false)	
			return;
		// send the request as ajax
		var url = appPathScript + "/Pages/Application/GeneralPages/SendMailCallBack.aspx";
		var params = "mailTo=" + textEmailTo +
					"&mailFrom=" + textFrom +
					"&bodyText=" + textMessage + 
					"&linkUrl=" + location.href +
					"&sendType=" + sendType +
					"&typeID=" + typeID;
		AJAX.asyncWithCallback(url, params, "");

		if ($("popupAnimatedGif") && $("shapeshifterswf")) {
		    alert("Email Sent Successfully");
		    $("popupAnimatedGif").style.display = "none";
		    $("shapeshifterswf").style.display = "block";
		    try { toggleSWF("shapeshifterswf"); }
		    catch (err) { }
		}
		else {
		    hidePopup('popup_send_movie');
		    try { toggleSWF("shapeshifterswf"); }
		    catch (err) { }
		}
		
		
	}
	function validateMailForm(mailTo, mailFrom)
	{
		var errMessage = "";
		var isValid = true;
		if(trim(mailTo) == "")
		{
			errMessage = "the 'email to' field is mandatory";
			isValid = false;
		}
		else if(isValidEmails(mailTo) == false)
		{
			errMessage = "one of the email addresses is not valid";
			isValid = false;
		}
		else if(trim(mailFrom) == "")
		{
			errMessage = "the 'email from' field is mandatory";
			isValid = false;
		}
		var spnErr = document.getElementById("spnErrMsg");
		spnErr.innerHTML = errMessage;
		if(isValid == false)
		{
			msgElement = spnErr;
			startBlinking();
		}
		return isValid;
	}
	function isValidEmails(mailsAsStrting)
	{
		var i, isValid = false,mailAddress;
		var arrMails = mailsAsStrting.split(";");
		for(i=0; i < arrMails.length; i++)
		{
			mailAddress = arrMails[i];
			mailAddress = trim(mailAddress);
			if(mailAddress != "")
				isValid = validateMail(mailAddress);
		}
		return isValid;
	}
	function validateMail(mailAddress)
	{
		var isValid = true;
		if (mailAddress.search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == -1)
			isValid = false;
		return isValid;
	}
	
	// Removes leading whitespaces
	function LTrim( value ) {
		
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
		
	}

	// Removes ending whitespaces
	function RTrim( value ) {
		
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
		
	}
	
	
	// Removes leading and ending whitespaces
	function trim( value ) {
		
		return LTrim(RTrim(value));
		
	}
	
	
	function sEmailList()
	{
		var textEmailToDelete = "";
					
		var emailInputColl=document.getElementsByName("emailAddress");
		var isChecked = false;
		
		for (var i=0; i < emailInputColl.length; i++)
		{
			if (emailInputColl[i].checked)
			{
				var emailWasAdded = false;
				var address="";
				var email_array=document.getElementById("emailTo").value.split(";");
				address = emailInputColl[i].parentNode.parentNode.getElementsByTagName("label")[0].innerHTML;
				emailInputColl[i].parentNode.parentNode.style.display = "none";
				textEmailToDelete = textEmailToDelete+address+"; ";
									
				isChecked = true;
			}
		}
		if (!isChecked)
		{	
			alert("Anyone of checkboxes is checked!");				
		}
		
		return textEmailToDelete;
	}
	
	//function DeleteEmailAddresses()
	function DeleteEmailAddresses()
	{
		var textEmailToDelete = sEmailList();
		
		// remove contact at the server
		var url = appPathScript + "/Pages/Application/GeneralPages/SendMailCallBack.aspx";
		var params = "action=" + "delete" +
					 "&mailToDelete=" + textEmailToDelete; 
		
		// send the request as ajax		
		AJAX.asyncWithCallback(url, params, "");		
		
		// remove contact from client
		
	}
	
	
	//function AddAllEmailAddresses() ADD All - button [onclick]	
	function AddAllEmailAddresses()
	{
		var emailInputColl=document.getElementsByName("emailAddress");
				
		for (var i=0; i < emailInputColl.length; i++)
		{
			
			var emailWasAdded = false;
			var address="";
			var email_array=document.getElementById("emailTo").value.split(";");
			address = emailInputColl[i].parentNode.parentNode.getElementsByTagName("label")[0].innerHTML;
			
			for (var j=0; j < email_array.length; j++)
			{	
				if (trim(email_array[j]).toLowerCase()==trim(address).toLowerCase())
				{
					emailWasAdded = true;
					break;
				}
			}
			
			if(!emailWasAdded)
			{
				document.getElementById("emailTo").value = document.getElementById("emailTo").value+address+"; ";
			}
		}
	}
		
	//function AddEmailAddressChecked() ADD - button [onclick]
	function AddEmailAddressChecked()
	{
		var emailInputColl=document.getElementsByName("emailAddress");
		var isChecked = false;
		
		for (var i=0; i < emailInputColl.length; i++)
		{
			if (emailInputColl[i].checked)
			{
				var emailWasAdded = false;
				var address="";
				var email_array=document.getElementById("emailTo").value.split(";");
				address = emailInputColl[i].parentNode.parentNode.getElementsByTagName("label")[0].innerHTML;
				
				for (var j=0; j < email_array.length; j++)
				{	
					if (trim(email_array[j]).toLowerCase()==trim(address).toLowerCase())
					{
						emailWasAdded = true;
						break;
					}
				}
				
				if(!emailWasAdded)
				{
					document.getElementById("emailTo").value = document.getElementById("emailTo").value+address+"; ";
				}
				isChecked = true;
			}
		}
		if (!isChecked)
		{	
			alert("Anyone of checkboxes is checked!");
		}
	}
	
	function openAddressBook()
	{
		document.getElementById(td_open_popup_address_book_ID).style.display ="none";
		document.getElementById(popup_address_bookID).style.display = "";
		document.getElementById(popup_address_book_headerID).style.display = "";		
	}
	
	function handleEmailAddress(checkboxObj) 
	{
		var address;
		if(document.all)
		{
			address = checkboxObj.parentElement.nextSibling.childNodes[0].innerHTML;
		}	
		else
		{
			address = checkboxObj.parentNode.nextSibling.nextSibling.getElementsByTagName('label')[0].innerHTML;
		}	
		
			if (checkboxObj.checked) {
				document.getElementById('emailTo').value = document.getElementById('emailTo').value+address+'; ';	
			} else {
				var startAddresses = document.getElementById('emailTo').value.substring(0, document.getElementById('emailTo').value.indexOf(address));
				var endAddresses = document.getElementById('emailTo').value.substring(document.getElementById('emailTo').value.indexOf(address)+(address.length+2));
				
				document.getElementById('emailTo').value = startAddresses+endAddresses;	
			}
	}
	function revertCloseBtn(divObj) {
		divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('_over.gif', '.gif') : window.getComputedStyle(divObj, null).backgroundImage.replace('_over.gif', '.gif');
	}

