// JavaScript Document
sco_id = 0;
user_id = 0;
function makeURL()
{
	if(document.contentForm.sco_url.value==''){
		var name = document.contentForm.sco_title.value.toLowerCase();
		name = name.replace(/ /g,'_');
		var href = window.location.href.split(':');

        if(eval('href[2]')){
            var url = href[1];
            var dump = url.split('/');
            var dump1 = href[2].split('/');
            dump[3]=dump1[1];

        }
        else{
            var url = href[1];
            var dump = url.split('/');
        }


        document.contentForm.sco_url.value = 'http://' + dump[2] + '/' + dump[3] + '/launch/' + name + '/';
	}
}

function deletePic(pic,index)
{
	if(confirm("Do you really wish to delete this picture?")){
		if(test_xml_http()){
			var url ="admin/admin.php?o=delpic&pic="+pic;
			httpXmlRequest(url);
		}
	}
}

function delPicSuccess(xml)
{
	var elements = xml.getElementsByTagName('index');
	var index = elements[0].firstChild.nodeValue;
	document.getElementById('divpic_'+index).innerHTML="<input name='pic"+index+"' type='file'>";
}

var newwindow;
function popit(url) {
	newwindow=window.open(url,'name','height=400,width=400,top=100,resizable=no,scrollbars=no,toolbar=no,status=no');
	if (window.focus) {
		newwindow.focus()
	}
}

function bpre(sco)
{
	window.location ="content.php?o=addpur&sco="+sco;
}

function checkFields()
{
	for(var i = 0; i < document.contentForm.elements.length; i++)
	{
		if(document.contentForm.elements[i].value == "" && document.contentForm.elements[i].type != "select-one" && document.contentForm.elements[i].type != "hidden" && document.contentForm.elements[i].type != "button" && document.contentForm.elements[i].type != "file" && document.contentForm.elements[i].name != "sco_demo_url" && document.contentForm.elements[i].name != "sco_formal_cepoints"  && document.contentForm.elements[i].name != "sco_informal_cepoints"  && document.contentForm.elements[i].name != "sco_nsw_formal_points"  && document.contentForm.elements[i].name != "sco_nsw_informal_points")
		{
			alert("Please enter " + document.contentForm.elements[i].name.toUpperCase());
			document.contentForm.elements[i].focus();
			return false;
		}
	}

	if(!eval('document.contentForm.sco_outline'))
	{
		if(document.contentForm.summary.value.length > 750)
		{
			alert('Please only enter 750 characters in Summary field');
			document.contentForm.summary.focus();
			return false;
		}
	}
	if(eval('document.contentForm.sco_id.selectedIndex')!=undefined)
	{
		if(document.contentForm.sco_id.options[document.contentForm.sco_id.selectedIndex].value=='')
		{
			alert('Please select a SCO');
			return false;
		}
	}
	var demo_selected;
	if(eval('document.contentForm.demo'))
		demo_selected = get_radio_select_value(document.contentForm.demo);
	else{
		if(document.getElementById('sco_cats[]').options[7].selected && (document.contentForm.sco_formal_cepoints.value=='' || document.contentForm.sco_formal_cepoints.value==0) && (document.contentForm.sco_informal_cepoints.value=='' || document.contentForm.sco_informal_cepoints.value==0))
			demo_selected = 1;
		else
			demo_selected = 0;
	}

	if(demo_selected==1 && document.contentForm.sco_demo_url.value==''){
		alert("You have indicated that this is a free course, please provide URL.\nPlease also ensure the course is set as Public in the Breeze system!");
		document.contentForm.sco_demo_url.focus();
		return false;
	}

	var access_selected = false;
	for(var i=0;i<2;i++)
		if(document.contentForm.need_login[i].checked)
			access_selected = true;

	if(!access_selected)
	{
		alert('Please assign Access Restrictions');
		return false;
	}
	return confirm('Do you really wish to proceed?');
}

function showHideDiv(elem,val)
{
	if(val==0)
		document.getElementById(elem).style.display='';
	else
		document.getElementById(elem).style.display='none';

}

function showHideAccess(val)
{
	if(val==0){
		document.getElementById('div_demo').style.display='';
		document.getElementById('div_demo_1').style.display='none';
	}
	else{
		document.getElementById('div_demo').style.display='none';
		document.getElementById('div_demo_1').style.display='';
	}

}

function catSelected()
{
	if(document.getElementById('sco_cats[]').options[7].selected && (document.contentForm.sco_formal_cepoints.value=='' || document.contentForm.sco_formal_cepoints.value==0) && (document.contentForm.sco_informal_cepoints.value=='' || document.contentForm.sco_informal_cepoints.value==0)){
		document.getElementById('div_demo').style.display='';
		document.getElementById('div_demo_1').style.display='none';
	}
	else{
		document.getElementById('div_demo').style.display='none';
		document.getElementById('div_demo_1').style.display='';
		document.contentForm.sco_demo_url.value='';
	}
}

function needLoginSelect(val,total)
{
	for(var i=0;i<total;i++)
	{
		if(val==0)
		{
			document.getElementById('access_'+i+'_1').disabled=true;
			document.getElementById('access_'+i+'_0').disabled=true;
			document.getElementById('access_'+i+'_1').checked=true;
			document.getElementById('div_price_'+i).style.display='';
		}
		else
		{
			document.getElementById('access_'+i+'_1').disabled=false;
			document.getElementById('access_'+i+'_0').disabled=false;
			document.getElementById('access_'+i+'_0').checked=true;
			document.getElementById('div_price_'+i).style.display='none';
		}
	}
}

function get(id)
{
	var w = 900;
	var h = 550;
	var l = (screen.availWidth-w)/2;
	var t = (screen.availHeight-h)/2;

	window.open ("content.php?id="+id, "get","width="+w+",height="+h+",left="+l+",top="+t+",scrollbars=yes,resizable=yes");
}

function edit(id)
{
	window.location = "admin/admin.php?o=edit&id="+id;
}


function checkCourseReturn(xml)
{
	var elements = xml.getElementsByTagName('response-code');
	var response = elements[0].firstChild.nodeValue;

	elements = xml.getElementsByTagName('sco');
	var sco = elements[0].firstChild.nodeValue;

	if(response=='notcompleted')
		setTimeout('ajaxCheckCourseLaunch('+sco+')', 2000);
	else if(response=='survey')
		window.location = 'content.php?o=displaysurvey&sco='+sco;
	else if(response=='refresh')
		window.location.reload();
	else return;

}


function ajaxCheckCourseLaunch(sco)
{
	if(test_xml_http()){
		var url ="content.php?o=checkcoursecompletion&sco="+sco;
		//window.location = url;
		httpXmlRequest(url);
	}
}

function showContentWithNav(url, sco,pres, session, cw, ch, noScale, scrollbar, toolbar, status, location)
{
		if (session != "")
			url = url + (url.indexOf('?') == -1 ? "?" : "&amp;") + "session=" + session;
		var w, h, l, t;
		if (pres) {
		  w = 0.96 * screen.width;
		  h = 0.76 * screen.height;
		  l = (screen.width - w) / 2;
		  t = (screen.height - h) / 2;
		} else if (noScale) {
			w = cw;
			h = ch;
			l = (screen.width - w) / 2;
			t = (screen.height - h) / 2;
		} else {
		  if (cw > 0 && ch > 0) {
			if (cw <= screen.width) {
				  w = cw;
				  h = w * ch /cw;
				}
				if (h > screen.height) {
				  h = screen.height;
				  w = h * cw / ch;
				}
		  } else {
			w = 0.4 * screen.width;
			h = 0.3 * screen.height;
		  }

			  l = (screen.width - w) / 2;
			  t = (screen.height - h) / 2;
			}
		var options = "toolbar=" + toolbar + ",status=" + status + ",location=" + location + ",resizable=1,left=" + l + ",top=" + t + ",width=" + w + ",height=" + h;
		if (scrollbar)
			options += ",scrollbars=yes";
		var win = open(url, "", options);

        if(sco!='')
		  setTimeout('ajaxCheckCourseLaunch('+sco+')', 5000);
}

/**
 * Default showContent window, which displays popup w/ no navigation.
 */
function showContent(url, pres, session, cw, ch, noScale, scrollbar) {
	var toolbar = 0;
	var status = 0;
	var location = 0;
	showContentWithNav(url, pres, session, cw, ch, noScale, scrollbar, toolbar, status, location);
}

function viewReceipt(url)
{
	var w, h, l, t;
	w = 800;
	h = 600;
	l = (screen.width - w) / 2;
	t = (screen.height - h) / 2;

	var options = "toolbar=0,status=0,location=0,resizable=1,left="+l+",top="+t+",width="+w+",height="+h+",scrollbars=yes";

	var win = open(url, "", options);
}
function get_radio_select_value(obj)
{
	var return_val = '';

	var count = obj.length;
	for (i = 0; i < count; i++)
		if (obj[i].checked)
			return_val = obj[i].value;

	return return_val;
}

function checkNumber(field)
{
	if(field.value!='')
	{
		if (isNaN(field.value))
		{
			alert('This field requires a Number');
			field.value='';
			field.focus();
		}
	}
}

