// JavaScript Document
var win=null;

function NewWindow(mypage,myname,w,h,Isscroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+Isscroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=0,fullscreen=0';
win=window.open(mypage,myname,settings);}

function trim(s) 
{ 
    var l=0; var r=s.length -1; 
    while(l < s.length && s[l] == ' ') 
         l++;  
    while(r > l && s[r] == ' ') 
         r-=1;
    return s.substring(l, r+1); 
}

function checkUsername(strng)
{
	if ((strng.length < 4) || (strng.length > 20)) {
    	return "The username is the wrong length. Username must be from 4 to 20 characters.";
	}
	var illegalChars = /\W/;
  // allow only letters, numbers, and underscores
    if (illegalChars.test(strng)) {
       return "The username contains illegal characters.";
    }
	return '';
}

function checkPassword (strng) 
{
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if ((strng.length < 4) || (strng.length > 20)) {
		return "The password is the wrong length. Password must be from 4 to 20 characters";
	}
	else if (illegalChars.test(strng)) {
		return "The password contains illegal characters.";
	}
	return '';
}
//===============Check login to load Login box =============//
function checkLoged()
{
	$.ajax(
	{
		url: 'ajax/checklogin.php', 
		cache: false,
		success: function(message) 
		{
			if(message!='NULL')
				$("#login_box").empty().append(message);
			else
				document.getElementById('t_login').style.display = '';
		}
	});	
}
//===============End Check login to load Login box =========//

function login_(form,username,password,msgbox,login_page)
{
	$("#"+form).submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#"+msgbox).removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		if($('#'+username).val()==''){
			$("#"+msgbox).html('Please enter username to login!').addClass('error').fadeTo(900,1);
			$('#'+username).focus();
			return false;
		}

		var mess = checkUsername($('#'+username).val());
		if(mess!=''){
			$("#"+msgbox).html(mess).addClass('error').fadeTo(900,1);
			$('#'+username).focus();
			return false;
		}
		if($('#'+password).val()==''){
			$("#"+msgbox).html('Please enter password to login!').addClass('error').fadeTo(900,1);
			$('#'+password).focus();
			return false;
		}
		mess = checkPassword($('#'+password).val());
		if(mess!=''){
			$("#"+msgbox).html(mess).addClass('error').fadeTo(900,1);
			$('#'+password).focus();
			return false;
		}

		if(login_page!=''){
			$.post("ajax/"+login_page+"_login.php",{ username:$('#'+username).val(),password:$('#'+password).val(),rand:Math.random() } ,function(data)
			{
			  if(data != '' && data != 'incorrect') //if correct login detail
			  {
				$("#"+msgbox).fadeTo(200,0.1,function()  //start fading the messagebox
				{ 
					//add message and change the class of the box and start fading
					$(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
					function()
					{ 
					 	//redirect to secure page
						if(login_page=='ajax')
							data = "http://www.pumptoptvlocal.com/loadlogin.php?key="+data;

						window.location.replace(data);
						return true;
						//checkLoged();
						//$(this).html('&nbsp;').addClass('error').fadeTo(900,1);
					});
				});
			  }
			  else
			  {
				$("#"+msgbox).fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('The username or password incorrect.').addClass('error').fadeTo(900,1);
				});		
			  }
			});
		}
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#"+password).blur(function()
	{
		$("#"+form).trigger('submit');
	});
}