$(document).ready(function() {
	//THIS IS FOR PLACE HOLDER
	$('#search_value').placify();	
	$('#search_value').keypress(function(e){
		if(e.which == 13){
			e.preventDefault();
			document.searchFrm.submit();
			return false;
		}
	});
	
	$('#regFrm').submit(function() {
		// Do something
	}).validate({
		rules: {
			email: {
				required: true,
				email: true,
				remote: {
					url: _globalDocRoot + "checkValidemail.php",
					type: "post"
				}
			},
			password: 'required',
			cpassword: {
				required: true,
				equalTo: '#password'
			},
			first_name: 'required',
			last_name: 'required',
			company_name: 'required',
			phone_no: 'required'
		},
		messages: {
			email: {
				required: 'Please enter your email address',
				email: 'Please enter a valid email address',
				remote: 'Email entered already exists'
			},
			password: 'Please enter your password',
			cpassword: {
				required: 'Please reconfirm your password',
				equalTo: 'password and confirm password mismatch'
			},
			first_name: 'Please enter your first name',
			last_name: 'Please enter your last name',
			company_name: 'Please enter the company name',
			phone_no: 'Please enter your phone no'
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent().next());
			
		},
		submitHandler: function(form) {
			submitUserDetails();
		},
		success: function(label) {
			// set &nbsp; as text for IE
			label.html('&nbsp;').addClass('checked');
		}
	});
	
	$('#loginFrm').submit(function() {
		// Do something
	}).validate({
		rules: {
			login_email: {
				required: true,
				email: true
			},
			login_password: 'required'
		},
		messages: {
			login_email: {
				required: 'Please enter your email address',
				email: 'Please enter a valid email address'
			},
			login_password: 'Please enter your password'
		},
		
		errorPlacement: function(error, element) {
			error.appendTo(element.parent().next());
		},
		submitHandler: function(form) {
			checkUserAuthentication();
			// form.submit();
		},
		success: function(label) {
			// set &nbsp; as text for IE
			label.html('&nbsp;').addClass('checked');
		}
	});
	
	$('#searchFrm').submit(function() {
		// Do something
	}).validate({
		rules: {
			search_value: 'required'
		},
		messages: {
			search_value: 'Search field is blank'
		},
		
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler: function(form) {
			form.submit();
		},
		success: function(label) {
			// set &nbsp; as text for IE
			label.html('&nbsp;').addClass('checked');
		}
	});
	
	$('#forgotFrm').submit(function() {
		// Do something
	}).validate({
		rules: {
			forgot_email: {
				required: true,
				email: true
			}
		},
		messages: {
			forgot_email: {
				required: 'Please enter your email address',
				email: 'Please enter a valid email address'
			}
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler: function(form) {
			sendForgotEmail();
		},
		success: function(label) {
			// set &nbsp; as text for IE
			label.html('&nbsp;').addClass('checked');
		}
	});
	
	/*
	$(function(){
		$(document).pngFix();
	});


	
	
	//On Hover Over
	function megaHoverOver(){
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	    (function($) {
	        //Function to calculate total width of all ul's
	        jQuery.fn.calcSubWidth = function() {
	            rowWidth = 0;
	            //Calculate row
	            $(this).find("ul").each(function() { //for each ul...
	                rowWidth += $(this).width(); //Add each ul's width together
	            });
	            rowWidth = rowWidth + 50;
	        };
	    })(jQuery); 
	
	    if ( $(this).find(".row").length > 0 ) { //If row exists...
	
	        var biggestRow = 0;	
	
	        $(this).find(".row").each(function() {	//for each row...
	            $(this).calcSubWidth(); //Call function to calculate width of all ul's
	            //Find biggest row
	            if(rowWidth > biggestRow) {
	                biggestRow = rowWidth;
	            }
	        });
	
	        $(this).find(".sub").css({'width' :biggestRow}); //Set width
	        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
	
	    } else { //If row does not exist...
	
	        $(this).calcSubWidth();  //Call function to calculate width of all ul's
	        $(this).find(".sub").css({'width' : rowWidth}); //Set Width
	
	    }
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	
	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 100, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 500, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
	$("ul#topnav li .sub").hide();
	*/
	
});


function showSubMenu(obj,selType){
	var divobj = document.getElementById('submenu_' + obj);
	if(divobj){
		if(selType == 1){
	 		$('#submenu_'+ obj).show();	
	 		$('#navi_'+ obj).removeClass('home');	
	 		$('#navi_'+ obj).addClass('homeOver');	
	 	}else{
		 	$('#submenu_'+ obj).hide();	
		 	$('#navi_'+ obj).removeClass('homeOver');	
	 		$('#navi_'+ obj).addClass('home');	
		}	
	}
}

function submitUserDetails(){
	var frmObj = document.regFrm;
	var ajaxVar = new ajaxVars();
	ajaxVar.vars['incModule'] = 'users';
	ajaxVar.vars['action'] = "runFunction";
	ajaxVar.vars['func'] = "addUser";
	ajaxVar.vars['nosplit'] = 1;
	ajaxVar.vars['email'] = frmObj.email.value;
	ajaxVar.vars['password'] = frmObj.password.value;
	ajaxVar.vars['first_name'] = frmObj.first_name.value;
	ajaxVar.vars['last_name'] = frmObj.last_name.value;
	ajaxVar.vars['company_name'] = frmObj.company_name.value;
	ajaxVar.vars['phone_no'] = frmObj.phone_no.value;
	
	ajaxVar.gotoFunction = 'refreshUserPage';
	ajaxVar.AjaxPostSQL();
}

function refreshUserPage(dataArr, fnVars){
	var pageUrl = location.href;
	pageUrl = pageUrl.replace('#','');
	pageUrl = pageUrl.replace("?action=logout", "");
	pageUrl = pageUrl.replace("&action=logout", "");
	location.href = pageUrl;
}
function userLogOut(){
	var linkData = location.href;
	var separator = '?';
	if(linkData.indexOf("?") >= 0){
		separator = '&';
	}
	var pageUrl = location.href;
	pageUrl = pageUrl.replace('#','');
	
	location.href = pageUrl + separator + "action=logout";
}

function checkUserAuthentication(){
	var frmObj = document.loginFrm;
	
	var ajaxVar = new ajaxVars();
	ajaxVar.vars['incModule'] = 'users';
	ajaxVar.vars['action'] = "runFunction";
	ajaxVar.vars['func'] = "checkAuthentication";
	ajaxVar.vars['nosplit'] = 1;
	ajaxVar.vars['login_email'] = frmObj.login_email.value;
	ajaxVar.vars['login_password'] = frmObj.login_password.value;
	
	ajaxVar.gotoFunction = 'checkAuthentication';
	ajaxVar.AjaxPostSQL();
}

function checkAuthentication(dataArr, fnVars){
	var divObj = document.getElementById('loginErrId');
	divObj.innerHTML = '';
	if(dataArr == 1){
		divObj.innerHTML = 'Authentication failed!';
	}else{
		var linkData = location.href;
		linkData = linkData.replace('#','');
		if(linkData.indexOf("?action=logout") >= 0 || linkData.indexOf("&action=logout") >= 0){
			linkData = linkData.replace("?action=logout", "");
			linkData = linkData.replace("&action=logout", "");
		}
		location.href = linkData;
	}
}

function sendForgotEmail(){
	var frmObj = document.forgotFrm;
	
	var ajaxVar = new ajaxVars();
	ajaxVar.vars['incModule'] = 'users';
	ajaxVar.vars['action'] = "runFunction";
	ajaxVar.vars['func'] = "sendForgotEmail";
	ajaxVar.vars['nosplit'] = 1;
	ajaxVar.vars['forgot_email'] = frmObj.forgot_email.value;
	ajaxVar.gotoFunction = 'emailPassSend';
	ajaxVar.AjaxPostSQL();
}

function emailPassSend(){
	var divText = document.getElementById('forErrId');
	divText.innerHTML = "An email has been send to your email address with the new password. If you email is valid.";
}

function showForgotBox(){
	var divObj = document.getElementById('forgotBox');
	if(divObj.className == 'blockdiv'){
		divObj.className = 'visiblediv';
	}else{
		divObj.className = 'blockdiv';
	}
}


