/**
 * Zozzer Common Javascript
 *
 * Copyright (c) 2007-2008, Zozzer Inc <info@zozzer.com>.
 *
 * This software is the confidential and proprietary information of
 * Zozzer. ("Confidential Information").
 *
 * @package    Web
 * @author     Aditya Khurana <akhurana@zozzer.com>
 * @copyright  2007-2008 Zozzer Inc
 * @link       http://www.zozzer.com
*/


// function for clearing the form on browser back
jQuery.fn.clearForm = function() {
  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
		 if (tag == 'form')
		   return jQuery(':input',this).clearForm();
		 if (type == 'text' || type == 'password' || tag == 'textarea')
		   this.value = '';
		 else if (type == 'checkbox' || type == 'radio')
		   this.checked = false;
		 else if (tag == 'select')
		   this.selectedIndex = -1;
  });
};

jQuery(document).ready(function() {
	anchor.init()
	textareaMaxlength.init();
});


// Function to slide to an anchor 
// add class anchor at your <a> tag
// add an id to your anchor

anchor = {
	init : function()  {
		jQuery("a.anchorLink").click(function () {	
			elementClick = jQuery(this).attr("href")
			destination = jQuery(elementClick).offset().top;
			jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		  	return false;
		})
	}
};

// Function to restrict textarea length to 'maxlength' attribute
// html element will class 'charsRemaining' is used to display the remaining length
// textarea and html element should share same parent
textareaMaxlength = {
	init : function() {
		jQuery('textarea[maxlength]').keyup(function(){
			var max = parseInt(jQuery(this).attr('maxlength'));
			if(jQuery(this).val().length > max){
				jQuery(this).val(jQuery(this).val().substr(0, jQuery(this).attr('maxlength')));
			}
			jQuery(this).parent().parent().find('.charsRemaining').html('<span>' + (max - jQuery(this).val().length) + '</span> characters left');
		});
	}
};

