$().ready(function() {

    
	$(".links, .clicks").click(function() {
		window.location=$(this).find("a").attr("href");return false;
	});

    
	$.ajaxSetup ({  
		cache: false  
	});
    
    // Forms  ----------------------------------
    
	$("#county_id").change(function(){
       
        var id = $(this).val();			  
		var loadUrl = '/ajax/formTowns/' + id;
		var ajaxDiv = '#result_town_id';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);

	});
    
    // Alpha Categories ----------------------------------
    $("#alpha_cats").hide();
    
    $("#cat_search").click(function(){
            $("#alpha_county").hide();
            $("#alpha_town").hide();
	        $("#alpha_cats").show();
            
            document.getElementById("cat_search").value = '';
	});
    
    $("#alpha_cats li").click(function(){			
			var id = $(this).attr("id");
            callAlphaCategories(id);
	});

    function callAlphaCategories(id){ 

        var loadUrl = '/ajax/searchAlphaCategories/' + id;
		var ajaxDiv = '.alpha_cats';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);
        
    }
    
    // Alpha Counties ----------------------------------
    $("#alpha_county").hide();
    
    $("#county_search").click(function(){
            $("#alpha_cats").hide();
            $("#alpha_town").hide();
	        $("#alpha_county").show();
            
            // Put message in county
            document.getElementById("county_search").value = '';
            
            // First reset the town
            document.getElementById("town_search").value = 'click to select a town' ;
            
            //Dont need alpha search
            callAlphaCounties();
	});
 
    function callAlphaCounties(){ 

        var loadUrl = '/ajax/searchAlphaCounties/';
		var ajaxDiv = '.alpha_county';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);
        
    }
    
    // Alpha Towns ----------------------------------
    $("#alpha_town").hide();
    
    $("#town_search").click(function(){
            $("#alpha_cats").hide();
            $("#alpha_county").hide();
	        $("#alpha_town").show();
            
            document.getElementById("town_search").value = '';
            
            //Dont need alpha search
            callAlphaTowns();
	});
 
    function callAlphaTowns(){ 
    
        var county = document.getElementById("county_search").value;
        
        var loadUrl = '/ajax/searchAlphaTowns/' + county;
		var ajaxDiv = '.alpha_town';
        
		$.get(
			loadUrl,
			{language: "php", version: 5},
			function(responseText){
				$(ajaxDiv).html(responseText);
			},
			"html"
		);
        
    }
    
    // Company Name
    $("#company_name_search").click(function(){		
            $("#alpha_cats").hide();
            $("#alpha_town").hide();
	        $("#alpha_county").hide();
			document.getElementById('company_name_search').value = '';
	});
 

        
});

// Only works outside of jquery scope

// Update category field
function updateCat($category){
    document.getElementById("cat_search").value = $category;
    $("#alpha_cats").hide();
}

// Update county field
function updateCounty($county){
    document.getElementById("county_search").value = $county;
    $("#alpha_county").hide();
    document.getElementById("town_search").value = 'select a town in ' + $this;
}

// Update town field
function updateTown($town){
    document.getElementById("town_search").value = $town;
    $("#alpha_town").hide();
}

function getRandomNum(lbound, ubound) {
	
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
	
}

function getRandomChar(number, lower, upper, other, extra) {
	
	var numberChars = "0123456789";
	var lowerChars = "abcdefghijklmnpqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = extra;
	
	if (number == true){
		charSet += numberChars;
	}
	
	if (lower == true){
		charSet += lowerChars;
	}
	
	if (upper == true){
		charSet += upperChars;
	}
	
	if (other == true){
		charSet += otherChars;
	}
	
	return charSet.charAt(getRandomNum(0, charSet.length));
	
}
	
function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther,latterNumber, latterLower, latterUpper, latterOther) {
	
	var rc = "";
	
	if (length > 0){
		rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
	}
	
	for (var idx = 1; idx < length; ++idx) {
		rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
	}
	
    if(document.getElementById("add_password").checked){
        document.getElementById("password").value = rc;
    }else{
        document.getElementById("password").value = '';
    }
    
}
