/* 
 * Some global inits here
 * 
 * If you don't find what you are looking for here it might be
 * in "Default.vm" due to the $link/$content bug
 */

$(function(){
	
	/*
	 * This is the form that shows when a seller has applied to be a seller
	 * for a magazine or a user for a company.
	 */
    $("form.accept-form").mtAcceptForm().mtAjaxForm({
		callBack: afterAllowDenyUser
	});
	
	
	/*
	 * All regular ajax form with no callbacks and so on
	 */
	$("form.ajax-form").mtAjaxForm();
    
	
	/*
	 * All non ajax forms. This is for validation purpose.
	 */
    $("form.standard-form").submit(function(){
        return $(this).validation().validate();
    });
	
	
	/*
	 * The contact form that is sliding in from the right needs a special callBack
	 * that is defined belowe.
	 */
	$("#contact-form").mtAjaxForm({
		callBack: closeContactForm
	});
	
	
	/*
	 * This is for zebra tables. The styling is done in common-system-style.css
	 */
    $("table.zebra-table tr:odd").addClass("odd-row");
	
	
	/*
	 * This is for all the date inputs. Give a input field the class .datepicker
	 * and you are good to go!
	 */
	$("input.datepicker").datepicker({
		
		closeText: 'Stäng',
        prevText: '&laquo;Förra',
		nextText: 'Nästa&raquo;',
		currentText: 'Idag',
        monthNames: ['Januari','Februari','Mars','April','Maj','Juni',
        'Juli','Augusti','September','Oktober','November','December'],
        monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun',
        'Jul','Aug','Sep','Okt','Nov','Dec'],
		dayNamesShort: ['Sön','Mån','Tis','Ons','Tor','Fre','Lör'],
		dayNames: ['Söndag','Måndag','Tisdag','Onsdag','Torsdag','Fredag','Lördag'],
		dayNamesMin: ['Sö','Må','Ti','On','To','Fr','Lö'],
        dateFormat: 'yy-mm-dd', firstDay: 1,
		isRTL: false,
		showWeek: true
	});
	
	
	/*
	 * This adds the text "(OBLIGATORISKT)" 
	 * to all fields with label-class required.
	 */
  	$('.required').append('<strong>&nbsp;(OBLIGATORISKT)</strong>');
	
	  
});


/*
 * Callback that destroys the deny/approve seller/user box
 */
afterAllowDenyUser = function(response){
	
	if(response.success){
		
		var id="#confirm-box-" + response.delete_id;
		$(id).slideUp("2000", function(){
        	$(this).remove();
        });
		
	}	
}


/*
 * Callback for the contact form that is sliding in from the right
 * 
 * @param response A json string that contains success: "true/false"
 * 
 * @return
 */
closeContactForm = function(response){
	
	if(response.success) {
		var slidingDiv = $(this).parent("div.sliding-div");
		slidingDiv.tabSlideOut.slideIn();
		clearForm($("#contact-form"));
	}
	
};

/*
 * Global function that can be called to clear a form
 * 
 * @param form jQuery object representing the form to clear
 * 
 * @return
 */
clearForm = function(form){
	
	$("input[type='text'], input[type='password'], textarea", form).each(function(){
		$(this).val("");
		$(this).removeAttr("disabled");
	});
	
	$("input[type='checkbox']", form).attr("checked", false);
};


/*
 * Global function that can be called to strip HTML tags from a string
 * with string.stripHTML();
 */
 String.prototype.stripHTML = function(){
   
	// What a tag looks like
	var matchTag = /<(?:.|\s)*?>/g;
	
	// Replace the tag
	return this.replace(matchTag, "");

};
	
