var loginAttempts = 0;

$(document).ready(function() {  
	$("[tag='login']").mouseleave(function() {
	   $(this).hide();
	});	
	
	$(".menu,.menu_last").mouseover(function() {
	   $(this).removeClass('menu').addClass('menu_hover');
	});	
	
	$(".menu,.menu_last").mouseleave(function() {
	   $(this).removeClass('menu_hover').addClass('menu');
	});	
	
	$(".submenu,.submenu_last").mouseover(function() {
	   $(this).removeClass('submenu').addClass('submenu_hover');
	});	
	
	$(".submenu,.submenu_last").mouseleave(function() {
	   $(this).removeClass('submenu_hover').addClass('submenu');
	});	
	
	$(".login_header").mouseleave(function() {
	   $(".login_header").fadeTo("slow",.75);
	});	
	
	$(".login_header").mouseenter(function() {
	   $(".login_header").fadeTo("slow",1.0);
	});	
}); 


						   
						   
function executeOnSubmit(Message) {
	var res = confirm(Message);
	
	if(res)
	return true;
	else
	return false;
}

 $(document).mousemove(function(e){		  
	  pageX = e.pageX;
	  pageY = e.pageY;
	  clientX = e.clientX;
	  clientY = e.clientY;	 	 
 }); 	

function message_length(x,y) {
	if(x>y.cols*y.rows) {
		y.rows = Math.ceil((x/y.cols)*1.5);
		//document.getElementById('debug').innerHTML = y.rows;
	}
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = '';
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == '') { 
		thisfield.value = defaulttext;
	}
}

function checkall(theElement) {	 
	 i=1;
	 a = theElement + i;
	 if(document.getElementById(theElement).checked == true) {
		 do {
			 a = theElement + i;
			 var b = document.getElementById(a);
			 if(document.getElementById(a)) {
			 	b.checked = true;
			 }
			 i++;
		 } while(document.getElementById(a))
	 }
	 else {
		  do {
			 a = theElement + i;
			 var b = document.getElementById(a);
			 if(document.getElementById(a)) {
				 b.checked = false;
			 }
			 i++;
		 } while(document.getElementById(a))		 
	 } 
}
	
function cleartext(Field) {
	Field.value = '';
}

function bg(fieldname) {	
		fieldname.className = "Active_Cell";
}

function back(fieldname) {
	fieldname.className = "Center_Text";

}

var baseText = null; 
function showPopup(){   
	var popUp = document.getElementById("popupcontent");
	if (baseText == null) baseText = popUp.innerHTML;  
		popUp.innerHTML = baseText +       "<p><div align='center' id=\"statusbar\"><button onclick=\"hidePopup();\">Close window</button></div></p>";   
		var sbar = document.getElementById("statusbar");
		popUp.style.visibility = "visible";
		setOpacity()
}

function hidePopup(){   
	var popUp = document.getElementById("popupcontent");   
	popUp.style.visibility = "hidden";
	body.style.filter = 'alpha(opacity=100)';
}

function hide(itemID) {
	var x = document.getElementById(itemID);
	x.style.display = "none";
}

function show(itemID) {
	var x = document.getElementById(itemID);
	x.style.top=tempY;
	x.style.left=tempX;
	x.style.display = "inline";
}

function show_absolute(itemID) {
	var x = document.getElementById(itemID);
	x.style.display = "inline";
}

function show_center(itemID) {
	var x = document.getElementById(itemID);
	//document.write(x.offsetWidth/2);
	tempX = Math.round((screen.availWidth/2) - (x.offsetWidth/2)) + "px";
	tempY = Math.round((screen.availHeight/2) - (x.offsetHeight/2))+ "px";
	x.style.left=tempX;
	x.style.top=tempY;
	x.style.display = "inline";
}

function show_left(itemID) {	
	var x = document.getElementById(itemID);
	tX = (tempWidth - Math.round(x.offsetWidth)) + "px";
	document.getElementById('debug').innerHTML = "X";   
	x.style.left=tX; 
	x.style.top=tempY;
	x.style.display = "inline";
}

function LimitAttach(file) {
	extArray = new Array(".jpg", ".png", ".bmp", ".gif");
	allowSubmit = 'N'
    if (!file) return;
    while (file.indexOf("\\") != -1)
    file = file.slice(file.indexOf("\\") + 1);
    ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { 
			allowSubmit = 'Y'; 
			break; 
		}
    }
    if (allowSubmit == 'N') {
		alert("Please only upload files that end in types:  "
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file.");
		return;
	}
}

function logoutUser() {
	$.ajax({
		url: 'code_Logout.php',	
		success: function(data) {
			document.location = "index.php";
		}
	});		
}

function loginUser() {	
	$('#trLoginButtons').hide();
	$('#trLoginProcess').show();	
	
	$.ajax({
		url: 'code_Login.php',	
		data: "Username=" + $('#Username').val() + "&Password=" + $('#Password').val(),
		success: function(data) {
			if(data == 'FAILED') {
				loginAttempts++;
				$('#trLoginButtons').show();
				$('#trLoginProcess').hide();
				if(loginAttempts < 3) {
					$('#trErrorMessage').show();
					$('#tdErrorMessage').html("Invalid Username or Password."); 				
				}
				else {				
					document.location = "Password_Recovery.php";						
				}
			}
			else {			
				$('#spnUsername').html($('#Username').val());
				$('#divLogin').hide();
				$('#trLoginProcess').hide();
				$('#divUsername').show();
				$('#tdLogin').hide();
				$('#tdRegister').hide();
				$('#tdMyProfile').show();
				$('#tdSignOut').show();
				document.frmLogin.reset();							
			}		
		}
	});	 
}

function validateEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }

	 return true;					
}


function setCookie(cookieName,cookieValue,resetCookie) {
	if(resetCookie != 'Y') {
		expiredays = 365;
		var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=cookieName+ "=" +escape(cookieValue)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
		
	}
	else {	
		document.cookie = cookieName +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';		
	}
}

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 none() {
	
	
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
