//
//
//	comptonCommunicate.js
//
//	jscript for the vimas recorder
//	
//	2008-09-07	- commented out the STOP_RP() procedure call - i think we have a timing issue

	var optionNo = 0		// test
	var UserServerFolder = "miha"

	var blnRecorded = false;		// this variable gets set ONLY when the record button is hit
	//	var blnUploaded = 1;
	
	function vision()	
	{
		//STOP_RP();	- commented out 2008-09-07
		document.getElementById("loading").style.visibility="hidden";
		document.getElementById("loaded").style.visibility="visible";
	}
	
	function STOP_RP()	{
		document.AudioApplet.jsStop();
	}

	function fileLoaded()
	{
		alert("Your voice file has been sent to us and will be reviewed very shortly!");
		blnRecorded=false;	
		document.Gui_RP.Timer.value = '100%';

		var myUrl = '';
		
		myUrl	+=	'/cgi-bin/Communicate/comptonCommunicate.pl'	// window.location (passes all parms)
			+	'?'
			+	'Param_teacher_name'
			+	'='
			+	document.Gui_RP.Param_teacher_name.value
			;

		myUrl	+=	'&'
			+	'Param_client_name'
			+	'='
			+	document.Gui_RP.Param_client_name.value
			;

		myUrl	+=	'&'
			+	'Param_crypted'
			+	'='
			+	document.Gui_RP.Param_crypted.value
			;

		myUrl	+=	'&'
			+	'Submit_button'
			+	'='
			+	escape(document.Gui_RP.Submit_button.value)
			;	

		// alert(myUrl);

		window.location=	myUrl	;
		
	}

	function report_RP(s,num)
	{
		alert(s);
	}

	function setRecordLevel(num){}

	function setStatus(str,num)	
	{
		document.Gui_RP.Status.value = str;
		if(num==11) fileLoaded();
	}

	function setTime(s)	{
		document.Gui_RP.Timer.value = s;
	}

	function confirmRecord(s, num)	{
		if(confirm(s))
			RECORD_RP();
		else
			STOP_RP();
	}

	function addName(s)	{
		// var newOpt  = new Option(String(s),String(s));
		// var VILength = document.Gui_RP.VoiceItems.length;
		// document.Gui_RP.VoiceItems.options[VILength] = newOpt;
		//document.Gui_RP.FileName.value = "*.mp3";
	}

	function RECORD_RP()	{
			// 2010-12-27 - added the "confirm" to make sure we really do indeed want to "clobber" this recording
		if  (	    blnRecorded == true
			&&  (!( confirm('You have not yet uploaded this recording\NAre you sure you want to create a new one?') ))
		    )
		{
			return;
		}
		blnRecorded = true ;		// setting the record switch to ON
		document.AudioApplet.jsRecord();
	}

	function setparameter()	{
		//document.RPApplet.Setparameter(String("bla"),String("BLABLA"));

		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("Param_teacher_name")
				,	replaceChars(String(document.Gui_RP.Param_teacher_name.value))
				);


		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("Param_client_name")
				,	replaceChars(String(document.Gui_RP.Param_client_name.value))
				);

		// alert ( document.Gui_RP.Param_client_name.value );
		// alert ( document.Gui_RP.Param_talker_name.value );


		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("Param_talker_name")
				,	replaceChars(String(document.Gui_RP.Param_talker_name.value))
				);

		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("studentComments")
				,	replaceChars(String(document.Gui_RP.studentComments.value))
				);

		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("teacherComments")
				,	replaceChars(String(document.Gui_RP.teacherComments.value))
				);

		/**
		document.RPApplet.Setparameter(String("javaVersion"), String("1.2.3"));
		document.RPApplet.Setparameter(String("javaVendor"), String("sun health"));
		*/
		//document.RPApplet.Setparameter(String("javaVersion"),	document.detectjvmapplet.getJavaVersion()	);
		document.AudioApplet.Setparameter(String("javaVersion"),	document.detectjvmapplet.getJavaVersion()	);
		//document.RPApplet.Setparameter(String("javaVendor"),	document.detectjvmapplet.getJavaVendor()	);
		document.AudioApplet.Setparameter(String("javaVendor"),	document.detectjvmapplet.getJavaVendor()	);


		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("appName")
				,	String(navigator.appName)
				)
				;
		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("appVersion")
				,	String(navigator.appVersion)
				)
				;
		//document.RPApplet.Setparameter
		document.AudioApplet.Setparameter
				(	String("userAgent")
				,	String(navigator.userAgent)
				)
				;
	}

	function PLAYBACK_RP()	{
		document.AudioApplet.jsPlayback();
	}

	function PAUSE_RP()	{
		document.AudioApplet.jsPause();
	}

		/* document.RPApplet.UPLOAD(String(document.Gui_RP.FileName.value));  */
	function UPLOAD_RP()	{

	    if  ( blnRecorded == true ) {

		setparameter();

		var date = new Date();
		var month = formatDate(date.getMonth()+1);
		var day = formatDate(date.getDate());

		var upload_string = ''
			//+	'DummyNameToSatisfyVimasRequirements.mp3'
			+	replaceChars(String(document.Gui_RP.Param_client_name.value))
			+	'_'
			+	String(month)
			+	"_"
			+	String(day)
			+	'.MP3'
			;

		//document.RPApplet.UPLOAD(upload_string);
		document.AudioApplet.jsUpload(upload_string);
	    } else {
		alert ('Sorry, but you must first record something to send us!');
	    };
	}

function formatDate(num){
	return (num<10)?("0"+num) : new String(num) ;
}

function replaceChars(entry) {
	var out = "\n"; // with this	// perl needs double slash
	var add = "\t"; // replace this
	var temp = "" + entry; // temporary holder

	while (temp.indexOf(out) >-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}


	function WEBPLAY_RP()	{
	//document.RPApplet.WEBPLAY(UserServerFolder + "/" + document.Gui_RP.VoiceItems.value);
	document.AudioApplet.WEBPLAY_RP(UserServerFolder + "/" + document.Gui_RP.VoiceItems.value);
	}
	

function	checkForRecording()	{	/* 2008-03-25 */
    if  ( blnRecorded == true ) {
	alert ("Sorry, but you MUST upload your audio recording before you can upload a homework assignment\nOtherwise you loose your recording!");
	return false;
    } else {
	return true;
    }
}

function	setUrlForFlashPlayer()
{
	// var selectedRadioGroup=0;
	var selectedDate = 0;
	
	// alert( 'length =' + document.Gui_P.radioVoiceGroup.length );
	// alert( 'value =' + document.Gui_P.radioVoiceGroup.value );

	if  ( document.Gui_P.radioVoiceGroup.length )	{
		for (var i=0 ; i < document.Gui_P.radioVoiceGroup.length; i++ )	{
			if (document.Gui_P.radioVoiceGroup[i].checked )	{
				//						selectedRadioGroup = i;
				selectedDate = document.Gui_P.radioVoiceGroup[i].value;
				break;
			};
		};
	} else {
			// strangely, if there is only ONE element, it comes thru as undefined rather than a zero!
		selectedDate	= document.Gui_P.radioVoiceGroup.value ;
	};


	var myUrl = 'http://comptonpeslonline.com/cgi-bin/Communicate/deliverVoice.pl'
		+	'?'
		+	'Param_teacher_name='
		+	document.Gui_P.Param_teacher_name.value
		+	'&'
		+	'Param_client_name='
		+	document.Gui_P.Param_client_name.value
		+	'&'
		+	'Param_upload_date='
		//					+	document.Gui_P.radioVoiceGroup[selectedRadioGroup].value
		+	selectedDate
		;

	document.mp3player.SetVariable
		(	'url'
		,	myUrl
		);
}
;



