
function HideContent(d) {
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	document.getElementById(d).style.display = "block";
}
function ReverseDisplay(d) {
	if(document.getElementById(d).style.display == "none") { 
		document.getElementById(d).style.display = "block"; 
	}else { 
		document.getElementById(d).style.display = "none"; 
	}
}


function unsubscribe(){
	error = false;
	
	if( $('f_email').get('value') != "" ){
		if( Validate_Email_Address( $("f_email").value ) == false ){
			alert(ERR_WRONG_EMAIL);
			error = true;
		}
	}else{
		alert(ERR_WRONG_EMAIL);
		error = true;	
	}

	if( !error ){
		$('frmUnsubscribe').submit();
	}
}

function comment(){
	error = false;

	if( $('remark').get('value') == "" ){
		alert(ERR_WRONG_REMARK);
		error = true;
	}

	if( !error ){
		$('frmComment').submit();
	}		
}

function contact(){
	error = false;
	
	if( $('email').get('value') != "" ){
		if( Validate_Email_Address( $("email").value ) == false ){
			alert(ERR_WRONG_EMAIL);
			error = true;
		}
	}else{
		alert(ERR_WRONG_EMAIL);
		error = true;	
	}

	if( $('name').get('value') == "" ){
		alert(ERR_WRONG_NAME);
		error = true;
	}

	if( $('remark').get('value') == "" ){
		alert(ERR_WRONG_REMARK);
		error = true;
	}

	if( !error ){
		$('frmContact').submit();
	}	
}

function login(){
	error = false;
	
	if( $('email').get('value') == "" ){
		alert(ERR_WRONG_EMAIL);
		error = true;	
	}


	if( $('pass').get('value') == "" ){
		alert(ERR_WRONG_PASS);
		error = true;
	}

	if( !error ){
		$('frmLogin').submit();
	}	
}

function createAccount(){
	error = false;
	
	if( $('m_email').get('value') != "" ){
		if( Validate_Email_Address( $("m_email").value ) == false ){
			alert(ERR_WRONG_EMAIL);
			error = true;
		}
	}else{
		alert(ERR_WRONG_EMAIL);
		error = true;	
	}


	if( $('m_name').get('value') == "" ){
		alert(ERR_WRONG_NAME);
		error = true;
	}

	if( $('m_pass').get('value') == "" ){
		alert(ERR_WRONG_PASS);
		error = true;
	}

	if( !error ){
		var myRequest = new Request.HTML({ 
			'evalScripts' : 'false', 
			'method': 'post', 
			'noCache': 'true', 
			'update' : 'reg_status',
			'url' : 'assets/pages/checkUsername.php',
			'onSuccess' : function(responseTree, responseElements, responseHTML, responseJavaScript){
				if( responseHTML == "" ){
					$('reg_status').setStyle('display','none');
					$('frmCreateAccount').submit();
				}else{
					$('reg_status').setStyle('display','block');
				}
			}
			}).post( $('frmCreateAccount') );
	}		
}

function deleteAccount(){
	recoverLostPassword();
}

function recoverLostPassword(){
	error = false;
	
	if( $('f_email').get('value') != "" ){
		if( Validate_Email_Address( $("f_email").value ) == false ){
			alert(ERR_WRONG_EMAIL);
			error = true;
		}
	}else{
		alert(ERR_WRONG_EMAIL);
		error = true;	
	}

	if( !error ){
		$('frmLostPassword').submit();
	}
}

function sendSubscription(){
	error = false;
	
	if( $('sub_name').get('value') == $('sub_name').get('rel') ){
		alert(ERR_WRONG_NAME);
		error = true;
	}

	if( $('sub_name').get('value') == "" ){
		alert(ERR_WRONG_NAME);
		error = true;
	}

	if( $('sub_email').get('value') == $('sub_email').get('rel') ){
		alert(ERR_WRONG_EMAIL);
		error = true;
	}
	
	if( $('sub_email').get('value') != "" ){
		if( Validate_Email_Address( $("sub_email").value ) == false ){
			alert(ERR_WRONG_EMAIL);
			error = true;
		}
	}else{
		alert(ERR_WRONG_EMAIL);
		error = true;	
	}
	
	if( !error ){
		var myRequest = new Request.HTML({ 
			'evalScripts' : 'false', 
			'update': 'ajax_subscribe',
			'method': 'post', 
			'noCache': 'true', 
			'url' : 'assets/pages/subscribe.php'
			}).post( $('frmSubscribe') );
	}
}

function Validate_Email_Address(email_address){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (email_address.search(validRegExp) == -1){
		return false;
	}else{
		return true;
	}
}

function getSelectVal( selectID ){
	var selectOne = $(selectID);
	return selectOne.options[selectOne.selectedIndex].value;
}

function checkLength( theNumber ){
	if( ( theNumber.toString().length > 6 ) || (theNumber == 0) ){
		return "-";
	}else{
		return theNumber;
	}
}
 
function showHideOptions(){
	targets = ['startUnit_length', 'startUnit_temperature', 'startUnit_weight', 'startUnit_volume'];
	theMetric  = getSelectVal('metric');
	
	targets.each( function(item){
		if( item != ("startUnit_" + theMetric) ){
			$(item).setStyle ('display', 'none');
		}else{
			$(item).setStyle ('display', 'block');
		}
	});

	reCalc();
}

function reCalc(){
	$$('#converteer_results table tr td').each( function(el){
		$(el).empty();
	});
	
	theMetric  = getSelectVal('metric');
	
	if( theMetric == "length" ){
		calc_length();
		
	}else if( theMetric == "temperature" ){
		calc_temperature();
		
	}else if( theMetric == "weight" ){
		calc_weight();
		
	}else if( theMetric == "volume" ){
		calc_volume();
	}
}

function calc_volume(){
	startValue = ($('startVal').value).replace(/[^\d\^\.]/g,'');
	startUnit  = $('startUnit_volume').value;
	$('startVal').value = startValue;

	if( startUnit == "ml" ){
		liter 		= startValue * 0.001;

	}else if( startUnit == "cl" ){
		liter 		= startValue * 0.01;

	}else if( startUnit == "dl" ){
		liter 		= startValue * 0.1;

	}else if( startUnit == "liter" ){
		liter 		= startValue.toInt();

	}else if( startUnit == "oz" ){
		liter 		= startValue * 0.0295735296;

	}else if( startUnit == "pints" ){
		liter 		= startValue * 0.473176473;

	}else if( startUnit == "cups" ){
		liter 		= startValue * 0.236588236;

	}else if( startUnit == "quarts" ){
		liter 		= startValue * 0.946352946;

	}else if( startUnit == "gallons" ){
		liter 		= startValue * 3.78541178;
	}

	ml 		= liter * 1000;
	cl 		= liter * 100;
	dl 		= liter * 10;
	liter 		= liter;
	oz 		= liter * 33.8140227;
	pints 		= liter * 2.11337642;
	cups 		= liter * 4.22675284;
	quarts 		= liter * 1.05668821;
	gallons 	= liter * 0.264172052;
	
	$('con_1a').set('html', 'ml');
	$('con_1b').set('html', checkLength((ml).round(1)));
	
	$('con_2a').set('html', 'cl');
	$('con_2b').set('html', checkLength((cl).round(1)));
	
	$('con_3a').set('html', 'dl');
	$('con_3b').set('html', checkLength((dl).round(1)));
	
	$('con_4a').set('html', 'oz');
	$('con_4b').set('html', checkLength((oz).round(1)));	

	$('con_5a').set('html', 'pints');
	$('con_5b').set('html', checkLength((pints).round(1)));	

	$('con_6a').set('html', 'cups');
	$('con_6b').set('html', checkLength((cups).round(1)));	

	$('con_7a').set('html', 'quarts');
	$('con_7b').set('html', checkLength((quarts).round(1)));

	$('con_8a').set('html', 'gallons');
	$('con_8b').set('html', checkLength((gallons).round(1)));
}

function calc_weight(){
	startValue = ($('startVal').value).replace(/[^\d\^\.]/g,'');
	startUnit  = $('startUnit_weight').value;
	$('startVal').value = startValue;

	if( startUnit == "gram" ){
		gram    	= startValue.toInt();
		kilogram 	= startValue * 0.001;
		pounds 		= startValue * 0.00220462262;
		oz 		= startValue * 0.0352739619;

	}else if( startUnit == "kilogram" ){
		gram    	= startValue * 1000;
		kilogram 	= startValue.toInt();
		pounds 		= startValue * 2.20462262;
		oz 		= startValue * 35.2739619;

	}else if( startUnit == "pounds" ){
		gram    	= startValue * 453.59237;
		kilogram 	= startValue * 0.45359237;
		pounds 		= startValue.toInt();
		oz 		= startValue * 16;

	}else if( startUnit == "oz" ){
		gram    	= startValue * 28.3495231;
		kilogram 	= startValue * 0.0283495231;
		pounds 		= startValue * 0.0625;
		oz 		= startValue.toInt();
	}

	$('con_1a').set('html', 'gram');
	$('con_1b').set('html', checkLength((gram).round(1)));
	
	$('con_2a').set('html', 'kilogram');
	$('con_2b').set('html', checkLength((kilogram).round(1)));
	
	$('con_3a').set('html', 'pounds');
	$('con_3b').set('html', checkLength((pounds).round(1)));
	
	$('con_4a').set('html', 'oz');
	$('con_4b').set('html', checkLength((oz).round(1)));
}

function calc_temperature(){
	startValue = ($('startVal').value).replace(/[^\d\^\.]/g,'');
	startUnit  = $('startUnit_temperature').value;
	$('startVal').value = startValue;

	if( startUnit == "celsius" ){
		celsius    = startValue.toInt();
		fahrenheit = startValue * (9/5) + 32;

	}else if( startUnit == "fahrenheit" ){
		celsius    = (startValue - 32) * (5/9);
		fahrenheit = startValue.toInt();
	}
		
	$('con_1a').set('html', 'celsius');
	$('con_1b').set('html', checkLength( (celsius).round(1) ) );
	
	$('con_4a').set('html', 'fahrenheit');
	$('con_4b').set('html', checkLength( (fahrenheit).round(1) ) );	
	$('con_4a').setStyle('width', '60px');
}

function calc_length(){
	startValue = ($('startVal').value).replace(/[^\d\^\.]/g,'');
	startUnit  = $('startUnit_length').value;
	$('startVal').value = startValue;

	if( startUnit == "mm" ){
		meter 		= startValue * 0.001;

	}else if( startUnit == "cm" ){
		meter 		= startValue * 0.01;

	}else if( startUnit == "inche" ){
		meter 		= startValue * 0.0254;

	}else if( startUnit == "meter" ){
		meter 		= startValue.toInt();

	}else if( startUnit == "foot" ){
		meter		= startValue * 0.3048;

	}else if( startUnit == "yards" ){
		meter		= startValue * 0.9144;

	}else if( startUnit == "kilometer" ){
		meter		= startValue * 1000;

	}else if( startUnit == "mile" ){
		meter		= startValue * 1609.344;
	}

	mm 		= meter * 1000;
	cm 		= meter * 100;
	kilometer 	= meter * 0.001;
	inche 		= meter * 39.3700787;
	feet 		= meter * 3.2808399;
	yard 		= meter * 1.0936133;
	mile 		= meter * 0.000621371192;
	
	$('con_1a').set('html', 'mm');
	$('con_1b').set('html', checkLength( (mm).round(1)));
	
	$('con_2a').set('html', 'cm');
	$('con_2b').set('html', checkLength( (cm).round(1)));
	
	$('con_3a').set('html', 'meter');
	$('con_3b').set('html', checkLength( (meter).round(1)));
	
	$('con_4a').set('html', 'kilometer');
	$('con_4b').set('html', checkLength( (kilometer).round(1)));	

	$('con_5a').set('html', 'inch');
	$('con_5b').set('html', checkLength( (inche).round(1)));	

	$('con_6a').set('html', 'feet');
	$('con_6b').set('html', checkLength( (feet).round(1)));	

	$('con_7a').set('html', 'yard');
	$('con_7b').set('html', checkLength( (yard).round(1)));

	$('con_8a').set('html', 'mile');
	$('con_8b').set('html', checkLength( (mile).round(1)));	
}
