// Javascript document

var defaultSearchValue = "Search";

$(document).ready(function() {

	// Bind Input masks

	$(".telMask").mask("(999) 999-9999");
	
	// Bind CSS states
	
	$(".m-wp-faqlist > h2").bind('click', function() {
		$(this).next().slideToggle('fast');
	});
	$(".m-mail").bind('click', function() {
		$("#wp-mail").toggleClass('m-offs');
		return false;
	});
	$("#lnkSendToFriend").bind('click', function() {
		$.scrollTo('#wp-mail');
		$("#wp-mail").toggleClass('m-offs');
		return false;
	});
	
	// Searchbox Click Behavior
	
	defaultSearchValue = $('.txt-box-srch').val();
	$('.txt-box-srch').bind({
		click: function() {
			if($(this).val() == defaultSearchValue) {
				$(this).val('');
			}
		},
		blur: function() {
			if($(this).val() == '') {
				$(this).val(defaultSearchValue);
			}
		}
	});
	
	// Access Client Zone
	
	$('#clientzonelink').bind('click', function () {
		$(document.body).append('<div id="ltbox_black"></div>');
		$('#ltbox_black').css({
			'height': $(document).height(),
			'opacity': 0.70
		}).hide();
		$('#ltbox_black').fadeIn('normal', function () {
			$('#ltbox_content').css('display', 'block');
			$('#ltbox_content').hide();
			$('#ltbox_content').fadeIn();
		});
		return false;
	});
	$('#ltbox_content div:first-child a').bind('click', function () {
		$('#ltbox_content').fadeOut('normal', function () {
			$('#ltbox_content').css('display', 'none');
			$('#ltbox_black').fadeOut('normal', function () {
				$('#ltbox_black').remove();
			});
		});
		return false;
	});
	
	// Preload images
	
	pic1 = new Image(); 
	pic1.src = "inc/img/ajaxload.gif"; 
	
});

function doMainSearch() {
	window.location = 'http://www.genivar.com/en/Pages/Search.aspx?k=' + $('.txt-box-srch').val();
	return false;
}

function sendFriendEmail() {
	
	// Get the querystring
	
	$('#__VIEWSTATE, #__EVENTVALIDATION, #__PREVIOUSPAGE, #__EVENTARGUMENT,  #__EVENTTARGET').remove();
	var qrystring = $('#aspnetForm').serialize();
	
	// Show Load UI
	
	$('#wp-mail input, #wp-mail textarea').attr('disabled','disabled');
	$('#stflastrow > input[type=button]').before('<img src="inc/img/ajaxload.gif" alt="Loading" style="vertical-align:middle;" /> ');
	
	// Do the request
	
	$.ajax({
	   type: "POST",
	   url: "SendToFriend.aspx?jobid=" + getQueryString()['id'] + "&curlang=" + $('#stflastrow > input[type=hidden]').val(),
	   data: qrystring,
	   success: function(rtndata){
		var datarow = rtndata.split('|');
		 $('#wp-mail input, #wp-mail textarea').attr('disabled','');
		 alert(datarow[1]);
		 $('#stflastrow > img').remove();
		 if(datarow[0] == "OK") {
			$('#wp-mail input[type=text], #wp-mail textarea').val('');
			$('#wp-mail').toggleClass('m-offs');
		 }
	   },
	   error: function(XMLHttpRequestobj, textStatusobj, errorThrownobj) {
		$('#wp-mail input, #wp-mail textarea').attr('disabled','');
		$('#stflastrow > img').remove();
		alert("The request to the server failed. Try again later. (" + textStatusobj + ": " + errorThrownobj + ")");
	   }
	   
	 });
	
	return false;

}

function getQueryString() {
  var result = {}, queryString = location.search.substring(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

