// DivShare Scriptssssss

function submitForm(name) {
    document[name].submit();
    return false;
}

function isKeyPressEnterSubmitForm(formName, e) {
    
    var ENTER_KEY = 13;
    var code = "";
    
    if (window.event) {
        code = e.keyCode;
    }
    else if (e.which) {
        code = e.which;
    }
    if (code == ENTER_KEY) {
        submitForm(formName);      
    }     
}

function toggle(id, newValue) {
	if (document.getElementById(id)) {
		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';
	}
	
	if (document.getElementById('profile_this_file')) {
		document.getElementById('profile_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';
	}
	
	if (document.getElementById('profile_this_file')) {
		document.getElementById('profile_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 updateInfo(){ return "updated"; }

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 getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

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) {
	
	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_folder(gallery_id, secret_code) {
    
    emails = document.getElementById('email_to_input_' + gallery_id).value;
    
    new Ajax.Updater('','/scripts/ajax/send_sharing_email.php',{ 'method':'post' , 'parameters': 'g=' + gallery_id + '&s=' + secret_code + '&emails=' + emails, 'onSuccess':function(request){ email_success(request, gallery_id); }, 'onFailure':function(request, gallery_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();
}

function select_checkboxes_basic(FormName, FieldName, CheckValue) {
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes) {
		objCheckBoxes.checked = CheckValue;
	} else {
		for(var i = 0; i < countCheckBoxes; i++) {
			objCheckBoxes[i].checked = CheckValue;
		}
	}
}

function trimAll(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') 	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function leftTrim(sString) {
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	return sString;
}

function audio_popup() {
	window.open('http://www.divshare.com/scripts/captcha/audio_page.php', 'audio_captcha', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=250');	
}
function setPrivateDaemon(id, type, elementShow, elementHide) {
	$j.getJSON("/scripts/ajax/set_private.php", { id: id, type: type},
		  function(data){
			  if (data.err != '') {
				alert(data.err);
			  }
			  else {			  
				$j(elementHide).hide();  
				$j(elementShow).show();  
			  }
	});      
}   

