

Event.observe(window, 'load', function() 
{
	if ($('register-form'))
	{
		var v = new Validation('register-form',{stopOnFirst:false});	
		Validation.addAllThese([
			['validate-password', 'Your password must be more than 6 characters and not be password or the same as your name', {
				minLength : 7,
				notOneOf : ['password','PASSWORD','1234567','0123456'],
				notEqualToField : 'username'
			}],
			['validate-password-confirm', 'Your confirmation password does not match your first password, please try again.', {
				equalToField : 'password'
			}],
			['validate-unique-username', 'Please choose another username.', {
				notEqualToField : 'username_exists'
			}]														
		]);
	}
	if ($('upgrade-register-form'))
	{
		var v = new Validation('upgrade-register-form',{stopOnFirst:false});	
		Validation.addAllThese([
			['validate-password', 'Your password must be more than 6 characters and not be password or the same as your name', {
				minLength : 7,
				notOneOf : ['password','PASSWORD','1234567','0123456'],
				notEqualToField : 'username'
			}],
			['validate-password-confirm', 'Your confirmation password does not match your first password, please try again.', {
				equalToField : 'password'
			}],
			['validate-unique-username', 'Please choose another username.', {
				notEqualToField : 'username_exists'
			}],
			['validate-verification-code', 'Verification code is wrong.', {
				notEqualToField : 'code_exists'
			}]																		
		]);
	}
	if ($('login-form'))
	{
		
		var v = new Validation('login-form',{stopOnFirst:false});	
	}
	if ($('profile-form'))
	{
		
		var v = new Validation('profile-form',{stopOnFirst:false});	
	}
	if ($('user-change-password-form'))
	{
		
		var v = new Validation('user-change-password-form',{stopOnFirst:false});
		Validation.addAllThese([
			['validate-password', 'Your password must be more than 6 characters and not be password or the same as your name', {
				minLength : 7,
				notOneOf : ['password','PASSWORD','1234567','0123456'],
				notEqualToField : 'username'
			}],
			['validate-password-confirm', 'Your confirmation password does not match your first password, please try again.', {
				equalToField : 'new_password'
			}]														
		]);
			
	}
	if ($('password-retrieval-form'))
	{
		var v = new Validation('password-retrieval-form',{stopOnFirst:false});		
	}
	if ($('reset-token-form'))
	{
		var v = new Validation('reset-token-form',{stopOnFirst:false});	
		Validation.addAllThese([
			['validate-password', 'Your password must be more than 6 characters and not be password or the same as your name', {
				minLength : 7,
				notOneOf : ['password','PASSWORD','1234567','0123456']
			}],
			['validate-password-confirm', 'Your confirmation password does not match your first password, please try again.', {
				equalToField : 'password'
			}]														
		]);
				
	}	
	if ($('payment_form'))
	{
		var v = new Validation('payment_form',{stopOnFirst:false});		
	}			
});	

function checkAvailability(){
	new Ajax.Request(
			'is_available.php', 
			{
				method: 'get', 
				parameters: 'username='+$('username').value, 
				onComplete: showResponse
			});

    //eval(new Ajax.Updater('advice-validate-unique-username', 'is_available.php', {method: 'get', parameters:'username='+$('username').value}));
}

function checkCaptcha(){
	//alert($('recaptcha_response_field').value);
	new Ajax.Request(
			'check_captcha.php', 
			{
				method: 'post', 
				parameters: 'recaptcha_challenge_field='+$('recaptcha_challenge_field').value+'&recaptcha_response_field='+$('recaptcha_response_field').value, 
				onComplete: errorCaptcha
			});

    //eval(new Ajax.Updater('advice-validate-unique-username', 'is_available.php', {method: 'get', parameters:'username='+$('username').value}));
}


function errorCaptcha(originalRequest)
{
	//$('advice-validate-captcha').innerHTML = originalRequest.responseText;
	//alert(originalRequest.responseText);
	if (originalRequest.responseText == 'Invalid')
	{
		$('validate_captcha').value = 'Invalid';
		$('validate_captcha2').value = 'Invalid';
	}	
	else
	{
		$('validate_captcha').value = 'Valid';
		$('validate_captcha2').value = '';		
	}
}

function checkCaptchaCode(code){
	new Ajax.Request(
			'validate_code.php', 
			{
				method: 'post', 
				parameters: 'code='+code, 
				onComplete: showCodeResponse
			});
}

function showCodeResponse(originalRequest)
{
	$('advice-validate-code').innerHTML = '';
	if (originalRequest.responseText == 'Thanks, you entered the correct code.')
	{
		$('advice-validate-code').style.color = "green";
		$('code_exists').value = '';
	}
	else
	{
		$('advice-validate-code').style.color = "red";		
		$('code_exists').value = $('code').value;
	}
}


function showResponse(originalRequest)
{
	$('advice-validate-unique-username').innerHTML = originalRequest.responseText;
	if (originalRequest.responseText == '&nbsp;&nbsp;&nbsp;This username is available.')
	{
		$('advice-validate-unique-username').style.color = "green";
		$('username_exists').value = '';
	}
	else
	{
		$('advice-validate-unique-username').style.color = "red";		
		$('username_exists').value = $('username').value;
	}
}

function deleteFile(fileURL)
{
	window.location=fileURL;
}


function refreshpage()
{
	window.location.reload();
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}

function showNewAccount(){
	$('new_account').style.display = '';
	if ($('existing_account')) $('existing_account').style.display = 'none';
	$('user_registration').value = "1";
}

function showExistingAccount(){
	$('existing_account').style.display = '';
	if ($('new_account')) $('new_account').style.display = 'none';
	$('user_registration').value = "2";	
}

function toggleEditForm(element){
	$(element).toggle();
}

function showFileEdit(file_id){
     var url = './edit_file_info.php';
     var params = 'id='+file_id;
     var target = 'edit_'+file_id;
     var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: params});
}

function showEditDescription(file_id){
     var url = './edit_description.php';
     var params = 'id='+file_id;
     var target = 'form_overlay';
     var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: params});
}

function showAddPassword(file_id){
     var url = './add_password.php';
     var params = 'id='+file_id;
     var target = 'form_overlay';
     var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: params});
}
function showDownloadLink(file_url, file_id,filetype,playerURL,filename){
     var url = './show_download_link.php';
     var params = 'url='+file_url+'&id='+file_id+'&filetype='+filetype+'&playerURL='+playerURL+'&fileName='+filename;
     var target = 'edit_'+file_id;
     var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: params});
}


function copyToClipboard()
{
	description.innerText = description.innerText;
	Copied = description.createTextRange();
	Copied.execCommand("Copy");	
}
function RefreshCaptcha()
{
  var captchaImage = document.getElementById('captcha');
  captchaImage.src = 'lib/securimage/securimage_show.php?sid=' + Math.random(); 
  return false
}

function showAddYaboe(file_id){
	 var url = './add_yaboe.php';
	 var params = 'id='+file_id;
	 var target = 'edit_'+file_id;
	 var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: params});
}

function postAddYaboe(){
	var url = './add_yaboe.php';
	var params = 'id='+$F('file_id')+'&yaboe_username='+$F('yaboe_username');
	var target = 'edit_'+$F('file_id');
	
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: params});
}
