// JavaScript Document

//-------------------------------------------------------------
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

//-------------------------------------------------------------
var sendingtext = "<center><div class='SubTitle'>Sending... Please Wait</div><img src='/images/loading1.gif' height='32' width='32' /></center>";
var loadingtext = "<center><div class='SubTitle'>Loading... Please Wait</div><img src='/images/loading1.gif' height='32' width='32' /></center>";
var logintext = "<center><div class='SubTitle'>Logging In... Please Wait</div><img src='/images/loading1.gif' height='32' width='32' /></center>";
var calendarloadtext = "<center><div class='ContentHeader'>Loading Calendar Please Wait<p><img src='/images/loading3.gif' /></p></div></center>";
var loadingtextsm = "<center><div class='ParTitle'>Loading... Please Wait</div><img src='/images/loading1.gif' height='32' width='32' /></center>";

function validateEmail(elementValue){  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return emailPattern.test(elementValue);  
}   


//****************************************************//
//* START OF JQUERY FUNCTIONS
//****************************************************//
$(document).ready(function(){
	
	function submit_contact_form() {  
		$("#ContactForm").fadeOut("fast", function() {
			$("#ContactForm").html("<center><b>Please Wait</b><br /><img src='/images/loading1.gif' /></center>");
			$("#ContactForm").fadeIn("fast");
		});
		
		$.ajax({
	    type: "POST",
  	  url: "/include/postcontact.php",
  	  enctype: 'multipart/form-data',
			data: $("#frmContactUs").serialize(),
			success: function(data) {
          $("#ContactForm").html(data);
          $("#ContactForm").fadeIn("normal");
					$(".btnSubmitContact").click(submit_contact_form);
			}  ,
			error: function(data) {
          $("#ContactForm").html("Error");
          $("#ContactForm").fadeIn("normal");
					$(".btnSubmitContact").click(submit_contact_form);
			}  
		});

	};
	
	$(".btnSubmitContact").click(submit_contact_form);
	
	/// CLIENTS LIST ///
  function client_category_click(){
		
		var catid = $(this).attr("cat");
		var service = $(this).attr("service");
		var sortby = $(this).attr("sortby");
		
		$(".clientcategory").removeClass("clientcategoryactive");
		$("#clientcategory"+catid).addClass("clientcategoryactive");

		$("#clientlistbox").fadeOut("fast", function() {
			$("#clientlistbox").html(loadingtext+'<p><center>If page does not load click <a href="clients.php?cat='+catid+'&service='+service+'&type=Client">here</a>.</center></p>');
			$("#clientlistbox").fadeIn("fast", function() {		
				$.ajax({
					type: "GET",
					url: "/include/clientlist.php",
					data: "cat="+catid+"&type=Client&service="+service+"&sort="+sortby,
					success: function(data){
						$("#clientlistbox").html(data);
						$("#clientlistbox").fadeIn("normal");
						$(".clientexpand").click(client_expand_click);
						$(".clientcategory").click(client_category_click);
					},
					error: function(data, status, error) {
						$("#clientlistbox").html("<div class='boxError'>Error: "+error+"</div>"+data);
						$("#clientlistbox").fadeIn("normal");
						$(".clientcategory").click(client_category_click);
					}
				});		
			});
		});

  };

  function client_expand_click(){
		
		var clientid = $(this).attr("clientid");
		var catid = $(this).attr("cat");
		var service = $(this).attr("service");
		
		$("#clientlistbox").fadeOut("fast", function() {
			$("#clientlistbox").html(loadingtext+'<p><center>If page does not load click <a href="clients.php?id='+clientid+'&cat='+catid+'&service='+service+'&type=Client">here</a>.</center></p>');
			$("#clientlistbox").fadeIn("fast", function() {		
				$.ajax({
					type: "GET",
					url: "/include/clientlist.php",
					data: "id="+clientid+"&cat="+catid+"&type=Client&service="+service,
					success: function(data){
						$("#clientlistbox").html(data);
						$("#clientlistbox").fadeIn("normal");
						$(".clientcategory").click(client_category_click);
					},
					error: function(data, status, error) {
						$("#clientlistbox").html("<div class='boxError'>Error: "+error+"</div>"+data);
						$("#clientlistbox").fadeIn("normal");
					}
				});		
			});
		});

  };	
	
	//$(".clientcategory").click(client_category_click);
	//$(".clientexpand").click(client_expand_click);

	
	
	//-----------------------------------------
	// NEWS RELEASES
	//-----------------------------------------
	
	function news_back_link() {
  	var offset = $(this).attr("offset");
    var member = $(this).attr("member");
		
  	$("#NewsFrame").fadeOut("fast");

		$.ajax({
			type: "GET",
			url: "/include/newslist.php",
			data: "offset="+offset+"&member="+member+"&size=Medium",
			success: function(data){
      	$("#NewsFrame").html(data);
        $("#NewsFrame").fadeIn("normal");
				$(".NewsPageLink").click(news_page_link);
				$(".NewsBackLink").click(news_back_link)
				$(".NewsLink").click(news_load_link);
			},
			error: function(data, status, error) {
        $("#NewsFrame").html("<div class='boxError'>Error: "+error+"</div>"+data);
        $("#NewsFrame").fadeIn("normal");
			}
		});    
  };
	
	
	function news_page_link() {
  	var offset = $(this).attr("offset");
    var member = $(this).attr("member");
    
  	$("#NewsFrame").fadeOut("normal", function() {
			$("#NewsFrame").html("");															 

			$.ajax({
				type: "GET",
				url: "/include/newslist.php",
				data: "offset="+offset+"&member="+member+"&size=Medium",
				success: function(data){
					$("#NewsFrame").fadeIn("normal");
					$("#NewsFrame").html(data);
					$(".NewsPageLink").click(news_page_link);
					$(".NewsBackLink").click(news_back_link)
					$(".NewsLink").click(news_load_link);
				},
				error: function(data, status, error) {
					$("#NewsFrame").html("<div class='boxError'>Error: "+error+"</div>"+data);
					$("#NewsFrame").fadeIn("normal");
				}
			});   
		});

  };
	
	
	function news_load_link() {
  	var newsid = $(this).attr("news");
  	var offset = $(this).attr("offset");
    var member = $(this).attr("member");
    
  	$("#NewsFrame").fadeOut("fast");

		$.ajax({
			type: "GET",
			url: "/include/newslist.php",
			data: "news="+newsid+"&offset="+offset+"&member="+member+"&size=Large",
			success: function(data){
      	$("#NewsFrame").html(data);
        $("#NewsFrame").fadeIn("normal");
				
				$(".NewsPageLink").click(news_page_link);
				$(".NewsBackLink").click(news_back_link)
				$(".NewsLink").click(news_load_link);
			},
			error: function(data, status, error) {
        $("#NewsFrame").html("<div class='boxError'>Error: "+error+"</div>"+data);
        $("#NewsFrame").fadeIn("normal");
			}
		});    
  };

	$(".NewsPageLink").click(news_page_link);
	$(".NewsBackLink").click(news_back_link)
	$(".NewsLink").click(news_load_link);	
	
	

		//******************* POLLS ********************//
		
		function save_poll_vote(){
			var pollid = $(this).attr("pollid");
			//alert(pollid);
			$("#PollBox"+pollid).fadeOut("normal", function() {  
				$.ajax({
					type: "POST",
					url: "/include/savepoll.php",
					enctype: 'multipart/form-data',
					data: $("#frmPoll"+pollid).serialize(),
					success: function(data) {
						$("#PollBox"+pollid).html(data);
						$("#PollBox"+pollid).fadeIn("normal");		
											
						$(".btnSavePoll").click(save_poll_vote);
						$(".btnShowPoll").click(show_poll);
						$(".btnPollResults").click(show_poll_results);
					}  ,
					error: function(data) {
						$("#PollBox"+pollid).html("Error");
						$("#PollBox"+pollid).fadeIn("normal");							
					}  
				});
			});		
		};			

		function show_poll_results(){
			var pollid = $(this).attr("pollid");
			var polllg = $(this).attr("polllg");
			//alert(pollid);
			$("#PollBox"+pollid).fadeOut("normal", function() {  
				$("#PollBox"+pollid).html(loadingtextsm);
				$("#PollBox"+pollid).fadeIn("normal", function() {  
					$.get("/include/pollresults.php", { poll: pollid, large: polllg},  function(data) {
						$("#PollBox"+pollid).fadeOut("normal", function() { 
							$("#PollBox"+pollid).html(data);
							$("#PollBox"+pollid).fadeIn("normal");				
									
							$(".btnSavePoll").click(save_poll_vote);
							$(".btnShowPoll").click(show_poll);
							$(".btnPollResults").click(show_poll_results);
						});
					});
				});
			});		
		};			
		
		function show_poll(){
			var pollid = $(this).attr("pollid");
			var polllg = $(this).attr("polllg");
			//alert(pollid);
			$("#PollFrame"+pollid).fadeOut("normal", function() {  
				$("#PollFrame"+pollid).html(loadingtextsm);
				$("#PollFrame"+pollid).fadeIn("normal", function() {  
					$.get("/include/showpoll.php", { poll: pollid, large: polllg},  function(data) {
						$("#PollFrame"+pollid).fadeOut("normal", function() {  
							$("#PollFrame"+pollid).html(data);
							$("#PollFrame"+pollid).fadeIn("normal");
							
							$(".btnSavePoll").click(save_poll_vote);
							$(".btnShowPoll").click(show_poll);
							$(".btnPollResults").click(show_poll_results);
						});							
					});
				});
			});		
		};			
		
		$(".btnSavePoll").click(save_poll_vote);
		$(".btnShowPoll").click(show_poll);
		$(".btnPollResults").click(show_poll_results);	
		
		
	/***********************************************/
	/**** AD REDIRETION ****************************/
	function redirect_ad_link() {
		var linkid = $(this).attr("lid");
		$.get("/include/redirectad.php", { id: linkid},  function(data) {
			if (data != "") {
				window.location = data;
				return false;
			}
		});
	};
	
	$(".lnkRedirectAdV").click(redirect_ad_link);
	$(".lnkRedirectAdH").click(redirect_ad_link);
});


 
