// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function display_upload(id, path, name, check){
	var ary = path.split('/');
	var path = '/'+ary[ary.length-4]+'/'+ary[ary.length-3]+'/'+ary[ary.length-2]+'/'+ary[ary.length-1];
	var display_path = path;
	if ( ary[ary.length-1].match(/.pdf$/) ) {
		$('imgviewer').src = "/images/external_pdf.png?"+path;
	}
	else if ( ary[ary.length-1].match(/.xl(s|sx|sm|tx|tm|sb|am)$/) ) {
		$('imgviewer').src = "/images/external_xls.png?"+path;
	}
	else if ( ary[ary.length-1].match(/.do(c|cx|cm|tx|tm)$/) ) {
		$('imgviewer').src = "/images/external_doc.png?"+path;
	}
	else if ( ary[ary.length-1].match(/.pp(t|tx|tm|tx|tm|am|sx|sm)$/) ) {
		$('imgviewer').src = "/images/external_ppt.png?"+path;
	}	
	else {
		var oImg = new Image();
		oImg.src = path;
		$('imgviewer').src = path;
	}
	if ($('image_form').value == "Modify") {
		$('upload_name').value = name;
		$('display_path').value = display_path;
		$('upload_active').checked = check;
	}
	$('upload_id').value = id;
	$('hidden_name').value = name;
	$('hidden_path').value = display_path;
	$('hidden_active').value = check;
}

function toggle_edit() {
	if ($('image_form').value == "Modify") {
		$('upload_file_temp').value = '';
		$('upload_file').value = null;
		$('upload_name').value = $('hidden_name').value;
		$('display_path').value = $('hidden_path').value;
		$('upload_active').checked = $('hidden_active').value;
		$('new_id').style.display = 'none';
		$('old_id').style.display = 'block';
	}
	else if ($('image_form').value == "New") {
		$('hidden_name').value = $('upload_name').value;
		$('upload_name').value = null;
		$('hidden_path').value = $('display_path').value;
		$('display_path').value = null;
		$('hidden_active').checked = $('upload_active').value;
		$('upload_active').checked = false;
		$('new_id').style.display = 'block';
		$('old_id').style.display = 'none';
	}
}

function launch_gallery(){
	myWindow = window.open("", "tinyWindow", 'toolbar=0,resizable=1,scrollbars=0,width=780,height=550')
	myWindow.document.location ='/uploads.popup'
	myWindow.focus();
}

function open_external(){
	if ( $('imgviewer').src.match(/\/external_(pdf|xls|doc|ppt).png?/) ) {
		var pdf = $('imgviewer').src.split('?');
		window.open(pdf[pdf.length-1]);
	}
}

function launch_chat(question){
	myWindow = window.open("", "tinyWindow", 'toolbar=false,width=640,height=480')
	try {
		myWindow.document.location ='/chat/chat_window?question='+question	
	}catch(e){
		alert("Please Close your existing chat window")
	}
	
	myWindow.focus();
}

function check_for_trigger(val){
	if(val =='range'){
		new Effect.Appear('daterange')
	}else{
		$('daterange').style.display = 'none'
	}	
}
