﻿// function isValidEmail implemented using regular expressions
function isValidEmail (strEmail)
{
	var emailRegExp = /^.+\@.+\..+$/  ;
	strEmail = Trim(strEmail) ;
	return emailRegExp.test(strEmail) ;
}

////////////////////////////////////////////////////////
// trim is similar to the Trim fucntion in VBScript   //
//   it uses ltrim(left trim), to eliminate the left  //
//   side spaces, and rtrim(right trim),to eliminate  //
//   the right side spaces.                           //
// Parameters:                                        //
//	IN - strParam : the id of the text input          //                        
////////////////////////////////////////////////////////

function Trim(strParam)
{
	strParam = LTrim(strParam) ;
	strParam = RTrim(strParam) ;
	return strParam ;
}
// Eliminate all Left Spaces	////////////
function LTrim(strParam)
{
	var c;
	for(var i = 0 ; i < strParam.length ; i++)
	{
		c = strParam.charAt(i) ;
		if( c != " ")
			break ;
	}
	strParam = strParam.substring(i,strParam.length)	 ;
	return strParam ;
}
// Eliminate all Right Spaces	/////////////
function RTrim(strParam)
{	
	var c ;
	for(var i = strParam.length-1 ; i>0 ; i--)
	{
		c = strParam.charAt(i) ;
		if( c != " " )
			break ;
	}
	if(i != strParam.length-1)
		strParam = strParam.substring(0,i+1) ;
	
	return strParam ;
}
///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/////			NewsLetter Subscribtion Validation				//////////////
/////			By Mohammed Al-Tobji @05-08-2002				//////////////
//////////////////Added By Fuad Yaish @ 20-1-2003/////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function SubscribeUser()
{
	var strUserEmail = Trim(document.getElementById("userEmailAddress").value) ;
	if (strUserEmail == "")
	{
			alert("Please insert your Email ") ;
			return false ;
	}
	else 
	{
	 if( !(isValidEmail(strUserEmail)) )
	  {	
		//alert("الرجاء إدخال  العنوان البريدي  على الشكل التالي:name@domain.com");
		alert("please insert your email as the following form :"+ "\n name@domain.com");
		document.getElementById("userEmailAddress").focus();		
		return false ;
	  }
	   // if every thing goes well 
	   SaveUser(strUserEmail) ;
	   
	}
}

function SaveUser(strUserEmail)
{
	// save the user by loading a template XML file 
	// containing Insert SP. and check the return value
	var cu_no  = "-1" ;
	var result = "-1" ; // to indicate error as default	
	var lblResult = document.getElementById("subscribtionResult") ;
	
		
	if( document.getElementById("cu_no") )
	{
		cu_no = document.getElementById("cu_no").value ;
	}
	else
	{
		alert("could not find cu_no input") ;
		return false ;
	}
	
	// apply an HTTPRequest
	// send an HTTPRequest to save the data on an ASP page
	
	
	var objHTTPRequest;
	try
	{
		// IE
		objHTTPRequest = new ActiveXObject("MSXML2.XMLHTTP") ;
		
		
	}
	catch( e )
	{
		//firefox
		objHTTPRequest=new XMLHttpRequest();
		
		
	}
	
	var strURL = "/site/xml/topics/Newsletter/SubscNewsletter.asp?cu_no=" + cu_no + "&email=" + strUserEmail ;	

	try
	{
	
		objHTTPRequest.open("POST",strURL,false) ; // false for asyncronous call
												   // which means wait tell the page
											       // is fully executed, and read the returned value
		// send the request
		objHTTPRequest.send("");
		
		// check the result
		switch(objHTTPRequest.responseText)
		{
				case "0" : // already used email
				lblResult.innerHTML = "This email is already used" ;
				break ;
			case "1" : // email saved correctly
				lblResult.innerHTML = "Thank you for subscribing Newsletter";//<br> you will receive a confirmation email in a moments" ;
				document.all.item("userEmailAddress").disabled = true ;
				break ;
			default : // Error
				lblResult.innerHTML = "Saving error" ;
				break ;
		}	
	}
	catch(e)
	{
		alert(e.description);
		return false ;
	}

	objHTTPRequest =  null ;
}