// allow only numbers

function check_number(e)
{
	var keycode = e.which ? e.which : document.event.keyCode;	
	if( (keycode == 8) || (keycode == 46) || (keycode == 9) )   // 8 - backspace, 46 - delete, 9 - tab
	{
		return true;
	}
	if( (keycode < 48) || (keycode > 57) ) // && ( keycode != 8 && keycode != 46 ) 
	{
		return false;
	}
}

// hide numbers
function hidenumbers(e)
{	

     var e =  e || window.event;
	
	var keycode = e.which ? e.which : e.keyCode;	
	
	if ((keycode>47)&&(keycode<58))
	{
		return false;
	}
}

function checkEnter(e)
{	if(window.event != undefined)	
	var keycode = e.which ? e.which : window.event.keyCode;
	if( keycode == 13 )
	{
		document.forms[0].btn_support.click();
	}
}
function form_validator(UserLoginForm){

	if ( $('#layer1_first_name').val().length == 0 )
	{
		alert("Please enter a value for the \"FirstName\" field.");
		$('#layer1_first_name').focus();
		return (false);
	}
/*
	if ( $('#last_name').val().length == 0 )
	{
		alert("Please enter a value for the \"LastName\" field.");
		$('#last_name').focus();
		return (false);
	}
	if ( $('#phone_number').val().length == 0 )
	{
		alert("Please enter a value for the \"PhoneNumber\" field.");
		$('#phone_number').focus();
		return (false);
	}
*/
	if ( $('#layer1_email').val().length == 0 )
	{
		alert("Please enter a value for the \"Email\" field.");
		$('#layer1_email').focus();
		return (false);
	}
	else
	{
		if ( ! checkEmail($('#layer1_email').val()) )
		{
			alert("Please enter a valid email id.");
			$('#layer1_email').focus();
			return (false);
		}
	}
 
 //return true;	
	 var name = $('#layer1_first_name').val();
	//var last_name = $('#last_name').val();
	var email = $('#layer1_email').val();
	//var request = $('#request').val();
		
	$('#layer1').hide();
	
	$.ajax({
		   url: "index.php?module=StartChat&mode=ajax",		 
		   type: "post",
			data:"name="+name+"&email="+email+"&status=fake",
		   success: function(msg)
		   {
		  // startSession();
			// alert("fake "+msg);
				/* if( msg == "login" )
				{
				
					setVisible('layer1');
				}
				else
				{ */
					username = msg.substr(0,msg.lastIndexOf('='));
					technician = msg.substr(msg.lastIndexOf('=')+1);
					chatWith(technician);
					
				//	alert("Fake:"+technician);
					//}
		   }		   
	});
}

function checkEmail(email) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
return (true);
}
return (false);
}



function preChat(lid){

if( lid == 1 ){


  //setVisible('layer1');

 }else{
    
  if ( $("#chtbxopned").val() !=1 ) startChat();
}

  

}

function startChat()
{


	
	var username;
	var technician;
	$('#livechat_id').hide();
	//window.open('<?php echo HTTP_SERVER_BASE.'livechat/index.php?module=user&prinid='.PRNID;?>','livechat','status=1,toolbar=1,width=480,height=450');
	$.ajax({
		   url: "index.php?module=StartChat&mode=ajax",		 
		   type: "post",											 
		   success: function(msg)
		   {		//alert(msg);	 
				//if( msg == "login" )
				//{					
				//	setVisible('layer1');
				//}
				//else
				//{
					startSession();
					username = msg.substr(0,msg.lastIndexOf('='));
					technician = msg.substr(msg.lastIndexOf('=')+1);					
					chatWith(technician);
					
					//alert("Not Fake:"+technician);
				//}
		   }		   
	});
}


function startSession()
{	
originalTitle = document.title;
	startChatSession();	
	$([window, document]).blur(function(){
		windowFocus = false;
	}).focus(function(){
		windowFocus = true;
		document.title = originalTitle;
	});

}
