// 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 MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function isValidZip (zip,country){
	if(!zip){
		alert("Please enter ZIP code.");
		return false;
	}
	var zipRegExp = "";
	if(!country) format = 'US';
	switch(country){
		case'US': zipRegExp = /^\d{5}$|^\d{5}-\d{4}$/; break;
		case'CA': zipRegExp = /^[A-Z]\d[A-Z] \d[A-Z]\d$/; break;
		case'FR': zipRegExp = /^\d{5}$/; break;
		case'Monaco':zipRegExp = /^(MC-)\d{5}$/; break;
	}
	if(zipRegExp!="")
		if(!zipRegExp.test(zip)){
			alert("ZIP code is not valid.");
			return false;
		}
	return true;
}

function isValidEmail(string){
	emailRegExp = (/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/);

	if(!emailRegExp.test(string)){
		alert("Email is not valid.");
		return false;
	}
	return true;
}

function isPhoneNum(s)
{
	if(s!="() ")
	{
		isPhoneNumRegExp = (/^\([1-9]\d{2}\)\s?\d{7}$/);
		if (!isPhoneNumRegExp.test(s)) {
			alert("Phone number must be entered as: xxx xxxxxxx");
			return false;
		}
	}
	return true;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         IsNumber = false;
   }

   return IsNumber;
   
   }
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 < 6) || (strng.length > 20)) {
		return "The password is the wrong length. Password must be from 6 to 20 characters";
	}
	else if (illegalChars.test(strng)) {
		return "The password contains illegal characters.";
	}
	return '';
}

function login_(form,username,password,msgbox)
{
//	$("#"+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;
		}
		$.post("ajax/ajax_login.php",{ username:$('#'+username).val(),password:$('#'+password).val(),rand:Math.random() } ,function(data)
		{
			if(data != ''){
				if(data != 'no') //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()
						  { 
								window.location.replace(data);
						  });
					});
				}
				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');
	});
}

function logout(){
	$.ajax(
	{
		url: "logout.php", 
		cache: false,
		success: function(message) 
		{
			$("#block_login").empty().append(message);
			arr_TopMenu = ["about", "advertisers", "partners", "contact"];
			arr_PageTopMenu = ["digital.php", "advertisers.php", "partners.php", "contact.php"];
			p_active = "";
			
			loadTab("?a=home");	
			loadBlockLogin();		 
			LoadTopMenu();
			
		}
	});	
}

function loadBlockLogin(){
	$.ajax(
	{
		url: "ajax/login_block.php", 
		cache: false,
		success: function(message)
		{
			$("#block_login").empty().append(message);
		}
	});	
}

