// JavaScript Document

$(document).ready(function(){ 
        $("ul.sf-menu").supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    35,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish();  // call supersubs first, then superfish, so that subs are 
                         // not display:none when measuring. Call before initialising 
                         // containing tabs for same reason. 
		
		// For Autosuggest
		
		var options_xml = {
			script: function (input) { return "/ajax/search/" + input; },
			varname:"input",
			entries:10,
			timeout:6000,
			delay:10,
			cache:false
		};
		var as_xml = new bsn.AutoSuggest('NoteQ', options_xml);
		
		// To validate contact form
		
		$(".numeric").format({precision: 14,autofix:false});
		
		// Site search functionality
		
		$("#NoteQ").focus(function() {
			var t = $("#NoteQ").val();
			if (t == 'search site') {
				$("#NoteQ").val('');
			}
		});
		
		$("#NoteQ").blur(function() {
			var t = $("#NoteQ").val();
			if (t == '') {
				$("#NoteQ").val('search site');
			}
		});

		// Round only IE4.0+

		if (!document.all)
			$('.rounded').corners();
		
		
		// Cycle ticker on splash page
		
		if($('#ticker-cycle').length) {
			$("#ticker-cycle").cycle({
				fx: 'fade',
				pause: true,
				timeout: 5000
			});		
		}
		
		if($(".tbpopup").length) {
			
			content = document.getElementById('thickboxcontent').innerHTML;
			
			jQuery.facebox(content);
			
		}

		$("#accordion").accordion({ header: "h3" });
		
		
		$("#contactussubmit").click(function() {  
     		// validate and process form here  
			$('.error').hide();  
			var fname = $("input#fname").val();  
			if (fname == "") {  
			   $("label#fname_error").show();  
			   $("input#fname").focus();  
			   return false;  
			}  
			var lname = $("input#lname").val();  
			if (lname == "") {  
			   $("label#lname_error").show();  
			   $("input#lname").focus();  
			   return false;  
			}  
			var company = $("input#company").val();  
			if (company == "") {  
			   $("label#company_error").show();  
			   $("input#company").focus();  
			   return false;  
			}  			
			var email = $("input#email").val();  
			if (email == "") {  
			   $("label#email_error").show();  
			   $("input#email").focus();  
			   return false;  
			}  
			var position = $("input#position").val();
			var phone    = $("input#phone").val();
			var comments = $("textarea#comments").val();
			
			
			var cbs = [];
			$("input[name^='interests']").each(function() {
				if (this.checked) { cbs.push(this.value); }
			});
			
			var len = cbs.length;
			var dataString = 'fname='+ fname + 
							 '&lname=' + lname + 
							 '&company=' + company + 
							 '&position=' + position + 
							 '&email=' + email + 
							 '&phone=' + phone + 
							 '&comments=' + comments;  
	
			for(i=0;i<len;i++) {
				dataString += '&interests[' + i + ']=' + cbs[i];
			}
			$("#contactussubmit").attr('disabled', 'disabled'); 
			$("#contactussubmit").val('Sending...');
			$("#cancelstatus").html('<img src="/images/indicator_blue_small.gif" />');
			
			$.ajax({  
			  type: "POST",  
			  url: "/ajx/contactus_submit.php",  
			  data: dataString,  
			  success: function() {  
			  	window.scrollTo(0,0);
				
				$('#contactform').html("<div id='message'></div>");  
				$('#message').html("<h2>Contact Form Submitted!</h2><div id='message2'></div>")
				.append("<p>Please check your email for an email verification.</p>")  
				.hide()  
				.fadeIn(1500, function() {  
				  $('#message').append("<img id='checkmark' src='/images/check.png' />");  
				});  
			  }  
			});  
			
			
			return false; 			
		 }); 
}); 

function ajaxSubmitComment(val) {
	dataString = $('#CommentAddForm').serialize();
	
	htmlstring  = '<h1>Add Comment</h1>';
	
	htmlstring += '<div style="text-align: center; width: 500px;" >';
	htmlstring += '<p>Please wait while we add your comment.</p>';
	htmlstring += '<div style="width: 34px; margin: 0 auto;" ><img src="/img/indicator_blue_large.gif" alt="loading anim" /></div>';
	
	htmlstring += '</div>';
	
	$('#inlineCommentforTB').html(htmlstring);

	$.ajax({  
	  type: "POST",  
	  url: val.action ,  
	  data: dataString,  
	  success: function(data) {  
		if (data.indexOf('commentadded') != -1) {


			/*

			list  = document.getElementById("comments_ordered_list");
			newel = document.createElement("li");
			
			var newDate = new Date;
			var tempID  = newDate.getTime();
				alert(tempID);
				
				var tempID = new String (tempID);
				
				alert(tempID.length);
				
				lengthOfId = tempID.length - 5;
				alert(lengthOfId);
			    tempID     = tempID.substr( lengthOfId );
				 
				alert(tempID);
				
			newel.setAttribute('id', tempID);
			newel.className = 'tempCommentStyle';
			
			list.insertBefore(newel, list.firstChild)
			content = '<p>hello</p>';
			
			$('#' + tempID ).append(content);
			
			*/

			htmlstring  = '<div style="text-align: center; width: 500px;" >';
			htmlstring += '<p>Your comment has been successfully added. Your comment must be approved before it is visible online.</p>';
			htmlstring += '</div>';
			
			$('#inlineCommentforTB').html(htmlstring);

		}
		else if (data.indexOf('commentfailed') != -1) {
			
			htmlstring  = '<div style="text-align: center; width: 500px;" >';
			htmlstring += '<p>There was a problem adding your comment. Please try again later.</p>';
			htmlstring += '</div>';
			
			$('#inlineCommentforTB').html(htmlstring);			
		}
	  }  
	});  	
	

	return false;


}	
	
function emailStory(val) {
	dataString  = $('#sendStoryAsEmail').serialize();
	
	htmlstring  = '<div style="text-align: center; width: 500px;" >';
	htmlstring += '<p>Please wait while we send your message.</p>';
	htmlstring += '<div style="width: 34px; margin: 0 auto;" ><img src="/img/indicator_blue_large.gif" alt="loading anim" /></div>';
	
	htmlstring += '</div>';
	
	
	$('#inlineCommentforTB').html(htmlstring);
	
	
	
	$.ajax({  
	  type: "POST",  
	  url: val.action ,  
	  data: dataString,  
	  success: function(data) {  
		if (data.indexOf('emailsent') != -1) {

			htmlstring  = '<div style="text-align: center; width: 500px;" >';
			htmlstring += '<p>Your message has been successfully delivered. You must wait 3 minutes before emailing another story.</p>';
			htmlstring += '</div>';
			
			$('#inlineCommentforTB').html(htmlstring);

		}
		else if (data.indexOf('emailsenderror') != -1) {
			
			htmlstring  = '<div style="text-align: center; width: 500px;" >';
			htmlstring += '<p>There was a problem delivering your message. Please try again later.</p>';
			htmlstring += '</div>';
			
			$('#inlineCommentforTB').html(htmlstring);			
		}
	  }  
	});  	
	

	return false;


}

function cfReset() {
	if (confirm("Are you sure you want to reset?")) {
		document.contactus.reset();
		document.getElementById("fname").focus();
	}
}




function sessionTimeOut() {
	//alert('Your session has timed out.');
	
	content  = '<div style="padding: 20px;"> ';
	content += 'Your session has timed out, please log back in.';	
	content += '</div>';	
	content += '<br/>';	
	
	jQuery.facebox(content);	
	
}

$(function() {
	$("input[name='phone']").keyup(function() {
		var curchr = this.value.length;
		var curval = $(this).val();
		if (curchr == 3) {
			$("input[name='phone']").val("(" + curval + ")" + "-");
		} else if (curchr == 9) {
			$("input[name='phone']").val(curval + "-");
		}
	});
});

