// baixa.la Scriptz

function toggle(id, newValue) {
	element=document.getElementById(id);
	element.style.display=newValue;
}

function showProgress() {
	toggle('submit_button', 'none');
	toggle('progress_bar', 'block');
}

function showGalleryProgress() {
	toggle('gallery_submit_button', 'none');
	toggle('gallery_progress_bar', 'block');
}


function activateEmail(default_emails) {
	emailbox=document.getElementById('email_to');
	if (emailbox.value==default_emails) {
		emailbox.value='';
		emailbox.style.color='#000';
	}

}

function checkTerms() {
	terms_element = document.getElementById('terms');
	if (terms_element.checked==true) {
		return true;
	} else {
		alert ('Please agree to the Terms of Use before uploading.');
		return false;
	}
}

function submitUpload() {

	if (checkTerms()) {
		showProgress();
		document.upload_form.submit();

		if (document.images) document.progress_bar_img.src = document.progress_bar_img.src;

		return false;

	} else {
		return false;
	}

}

function submitGalleryUpload() {
		showGalleryProgress();
		document.gallery_upload_form.submit();

		if (document.images) document.gallery_progress_bar_img.src = document.gallery_progress_bar_img.src;

		return false;
}

function show_multi() {
	toggle('multi_link','none');
	toggle('multi_files','block');

	if (document.getElementById('privacy_this_file')) {
		document.getElementById('privacy_this_file').innerHTML='these files';
	}

}

function hide_multi() {
	toggle('multi_link','block');
	toggle('multi_files','none');

	if (document.getElementById('privacy_this_file')) {
		document.getElementById('privacy_this_file').innerHTML='this file';
	}

}

// image auto-resizer for DL page

function get_window_size_new() {
	var myWidth = 0, myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
 	}
	// take 80% and return
	return Math.floor(eval(myWidth*0.8));
}

function resize_onload() {

	var source_width=get_image_width(); // printed earlier in page by PHP
	var window_width=get_window_size_new();
	var image_link = document.getElementById('image_link');

	if (source_width<window_width) {
		//
	} else {
		image_link.setAttribute('href','javascript:resize_image();');
		document.getElementById('image_img').width=window_width;
		image_link.style.cursor="url('/images/icons/zoomin.cur'), pointer";
	}

}

function resize_image() {

	var image_img = document.getElementById('image_img');
	var image_link = document.getElementById('image_link');
	var source_width=get_image_width();
	var window_width=get_window_size_new();

	if (image_img.width==source_width) {
		image_img.width=window_width; // shrink it
		image_link.style.cursor="url('/images/icons/zoomin.cur'), pointer";
	} else {
		image_img.width=source_width; // enlarge it
		image_link.style.cursor="url('/images/icons/zoomout.cur'), pointer";
	}

}

// Select Linking Options Boxes
function selectText(id) {
	element=document.getElementById(id);
	element.focus();
	element.select();
}

// Turn on File Description box once the Browse input is chosen
function turnOnDesc(browse_id, desc_id) {
	browse = document.getElementById(browse_id);
	desc = document.getElementById(desc_id);

	if (browse.value!='') {
		toggle(desc_id,'block');
	} else {
		toggle(desc_id,'none');
	}

}

function activateField(default_text, input_id) {
	inputbox=document.getElementById(input_id);
	if (inputbox.value==default_text) {
		inputbox.value='';
		inputbox.style.color='#000';
	}

}

function submitFlag() {
	reason = document.getElementById('flagged_reason').selectedIndex;
	other_value = document.getElementById('flagged_other_input').value;

	if (reason==0) {
		alert('Please choose your reason for flagging this file.');
		return false;
	} else {
		if (reason==3 && other_value=='') {
			alert('Please enter your reason in the "Other" field.');
			return false;
		} else {
			if (confirm('Are you sure you want to flag this file for review by the DivShare staff?')) {
				return true;
			} else {
				return false;
			}
		}
	}
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function hideDLMsg() {
	if (document.getElementById('dlmsg')) {
		toggle('dlmsg','none');
		setCookie('hide_dl_msg','1',7);
	}
}

function set_item_description(desc) {
	document.getElementById('item_description').innerHTML=desc;
}

function email_file(file_id, secret_code) {

    // Validar os emails.
	if (document.getElementById('email_to_input_' + file_id).value == "" ) {
		alert('Por favor, informe seu e-Mail.');
		return;
    }
    else if (document.getElementById('email_to_input_' + file_id).value.match(/(\w+)@(.+)\.(\w+)$/) == null){
        alert('Endereço de e-Mail inválido.');
		return;
    }


    emails = document.getElementById('email_to_input_' + file_id).value;

	new Ajax.Updater('','/scripts/ajax/send_sharing_email.php',{ 'method':'post' , 'parameters': 'f=' + file_id + '&s=' + secret_code + '&emails=' + emails, 'onSuccess':function(request){ email_success(request, file_id); }, 'onFailure':function(request, file_id){ alert(request.responseText); } } );
}

function email_success(request, file_id) {
	response = request.responseText;
	toggle('email_to_form_' + file_id,'none');
	toggle('email_to_response_' + file_id,'block');
	document.getElementById('email_to_response_text_' + file_id).innerHTML = response;
}

function refresh_email(file_id) {
	document.getElementById('email_to_input_' + file_id).value='';
	toggle('email_to_form_' + file_id,'block');
	toggle('email_to_response_' + file_id,'none');
	document.getElementById('email_to_input_' + file_id).focus();
}

