// JavaScript Document
function getQuerystring(key, default_){
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}

$(document).ready(function() {
    
    initMenus();
    
	$('#confirm').hide();
   
	jQuery.validator.messages.required = "";

	$("#frmLogin").validate({	
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below.';
				$("div.error span").html(message);
				$("div.error").show();
				$('html, body').animate({scrollTop:0}, 'slow');
			} else {
				$("div.error").hide();
			}
		},
		submitHandler: function() {			
			var dataString = $("#frmLogin").serialize();
    		$.ajax({
    			type: "POST",
    			url: "http://www.militaryforsaleforrent.com/ajax.php",
    			data: dataString,
    			success: function(retVal) {
    				if(retVal == 'r'){
                        window.location.href = 'http://www.militaryforsaleforrent.com/members.php?page=member_account';
    					return false;
    				}
    				else if(retVal == 'e'){
    					alert('Your subscription has been expired. Kindly pay the renewal fee on next page.');	
    					return false;
    				}else if(retVal == 's' || retVal == 'd'){
    					alert(retVal);
    					return false;
    				}else{
    				    alert('Invalid login and/or password.\r\nPlease try again or click "Forgot Password" link to recover your login details.');
    				    $('#username').val('');
                        $('#password').val('');
                        $('#username').focus();
                        return false;
                    }
    			}
    		});
		}	
	});	
});

$(function() {

	$(".doLogout").click(function() {
		var dataString = 'doCase=logout';
		$.ajax({
			type: "POST",
			url: "http://www.militaryforsaleforrent.com/ajax.php",
			data: dataString,
			success: function(retVal) {
				$(location).attr('href','http://www.militaryforsaleforrent.com');
				alert('Good bye!!!');
			}
		});
	});

	$(".doLogin").click(function() {
		// validate and process form here
		var username = $("input#username").val();
		if (username == "" || username == "username") {
			$("input#username").addClass('formError');
			$("input#username").focus();
			return false;
		}
		var password = $("input#password").val();
		if (password == "" || password == "password") {
			$("input#password").addClass('formError');
			$("input#password").focus();
			return false;
		}
		
		
		var dataString = 'doCase=loginCheck&username='+ username + '&password=' + password;
        
		$.ajax({
			type: "POST",
			url: "http://www.militaryforsaleforrent.com/ajax.php",
			data: dataString,
			success: function(retVal) {
				if(retVal === 'r'){
					//window.location.href = '/members.php?page=member_account';
                    window.location.reload();
					return false;
				}
				else if(retVal === 'e'){
					alert('Your subscription has been expired. Kindly pay the renewal fee on next page.');	
					return false;
				}else if(retVal === 's' || retVal === 'd'){
					alert(retVal);
					return false;
				}else{
				    alert('Invalid login and/or password.\r\nPlease try again or click "Forgot Password" link to recover your login details.');
                    return false;
                }
			}
		});
		return false;		
		
		
	});
	
	
	$(".doSendPassword").click(function() {
		// validate and process form here
		var key = $("input#key").val();
		if (key == "") {
			$("input#key").addClass('error');
			$("input#key").focus();
			return false;
		}

		$('#confirm').hide();
		$('#loading').show();
		$('#loading').html('<img src="http://www.militaryforsaleforrent.com/images/loader.gif" align="left" />&nbsp;&nbsp;please wait...');

		var dataString = 'doCase=sendPassword&key='+ key;

		$.ajax({
			type: "POST",
			url: "http://www.militaryforsaleforrent.com/ajax.php",
			data: dataString,
			success: function(retVal) {

				if(retVal === "error"){
					$('#confirm').addClass('error');
					$('#confirm').show();
					$('#loading').hide();
					$('#confirm').html('No record found. Please contact support for more details.');
					return false;
				}else{
					$('#loading').hide();
					$('#confirm').removeClass('error');
					$('#confirm').addClass('confirm');
					$('#confirm').show();
					$('#confirm').html('Password has been e-mailed to you at ******' + retVal + '<br /><br />(If not in INBOX, please check your SPAM and JUNK folders.)');
				}
			}
		});
		return false;		
		
		
	});
	
		
	
});


function confirmDelete(url){
    if(window.confirm("Are you sure? Click YES to proceed or CANCEL to abort.")){
        window.location.href = url;
    }
}

function goState(state){
    window.location.href = 'http://www.militaryforsaleforrent.com/state/' + state + '.html';   
}
