
$(function() {

	/** search button **/
	$('.searchbox').focus(function() {
		  if(this.value = 'Site search'){
			  this.value = '';
			  $(this).css('color','black');
		  }
	});
	/**
	 * This is a call to calendar to change a month/year
	 */
	$('.cal_month').click(function(e) {
		e.preventDefault();
		var mm_yyyy = $(this).attr('href');
		//alert(mm_yyyy);
		$.ajax({
			type: 'GET',
			  url: "./PHP/calendar/showevents.php",
			  data: {
				    month: mm_yyyy
				  },
			  dataType: "xml",
	 		  success: function(xml){
	 			// alert(xml);
	 		  	$("a.prev").attr('href',$(xml).find('Prev').text());
	 		  	$("span.curr").html($(xml).find('Curr').text());
	 		  	$("a.po").attr('href',$(xml).find('Next').text());
	 		  	$("span.cal-data").html($(xml).find('Data').text());
	 		  	
	 		  }, // The error handler.
			  error: function( xhr, ajaxOptions, thrownError ){
				  alert(xhr.status);
                  alert(thrownError);
			  }
		}); 		  
	});
	$('a.pttrigger').popupWindow({ 
		centerScreen:1,
		scrollbars:1,
		height:750,width:750,toolbar:0,status:0,resizable:0
	}); 
	var url = $(location).attr('href');
	if(!url.match('resultpage')){
		$('.comment_body').hide();
	}
	// toggles the slickbox on clicking the noted link  
	$('#toggleComments').click(function() {
		/*if($(this).html() == 'View comments') {
			$(this).html('Hide comments');
		} else {
			$(this).html('View comments');
		}*/
	    $('.comment_body').toggle(400);
	    return false;
	});

	/** comment form **/
	// load the modal window
	$('#writeComment').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#name').val('Your name');
		$('#email').val('Your email address');
		$('#comment').val('Enter your comment ...');

		//show the mask and contact divs
		$('#mask').css('height',$(document).height());
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	$('#contactForm input[type!=submit]').focus(function() {
		$(this).val(' ');
	});
	
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
		var username = $('#name').val(),
			email = $('#email').val().toLowerCase(),
			comment = $('#comment').val(),
			sid = $('#sid').val();
		
		//Error Count
		var error_count=0;
		
		//Regex Strings
		var username_regex = /^[a-z ]{5,50}$/,
			email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
			//Test Username
			if(username == '' || username == 'Your name') {
				$('#contact_header').after('<p class=error>Invalid name entered!</p>');
				error_count += 1;
			}
			
			//Test Email
			if(!email_regex.test(email)) {
				$('#contact_header').after('<p class=error>Invalid email entered!</p>');
				error_count += 1;
			}
			
			//Blank Comment?
			if(comment == '' || comment == 'Enter your comment ...') {
				$('#contact_header').after('<p class=error>No Comment was entered!</p>');
				error_count += 1;
			}

			//No Errors?
			if(error_count === 0) {

				$.ajax({
					type: "post",
					url: "/PHP/send.php",
					data: "name=" + username + "&email=" + email + "&comment=" + comment + "&sid=" + sid,
					error: function(record) {
						$('.error').hide();
						$('#sendError').slideDown('slow');
					},
					success: function (record) {
						$('.error').hide();
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
					}				
				});	
			}
			
			else {
                $('.error').show();
            }
			
		return false;
	});
	/** end comment **/

	/** for travel blast **/
	if($('#segmentation').length > 0){
	  $('#segmentation').css("display","none");
	}
	if($("input[name=sendTo]:checked").val() > 0) {
    	$("#segmentation").css("display","block");
    } else {
    	$("#segmentation").css("display","none");
    }
    $(".send").live("change", function(){
    	if($("input[name=sendTo]:checked").val() > 0) {
        	$("#segmentation").css("display","block");
        } else {
        	$("#segmentation").css("display","none");
        }
    });
    if($("#file1").length > 0){          
        $("#removefile").css("visibility","hidden");
    } else {
    	$("#removefile").css("visibility","visible");
    }
	$("#removefile").live("click", function(){
    	$("#addfile").html("<input type='file' name='file' id='file1' />");
    	$("#removefile").css("visibility","hidden");
	});
    $("#file1").live("change",function() {

        $(this).upload("http://travelpress.com/NEWS/Travel_Blast/upload.php", function(data) {
			response = data.split('|');
			$.each(data, function(i, res){
				if(response[0] == "Success"){
					
					var message = "File uploaded: "+response[1]+
					"<input type='hidden' name='message_file' value='"+response[1]+"'>";
					$("#addfile").html(message);
					$("#removefile").css("visibility","visible");
				} else {
					alert(response[0] + " " + response[1]);
				}

			});

					
        }, "text");
    });
   $("#company").autocomplete("http://travelpress.com/NEWS/Travel_Blast/company.php", {
		width: 260,
		selectFirst: false
	});
	
});
$(window).resize(function() {
    ww=$(window).width();
        w=Math.round(ww/2);
    $('#nav-wrap-left').css({'width':w});
    $('#nav-wrap-right').css({'width':w});
    location.reload();
});

