﻿// 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()
{
	
	
	if(document.all.item("userEmailAddress"))
	{
		if(document.all.item("userEmailAddress").disabled)
			return ;
		var strUserEmail = Trim(document.all.item("userEmailAddress").value) ;
	}
	else
	{
		alert("لم يتم إدخال البريد الالكتروني ") ;
		return false ;
	}
	
	
	if( !(isValidEmail(strUserEmail)) )
	{	
		//alert("الرجاء إدخال  العنوان البريدي  على الشكل التالي:name@domain.com");
		//alert(unescape("%u0627%u0644%u0631%u062C%u0627%u0621%20%u0625%u062F%u062E%u0627%u0644%20%20%u0627%u0644%u0639%u0646%u0648%u0627%u0646%20%u0627%u0644%u0628%u0631%u064A%u062F%u064A%20%20%u0639%u0644%u0649%20%u0627%u0644%u0634%u0643%u0644%20%u0627%u0644%u062A%u0627%u0644%u064A")+ "\n name@domain.com");
		//document.all.item("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.all.subscribtionResult ;
	
	if( document.all.item("cu_no") )
	{
		cu_no = document.all.item("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 = new ActiveXObject("MSXML2.XMLHTTP") ;
	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 = "لقد سبق إضافة هذا العنوان من قبل " ;
				lblResult.innerHTML = "This email is already used";
				break ;
			case "1" : // email saved correctly
				//lblResult.innerHTML = "شكرا لاشتراكك في النشرة الإلكترونية الدورية لجريدة الراية";//<br> you will receive a confirmation email in a moments" ;
				lblResult.innerHTML = "Thanks,Succesfuly regisetred.";
				document.all.item("userEmailAddress").disabled = true ;
				break ;
			default : // Error
				//lblResult.innerHTML = "نأسف ، لقد حدثة مشكلة خلال عملية الحفظ" ;
				lblResult.innerHTML = "Sorry, There was an error during saving process";
				break ;
		}	
	}
	catch(e)
	{
		alert(e.description);
		return false ;
	}

	objHTTPRequest =  null ;
}