dojo.require('cyim.ajax');
dojo.require("dijit.Dialog");
dojo.require("dojox.layout.ContentPane");
	
function ValidForm(){
	var objForm = document.frmTestimony ;
	if ((intErr = Valid(objForm.Nom.value,'<%=k_strLng%>')) != k_intOk){alert('Name: '+ AffErr('<%=k_strLng%>',intErr));return false;}
	if ((intErr = Valid(objForm.Prenom.value,'<%=k_strLng%>')) != k_intOk){alert('Firstname: '+ AffErr('<%=k_strLng%>',intErr));return false;}
	if ((intErr = Valid(objForm.CodePays.value, "<%=k_strLng%>")) != k_intOk ) { alert("Country: " + AffErr("<%=k_strLng%>", intErr)); return false; }
	if ((intErr = Valid(objForm.Email.value,'<%=k_strLng%>')) != k_intOk){alert('Email: '+ AffErr('<%=k_strLng%>',intErr));return false;}	
	if ((intErr = ValidEmail(objForm.Email.value, "<%=k_strLng%>")) != k_intOk ) { alert("Email: " + AffErr("<%=k_strLng%>", intErr)); return false;}
	if ((intErr = Valid(objForm.Organisme.value,'<%=k_strLng%>')) != k_intOk){alert('Hospital: '+ AffErr('<%=k_strLng%>',intErr));return false;}	
	if ((intErr = Valid(objForm.Service.value,'<%=k_strLng%>')) != k_intOk){alert('Unit: '+ AffErr('<%=k_strLng%>',intErr));return false;}	
	if ((intErr = Valid(objForm.strStreet.value,'<%=k_strLng%>')) != k_intOk){alert('Street address: '+ AffErr('<%=k_strLng%>',intErr));return false;}	
	if ((intErr = Valid(objForm.strZipCode.value,'<%=k_strLng%>')) != k_intOk){alert('Zip Code: '+ AffErr('<%=k_strLng%>',intErr));return false;}
	if ((intErr = Valid(objForm.strCity.value,'<%=k_strLng%>')) != k_intOk){alert('City: '+ AffErr('<%=k_strLng%>',intErr));return false;}	
	
	return true;
}

//closePopup() : Fermeture de la popup
function closePopup(BoxId, Guid) {
	var popup = dijit.byId(BoxId);
	popup.destroyRecursive();
	
	// génération d'un nouveau guid
	if (document.getElementById("GuidTestimony")) {
		document.getElementById("GuidTestimony").value=Guid;
	}
}

function newTestimony (a_strNom, a_strPrenom, a_strCodePays, a_strEmail, a_strOrganisme, a_strService,  a_strCity, a_strStreet, a_strZipCode, a_strState){

	url = 'strNom=' + a_strNom + '&strPrenom=' + a_strPrenom + '&strCodePays=' + a_strCodePays + '&strEmail=' + a_strEmail + '&strOrganisme=' + a_strOrganisme + '&strService=' + a_strService + '&strCity=' + a_strCity + '&strZipCode=' + a_strZipCode + '&strStreet=' + a_strStreet + '&strState=' + a_strState;			
	document.location.href = '/moduleTestimony/public/index.asp?page=form&'+ url  + '&X=' + Math.random()
}
function GoToAction(Action){
	var url = "";
	switch(Action)
	{
	case "form":
		document.location.href = '/moduleTestimony/public/index.asp?page=form&X=' + Math.random();
	  	break;
	case "question":
		url = 'strGuidTestimony=' +encodeURIComponent(document.frmTestimony.GuidTestimony.value) + '&strNom=' + encodeURIComponent(document.frmTestimony.Nom.value) + '&strPrenom=' + encodeURIComponent(document.frmTestimony.Prenom.value) + '&strCodePays=' + encodeURIComponent(document.frmTestimony.CodePays.value) + '&strEmail=' + encodeURIComponent(document.frmTestimony.Email.value) + '&strOrganisme=' + escape(encodeURIComponent(document.frmTestimony.Organisme.value)) + '&strService=' + escape(encodeURIComponent(document.frmTestimony.Service.value)) + '&strStreet=' + escape(encodeURIComponent(document.frmTestimony.strStreet.value))  + '&strCity=' + escape(encodeURIComponent(document.frmTestimony.strCity.value))+ '&strZipCode=' + encodeURIComponent(document.frmTestimony.strZipCode.value) + '&strState=' + escape(encodeURIComponent(document.frmTestimony.strState.value));			
		document.location.href = '/moduleTestimony/public/index.asp?page=question&' + url  + '&X=' + Math.random();
		break;	  
	case "byupload":
		url = 'strGuidTestimony=' + encodeURIComponent(document.frmQuestion.strGuidTestimony.value) + '&strNom=' + encodeURIComponent(document.frmQuestion.strNom.value) + '&strPrenom=' + encodeURIComponent(document.frmQuestion.strPrenom.value) + '&strCodePays=' + encodeURIComponent(document.frmQuestion.strCodePays.value) + '&strEmail=' + encodeURIComponent(document.frmQuestion.strEmail.value) + '&strOrganisme=' + escape(encodeURIComponent(document.frmQuestion.strOrganisme.value)) + '&strService=' + escape(encodeURIComponent(document.frmQuestion.strService.value))  + '&strStreet=' + escape(encodeURIComponent(document.frmQuestion.strStreet.value))  + '&strCity=' + escape(encodeURIComponent(document.frmQuestion.strCity.value))+ '&strZipCode=' + encodeURIComponent(document.frmQuestion.strZipCode.value) + '&strState=' + escape(encodeURIComponent(document.frmQuestion.strState.value));	
		url = url + '&strLanguage=' + encodeURIComponent(document.frmQuestion.strLanguage.value) + '&booAnonymous=' + getCheckedValue(document.frmQuestion.booAnonymous) + '&strICUOutcome=' + encodeURIComponent(document.frmQuestion.strICUOutcome.value) + '&booRealPatient=' + getCheckedValue(document.frmQuestion.booRealPatient) + '&strSendBy=' + encodeURIComponent(document.frmQuestion.strSendBy.value) + '&booPatientAgree=' + getCheckedValue(document.frmQuestion.booPatientAgree) + '&booPatientRequest=' + getCheckedValue(document.frmQuestion.booPatientRequest);
		document.location.href = '/moduleTestimony/public/index.asp?page=byUpload&' + url;
	  	break;
	case "byemail":
		url = 'strGuidTestimony=' + encodeURIComponent(document.frmQuestion.strGuidTestimony.value) + '&strNom=' + encodeURIComponent(document.frmQuestion.strNom.value) + '&strPrenom=' + encodeURIComponent(document.frmQuestion.strPrenom.value) + '&strCodePays=' + encodeURIComponent(document.frmQuestion.strCodePays.value) + '&strEmail=' + encodeURIComponent(document.frmQuestion.strEmail.value) + '&strOrganisme=' + escape(encodeURIComponent(document.frmQuestion.strOrganisme.value)) + '&strService=' + escape(encodeURIComponent(document.frmQuestion.strService.value)) + '&strStreet=' + escape(encodeURIComponent(document.frmQuestion.strStreet.value))  + '&strCity=' + escape(encodeURIComponent(document.frmQuestion.strCity.value))+ '&strZipCode=' + encodeURIComponent(document.frmQuestion.strZipCode.value) + '&strState=' + escape(encodeURIComponent(document.frmQuestion.strState.value));	
		url = url + '&strLanguage=' + encodeURIComponent(document.frmQuestion.strLanguage.value) + '&booAnonymous=' + getCheckedValue(document.frmQuestion.booAnonymous) + '&strICUOutcome=' + encodeURIComponent(document.frmQuestion.strICUOutcome.value) + '&booRealPatient=' + getCheckedValue(document.frmQuestion.booRealPatient) + '&strSendBy=' + encodeURIComponent(document.frmQuestion.strSendBy.value) + '&booPatientAgree=' + getCheckedValue(document.frmQuestion.booPatientAgree) + '&booPatientRequest=' + getCheckedValue(document.frmQuestion.booPatientRequest);
		document.location.href = '/moduleTestimony/public/index.asp?page=byEmail&' + url;
	  	break;
	default:
		// bypost
		url = 'strGuidTestimony=' + encodeURIComponent(document.frmQuestion.strGuidTestimony.value) + '&strNom=' + escape(encodeURIComponent(document.frmQuestion.strNom.value)) + '&strPrenom=' + escape(encodeURIComponent(document.frmQuestion.strPrenom.value)) + '&strCodePays=' + encodeURIComponent(document.frmQuestion.strCodePays.value) + '&strEmail=' + encodeURIComponent(document.frmQuestion.strEmail.value) + '&strOrganisme=' + escape(encodeURIComponent(document.frmQuestion.strOrganisme.value)) + '&strService=' + escape(encodeURIComponent(document.frmQuestion.strService.value)) + '&strStreet=' + escape(encodeURIComponent(document.frmQuestion.strStreet.value))  + '&strCity=' + escape(encodeURIComponent(document.frmQuestion.strCity.value))+ '&strZipCode=' + encodeURIComponent(document.frmQuestion.strZipCode.value) + '&strState=' + escape(encodeURIComponent(document.frmQuestion.strState.value));
		url = url + '&strLanguage=' + encodeURIComponent(document.frmQuestion.strLanguage.value) + '&booAnonymous=' + getCheckedValue(document.frmQuestion.booAnonymous) + '&strICUOutcome=' + encodeURIComponent(document.frmQuestion.strICUOutcome.value) + '&booRealPatient=' + getCheckedValue(document.frmQuestion.booRealPatient) + '&strSendBy=' + encodeURIComponent(document.frmQuestion.strSendBy.value) + '&booPatientAgree=' + getCheckedValue(document.frmQuestion.booPatientAgree) + '&booPatientRequest=' + getCheckedValue(document.frmQuestion.booPatientRequest );
		document.location.href = '/moduleTestimony/public/index.asp?page=byPost&' + url;	
	}
}


// Import d'un fichier
function ImportFile()
{
	var objForm = document.frmByUpload
	var intErr = 0 ;
	var filename = objForm.strFile.value;
	
	if ((intErr = Valid(filename, "en")) != k_intOk ) {  
		alert("Please import a file.");
		return; 
	}

	var hash = {'.doc' : 1,'.docx' : 1,'.rtf' : 1,'.rtf' : 1,'.pdf' : 1,'.jpg' : 1,'.gif' : 1,'.png' : 1,'.tiff' : 1,'.tif' : 1};
	
	var re = /\..+$/;
	var ext = filename.match(re);
	if (!hash[ext]) {
		alert("The extension file is not supported !!!");
		return;
	}

	
	theFeats = "height=120,width=500,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no";
	theUniqueID = (new Date()).getTime() % 1000000000;
	var myWin
	if ((intErr = Valid(objForm.strFile.value, "Fr")) == k_intOk )
		myWin = window.open("/ModuleTestimony/Public/Views/progressbar.asp?ID=" + theUniqueID, theUniqueID, theFeats);
	
	objForm.action = "/ModuleTestimony/Public/Actions/byUpload.asp?ID=" + theUniqueID;
	objForm.encoding = "multipart/form-data";		
	objForm.target = theUniqueID;
	try {
		setTimeout("document.frmByUpload.submit()", 2000);
	}
	catch(err) {
		if (myWin)
			myWin.close();
		alert("An error occurs during validation. Please verify your datas." + err );
	}
	
	
}

// Suppression d'un fichier
function DeleteFile(intIdTestimonyFile)
{
	if (confirm("Do you really want to delete this file ?")) {

		var objForm = document.frmByUpload
					
		theFeats = "height=120,width=500,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no";
		theUniqueID = (new Date()).getTime() % 1000000000;
		var myWin
		myWin = window.open("/ModuleTestimony/Public/Views/progressbar.asp?ID=" + theUniqueID, theUniqueID, theFeats);
		
		objForm.action = "/ModuleTestimony/Public/Actions/byUpload.asp?strAction=Supprimer&intIdTestimonyFile="+intIdTestimonyFile+"&ID=" + theUniqueID;
		objForm.encoding = "application/x-www-form-urlencoded";
		objForm.target = theUniqueID;
		try {
			setTimeout("document.frmByUpload.submit()", 2000);
		}
		catch(err) {
			if (myWin)
				myWin.close();
			alert("An error occurs during validation. Please verify your datas." + err );
		}		
	}			
}

function DisplayErrorSubmission(a_booShoworHide){
	if (a_booShoworHide) {
		document.getElementById("ErrorMessage").style.display = "block";
	} else {
		document.getElementById("ErrorMessage").style.display = "none";	
	}
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
// Soumission du formulaire	de question
function ValidQuestion(){
	
	DisplayErrorSubmission(false);
	var objForm = document.frmQuestion;

	if ((intErr = Valid(objForm.strLanguage.value, "<%=k_strLng%>")) != k_intOk ) { alert("Question 1: " + AffErr("<%=k_strLng%>", intErr)); return false; }
	if ((intErr = ValidRadioChecked(objForm.booAnonymous)) != k_intOk){ alert("Question 2: " + AffErr("<%=k_strLng%>", intErr)); return false; }
	if ((intErr = ValidSelection(objForm.strICUOutcome)) != k_intOk){ alert("Question 3: " + AffErr("<%=k_strLng%>", intErr)); return false; }	
	if ((intErr = ValidRadioChecked(objForm.booRealPatient)) != k_intOk){ alert("Question 4: " + AffErr("<%=k_strLng%>", intErr)); return false; }	
	if ((intErr = ValidSelection(objForm.strSendBy)) != k_intOk){ alert("Question 5: " + AffErr("<%=k_strLng%>", intErr)); return false; }		
	if ((intErr = ValidRadioChecked(objForm.booPatientAgree)) != k_intOk){ alert("Question 6: " + AffErr("<%=k_strLng%>", intErr)); return false; }		
	if ((intErr = ValidRadioChecked(objForm.booPatientRequest)) != k_intOk){ alert("Question 7: " + AffErr("<%=k_strLng%>", intErr)); return false; }	

	//Si réponse à Q2, Q4 ou Q6 n'est pas YES
	// la lettre ne peut pas être soumise
	if (getCheckedValue(objForm.booAnonymous) == "0"){DisplayErrorSubmission(true);return false;}
	if (getCheckedValue(objForm.booRealPatient) == "0"){DisplayErrorSubmission(true);return false;}
	if (getCheckedValue(objForm.booPatientAgree) == "0"){DisplayErrorSubmission(true);return false;}
	
	return true;
}	

// Soumission du formulaire	par chargement de fichier
function ValidByUpload(){
	var objForm = document.frmByUpload ;
	if ((intErr = Valid(objForm.strGuidTestimony.value,'<%=k_strLng%>')) != k_intOk){alert('General error : '+ AffErr('<%=k_strLng%>',intErr));return false;}
	//if ((intErr = Valid(objForm.strCommentaire.value,'<%=k_strLng%>')) != k_intOk){alert('Comment: '+ AffErr('<%=k_strLng%>',intErr));return false;}
	if (document.getElementById("TestimonyList").innerHTML == '') {alert('Please import a file.');return false;}
	//if ((intErr = ValidRadioChecked(objForm.strGeneralConditions)) != k_intOk){alert('You must accept the general conditions');return false;}
	
	var objForm = document.frmByUpload
	objForm.action = "/ModuleTestimony/Public/Actions/TestimonyAction.asp";
	objForm.encoding = "application/x-www-form-urlencoded";
	objForm.target = "_self";

	return true;
}	

function SubmitByUpload() {
	document.frmByUpload.submit();
/*		
		var aUrl = "/ModuleTestimony/Public/Actions/TestimonyAction.asp?X=" + Math.random();
		var newDiv = document.createElement('div');
		dojo.addClass("optionBox", "Notification");
		var d = cyim.ajax.xhrPost({ form: dojo.byId('frmByUpload'), url: aUrl, handleAs: "text" });
		d.addCallback(dojo.hitch(cyim.ajax, "getDataBack", newDiv,
			function() {
				dijit.byId('optionBox').attr("content", newDiv);
			}
		));
*/		
}	

// Soumission du formulaire	par email
function ValidByEmail(){
	var objForm = document.frmByUpload ;
	if ((intErr = Valid(objForm.strGuidTestimony.value,'<%=k_strLng%>')) != k_intOk){alert('General error : '+ AffErr('<%=k_strLng%>',intErr));return false;}
	//if ((intErr = Valid(objForm.strCommentaire.value,'<%=k_strLng%>')) != k_intOk){alert('Comment: '+ AffErr('<%=k_strLng%>',intErr));return false;}
	if ((intErr = Valid(getEditorValue('strCorps'),'<%=k_strLng%>')) != k_intOk){alert('Body: '+ AffErr('<%=k_strLng%>',intErr));return false;}	
	dojo.byId("strCorps").value = getEditorValue('strCorps');
	//if ((intErr = ValidRadioChecked(objForm.strGeneralConditions)) != k_intOk){alert('You must accept the general conditions');return false;}
	
	var objForm = document.frmByUpload
	objForm.action = "/ModuleTestimony/Public/Actions/TestimonyAction.asp";
	objForm.encoding = "application/x-www-form-urlencoded";
	objForm.target = "_self";

	return true;
}	

function SubmitByEmail() {
	if (ValidByEmail()){

	document.frmByUpload.submit()
	}
}	
