var link;


function checkCookie1(){

		/*cookieName=getCookie('gemnews');
		if (cookieName!=null && cookieName!="")
		  {
			return true;
		  }
		else
		  {*/
			 // alert(newsItem);
			  return openLogin();
		 // }
		}
function checkCookie(newsItem){
		link = newsItem;
		cookieName=getCookie('gemnews');
		if (cookieName!=null && cookieName!="")
		  {
		 	//alert('Welcome again '+cookieName+'!');
	 		//alert(newsItem);
	 		window.open(newsItem,'mywindow');
	 		//downloadResource(newsItem);
		  }
		else
		  {
			 // alert(newsItem);
			  openLogin();
		  }
		}

		function openLogin(){
			var URL1 = "popups/subscribe.html";
			$('#lightbox').jOverlay({url:URL1, autoHide : false, color:'#000', bgClickToClose : true, closeOnEsc : true, opacity : 0.2, center:true});
			$('#jOverlayContent').css("width", "380px");
			$('#jOverlayContent').css("height", "240px");
			return false;
		}
		
		function closeMe(){
			$.closeOverlay();
			return false;
		}

	function getCookie(c_name)
    {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
    }
		/*
		function setCookie(c_name,value,expiredays){
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			document.cookie=c_name+ "=" +escape(value)+
			((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
			alert("calling set cookies");
		}
*/

    function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
		

}
	
		
		
//common functions

function isBlank(val)
{

	if (val=="" || val==" ")
	{
		return true;
	}
	return false;
	
}//End of isBlank()



function trim(str)
{ 
	return((""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
}

function isValidEmail(value){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(trim(value)))
		return true;
	else
		return false;
	
}//End of isValidEmail()



function showSuccess(msg,id,note){
	succEle = document.getElementById(id);
	noteEle = document.getElementById(note);
	succEle.innerHTML = msg;
	noteEle.innerHTML = "<strong>Thank You</strong> for providing your email address. You can now begin downloading the News items.";
	document.getElementById("errHolder").style.display="none";
	document.getElementById("errHolder").className="";
	document.getElementById("login_form").style.display="none";
}//End of showStatus()


function changeError(id){
	document.getElementById(id).style.borderStyle="solid";
	document.getElementById(id).style.borderColor="red";
}//End of changeError()	

function changeProper(id){
	document.getElementById(id).style.borderStyle="";
	document.getElementById(id).style.borderColor="";
}//End of changeProper()	


function showError(ele,msg,obj){
	myEle = document.getElementById(ele)
	myEle.innerHTML = msg;
	myEle.className = "statusErr";
	document.getElementById(obj).focus();
	changeError(obj);
}//End of ismatch()	

function validate()
{
	var password = document.getElementById('password').value;
	
	if(isBlank(password)){
		showError("errHolder","Please enter password","password");
		return false;
	}
	else{
		changeProper("password");
	}	

	$.ajax({
	   type: "POST",
	   url: "/user/process-login.php",
	   data: "password="+password,
	   success: function(msg){
	   	if(msg=='Success'){
   		   	//showSuccess("Submission Successful","msg","notes");
		   	Set_Cookie( 'gemnews', 'itworks', '30', '/', '', '' );
		    window.location.href='/news.php';
		   //	checkCookie(link);
		   //	downloadResource(link);
		   	//window.open(newsItem,'link');
			return true;
			 var count=0;
					var myInt = setInterval(function(){
					 count ++;
					 if(count==6){
							$.closeOverlay();
							return false;
						}
					 },1000);
	   			}	else{
	   				$('#errHolder').html(msg);
               $('#errHolder').show();
	   			}
		}	

	 });
	 
	 //$.getJSON('http://pp-services.appspot.com/gem/save-contact.jsp?emailId='+email);

}


