// Paper calculator 

// AJAX function
function _makeRequest(url, divid, errormsg)
{
	var theDiv = $('#'+divid);	
	$.ajax({
		url: url,
		type: 'GET',
		success: function(data){
			theDiv.html(data);
			switch(theDiv.attr('id')){
				case 'loadcalculation':
					break;
				case 'loadaddress':
				case 'loadtowns':
					if($('mod_bnch_town')) {
						if(theDiv.attr('id') == 'loadtowns'){
							mod_getBranch('loadaddress');
						}
					}else{
						$('#loadaddress').html('');
					}
					break;
				default:
					theDiv(errorMsg);
					break;
			}
		} 
	}); 
	
}

function _errorLabel(labelID)
{
	document.getElementById(labelID).style.color="#CC0000";
}

function _normalLabel(labelID)
{
	document.getElementById(labelID).style.color="#000";
}


function checkCalculator()
{
	var submitForm = true;
	var width = document.getElementById('mod_pc_width').value;
	var height = document.getElementById('mod_pc_height').value;
	var gsm = document.getElementById('mod_pc_gsm').value;
	var price = document.getElementById('mod_pc_price').value;
	
	if (width == "" || height == "" || isNaN(width) || isNaN(height)) {
		submitForm = false;
		_errorLabel('lblSize');
	} else {
		_normalLabel('lblSize');
	}
	if (gsm == "" || isNaN(gsm)) {
		submitForm = false;
		_errorLabel('lblGsm');
	} else {
		_normalLabel('lblGsm');
	}
	if (price == "" || isNaN(price)) {
		submitForm = false;
		_errorLabel('lblPrice');
	} else {
		_normalLabel('lblPrice');
	}
		
	if (submitForm) {
		mod_paperCalculator('loadcalculation');
	}
	return false;
}



function submodules() {

	var h = $("body.home");
	if(h && h.length > 0) {
		return;
	}
	
	$(".addsubmodule").remove();

	$(".module:not('.no-mngr')").each(function() {

		var modulekey = submodules_getmodulekey($(this).attr('class'));

		if(!submodules_hasmodule(modulekey)) {
			var $add = $('<a href="#" title="Add this to your homepage" class="addsubmodule">+</a>');
			$add
				.attr('_modulekey', modulekey)
				.click(function(event) {
					event.preventDefault();
					submodules_add($(this).attr('_modulekey'));
					$(this).fadeOut(500);
				});

			$(this).find('.header').append($add);
		}	
		
		
	});

	function submodules_getmodulekey(className) {
		var classes = className.split(' ');
		for(var i=0, j=classes.length; i<j; i++) {
			if(classes[i].indexOf('mod-') == 0 && classes[i] != 'mod-news') {
				return (classes[i]);
			}
		}
	}


	function submodules_hasmodule(key) {
		for(var i=0, j=_rh_lumodules.length; i<j; i++) {
			if(_rh_lumodules[i][2] == key) 
				return true;
		}
		return false;
	}

	function submodules_add(key) {
		$.ajax({
			url: '/ajax/module-manager.php',
			type: 'POST',
			data: 'request=moduletoggle&state=on&key='+key,
			success: function(data){
				$msg=$('<div class="moduleupdatemessage">Added to your home page</div>');
				$msg.hide();
				var addedMod = $('a[_modulekey="'+key+'"]').parents('.module').find('h3');
				$msg.insertAfter(addedMod).slideDown("medium");
				window.setTimeout(submodules_hidemessages, 3000);
			},
			error: function(data){
				//console.log(data);
			}
		});		
	}

	function submodules_hidemessages() {
		$('.moduleupdatemessage').slideUp();
	}
	
}