// JavaScript Document
function $( name ){
	return document.getElementById( name );
}

function confirmAction(){
	if( confirm( "Are you sure you want to do this?" ) ) return true;
	else return false;
}

function delegate( that, thatMethod ){
    return function() { return thatMethod.call(that, arguments ); }
    //return function() { return thatMethod.apply(that, arguments ); }
}

function registerClearDefault( inpt, defTxt ){
	inpt.onfocus = function(){ inptClearDefault( inpt, true, defTxt ); }
	inpt.onblur = function(){ inptClearDefault( inpt, false, defTxt ); }
}

function inptClearDefault( inpt, isfocus, txt ){
	if( isfocus ){
		if( inpt.value == txt ) inpt.value = "";
	}else{
		if( inpt.value == "" ) inpt.value = txt;
	}
}

function popDownload( type, id  ){
	var strWidth = 500;//560;
	var strHeight = 420;//295;
	var leftStr = (screen.width-strWidth)/2;
	var topStr = (screen.height-strHeight)/2-100;
	windowProperties = "toolbar=no,menubar=no,titlebar=no,directories=no,fullscreen=no,scrollbars=yes,statusbar=no,status=no,location=no,resizable=no,height="+strHeight+",width="+strWidth+",left="+leftStr+",top="+topStr;
	window.open( "/pop-download-" + type + "?mediaid=" + id, type + '_Download',windowProperties);
}

function checkRegistrationForm( form ){
	form.reg_username.value = clean( form.reg_username.value );
	if( form.reg_username.value.length < 4 || form.reg_username.value.length > 15 ){
		alert( "Usernames must be between 4 and 15 alphanumeric characters" );
		form.reg_username.focus();
		return false;
	}else if( form.reg_email.value.length < 4 ){
		alert( "Please enter a valid email address" );
		form.reg_email.focus();
		return false;
	}else if( form.reg_password.value.length < 5 || form.reg_password.value != form.reg_password2.value ){
		alert( "Your password must be a minimum of 5 characters and must match the password confirmation" );
		form.reg_password.focus();
		return false;
	}else if( form.reg_captcha.value.length < 6 ){
		alert( "Please enter the verification code" );
		form.reg_captcha.focus();
		return false;
	}else if( !form.reg_terms.checked ){
		alert( "Please indicate that you have read our terms and privacy policy" );
		form.reg_terms.focus();
		return false;
	}
	
	return true;
}

function clean( txt ){
	return txt.replace( / /g, "" );
}

