// JavaScript Document
function fsToggleDirectory(directorywrapper,start_dir,originaldir,inputname,extensions,omitdirectory,image){

	if($(directorywrapper).css("display")==undefined){
		$(directorywrapper).css("display","none");
	}
	if($(directorywrapper).css("display") == "none"){
		$(directorywrapper).css("display","block");
		if ($(directorywrapper).html() == ""){
			$(directorywrapper).html("<img src='/images/loading.gif' width='32' height='32' />");
			$.post("generalhelper.php",
			{
				ajaxcall:true,
				action:"opendirectory",
				start_dir:start_dir,
				originaldir:originaldir,
				inputname:inputname,
				extensions:extensions,
				omitdirectory:omitdirectory,
				image:image
			},
			function(data) {
				if (data == ""){
					$(directorywrapper).html("<span style='color:#990000'>empty</span>");
				} else {
			   		$(directorywrapper).html(data);
				}
			});				
		}
	} else {
		$(directorywrapper).css("display","none");
	}

}

function toggleDisplayStyle(id){
	if (document.getElementById(id).style.display == ''){
		document.getElementById(id).style.display = 'none';	
	} else {
		document.getElementById(id).style.display = '';	
	}
}

function toggleDropDown(divid,inputid){
	if (document.getElementById(divid).className == 'dropdownVisible'){
		document.getElementById(inputid).className = 'inputDropdown';	
		document.getElementById(divid).className = 'dropdownHidden';	
	} else {
		document.getElementById(inputid).className = 'inputDropup';
		document.getElementById(divid).className = 'dropdownVisible';
	}
}

// this function is used in cpshop_metaproducts.php to close the product select when mouse goes out
function settimeoutcheck(inputcheck, divtostyle, styletoapply){
	document.getElementById(inputcheck).value = 0;
	var t = setTimeout("timedfunction('"+inputcheck+"', '"+divtostyle+"', '"+styletoapply+"')",4000);
}
function timedfunction(inputcheck, divtostyle, styletoapply){
	if(document.getElementById(inputcheck).value == 0){
		document.getElementById(divtostyle).className=styletoapply;
		document.getElementById(inputcheck).value = 1;
	}	
}

function togglepermissionvalues(ptypeselect, valuea, valueb){
	ptype = document.getElementById(ptypeselect).value;
	if (ptype == 'member'){
		document.getElementById(valuea).disabled=true;	
	} else {
		document.getElementById(valuea).disabled=false;	
	}
	if (ptype == 'member' || ptype == 'above' || ptype == 'below'){
		document.getElementById(valueb).disabled=true;
	} else {
		document.getElementById(valueb).disabled=false;	
	}
}

function togglecheckboxes(checkname){
	var toggleval =0;
	for (i = 0; i < checkname.length; i++){
		if (checkname[i].checked){
			toggleval++;
		} else {
			toggleval--;	
		}
	}		
	toggleval = toggleval>=0?false:true;
	for (i = 0; i < checkname.length; i++){
		checkname[i].checked = toggleval;
	}	
}

function formsubmit(formname,action,value,alerttype,confstr){
	if (confstr === undefined){
		document.getElementById('formaction').value = action;
		document.getElementById('formvalue').value = value;
		document.forms[formname].submit();
	} else {
		if (alerttype == 1){
			if (confirm(confstr)){
				document.getElementById('formaction').value = action;
				document.getElementById('formvalue').value = value;
				document.forms[formname].submit();
			}				
		}
		if (alerttype == 2){
			if (prompt(confstr,'') == 'DELETE'){
				document.getElementById('formaction').value = action;
				document.getElementById('formvalue').value = value;
				document.forms[formname].submit();
			}				
		}		
	}

}
function htmlquotes(value){
	value = value.replace(/"/,"&quot;");
	value = value.replace(/'/,"&#39;");
	return value
}

function checkWordCount(obj,wordLimit){
	var len = obj.value.split(/[\s]+/);
	if(len.length > (wordLimit + 1)){
		alert("You have exceeded the "+wordLimit+" word limit");
		obj.oldValue = obj.value!=obj.oldValue?obj.value:obj.oldValue;
		obj.value = obj.oldValue?obj.oldValue:"";
		return false;
	}
	return true;
}

function showEvents(date){
	$("#eventsTooltip"+date).css("display","block");
}

function hideEvents(date){
	$("#eventsTooltip"+date).css("display","none");	
}

function setHeight(element,height,minheight,padding){
	if (Number(height)){
		setheight = height
	} else {
		setheight = $(height).height();
		if (Number(padding)){
			setheight=setheight-padding;
		}
	}
	if (typeof minheight != "undefined" && Number(minheight)){
		if (setheight < minheight){
			setheight = minheight;	
		}
	}
	if (Number(setheight)){
		$(element).height(setheight);
	}
}

//calls downloader script to open the dialog script for specified file
function forceDownload(domain,id,type){
	window.location.href="http://"+domain+"/downloader.php?mi_id="+id+"&umi_type="+type;
}

function loadvideo(type,id){
	if(type=='youtube'){
		var videoHtml = '<object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/'+id+'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+id+'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object>';
		$("#youtubePlayer").css("display","block");
		$("#youtubePlayer").html(videoHtml);
	}
}
