var gWDYTNPageTbl01=1;
var gWDYTNPageTbl02=1;
var gWDYTNPagQtd=5;

var __SELECTEDCOLOR = 'dodgerblue'
var __NOTSELECTEDCOLOR = 'white'

var __BOLD = 'bold'
var __NOTBOLD = 'bold'

function InitWDYTN(){
	gWDYTNPageTbl01=1;
	gWDYTNPageTbl02=1;
	gWDYTNPagQtd=5;
	WDYTNBehavior();
	WDYTNRefreshList1();
	WDYTNRefreshList2();
}

function WDYTNRefreshList1(){
	deleteRows("tblMySurvey",1);
	var oRow=document.getElementById("tblMySurvey").insertRow(document.getElementById("tblMySurvey").rows.length);
	var oCell=document.getElementById("tblMySurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
	oCell.height = '17';oCell.align = 'center';oCell.bgColor="#FFFFFF";oCell.vAlign="top";oCell.colSpan="4";
	oCell.innerHTML = '<p class="txt-gris-osc" style="font-size:small;color:red">&nbsp;'+MSG_WDYTN02+'</p>';
	setTimeout( 'WDYTNshowMyQuestionList1();', 50);
}

function WDYTNshowMyQuestionList1(){

	var oRow;
	var oCell;
	var sFiller;

	document.getElementById('divCenter').onlayoutcomplete = null;
	XML_SurveyData = Sarissa.getDomDocument();

	if ( SaveXML(XML_SurveyData,'../ProcessSurvey.ecgp?ac=getSurvey&mode=MyOwn&ps='+getCookie('ecgplogin')+'&pag='+gWDYTNPageTbl01+'&pagqtd='+gWDYTNPagQtd) ){
		deleteRows("tblMySurvey",1);

		for (n=0;n< XML_SurveyData.getElementsByTagName("Survey").length; n++ ){
			var oNode = XML_SurveyData.getElementsByTagName("Survey").item(n);

			if (oNode.getAttribute('id').length>0){
				oRow=document.getElementById("tblMySurvey").insertRow(document.getElementById("tblMySurvey").rows.length);

				oCell=document.getElementById("tblMySurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'left';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML =  '<p class="txt-gris-osc"><b><span style="cursor:hand" onclick="goToSurvey('+oNode.getAttribute('id')+');">'+oNode.getElementsByTagName("Question").item(0).text+'</span></b></p>';
				oCell.innerHTML += '<p class="txt-gris-osc">STATUS: '+oNode.getElementsByTagName("Project").item(0).getElementsByTagName("Status").item(0).text+'</p>';

				oCell=document.getElementById("tblMySurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'left';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML =  '<p class="txt-gris-osc">'+String(oNode.getElementsByTagName('StartDate').item(0).text).substring(0,10)+'</p>';

				oCell=document.getElementById("tblMySurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'center';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML = '<p class="txt-gris-osc"><img style="cursor:hand" onclick="goToReport('+oNode.getAttribute("id")+')" src="../img/WDYTN/resultados_05.gif" alt="'+oNode.getElementsByTagName("Answers").item(0).getElementsByTagName("Complete").item(0).text+' '+MSG_WDYTN06+'"/></p>';

				oCell=document.getElementById("tblMySurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'center';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML = '<p class="txt-gris-osc"><input type="checkbox" id="'+oNode.getAttribute('id')+'"/></p>';
			}
		}
	}

	WDYTNBehavior();
}


function loadWDYTNContent(divId,pathToFile){
	if ( getCookie('ecgplogin') ){
		var loadingHTML = '<br/><br/><br/><p align="center"><img src="img/Common/Loading.gif"/></p>';

		if (divId == 'divCenter' ) {
			var s1 = document.getElementById('divCenter').innerHTML;
			if ( s1.indexOf('Common/loading.gif')>0){
				return false;
			}
		}

		var ajaxIndex = dynamicContent_ajaxObjects.length;
		if (divId=='divCenter'){
			document.getElementById(divId).innerHTML = loadingHTML;
		}
		dynamicContent_ajaxObjects[ajaxIndex] = new sack();
		dynamicContent_ajaxObjects[ajaxIndex].method  = "GET";
		dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;

		dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ showWDYTNContent(divId,ajaxIndex,pathToFile); };  
		dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
  
	}  
	else{
		ajax_loadContent('divCenter','NotLogged.html');
	}
} 

function showWDYTNContent(divId,ajaxIndex,pathToFile){
	document.getElementById(divId).innerHTML = dynamicContent_ajaxObjects[ajaxIndex].response;
	dynamicContent_ajaxObjects[ajaxIndex] = null ;
	InitWDYTN();
}

function WDYTNBehavior(){
}

function prevWDYTNPage(t){
	if (t==1){
		if (--gWDYTNPageTbl01==0){
			++gWDYTNPageTbl01;
		}
		WDYTNRefreshList1();
	}
	else{
		if (--gWDYTNPageTbl02==0){
			++gWDYTNPageTbl02;
		}
		WDYTNRefreshList2();
	}
}

function firstWDYTNPage(t){
	if (t==1){
		gWDYTNPageTbl01=1;
		WDYTNRefreshList1();
	}
	else{
		gWDYTNPageTbl02=1;
		WDYTNRefreshList2();
	}
}

function refreshWDYTNPage(t){
	if (t==1){
		WDYTNRefreshList1();
	}
	else{
		WDYTNRefreshList2();
	}
}

function nextWDYTNPage(t){
	if (t==1){
		++gWDYTNPageTbl01;
		WDYTNRefreshList1();
	}
	else{
		++gWDYTNPageTbl02;
		WDYTNRefreshList2();
	}
}

function goToSurvey(s,p){
	window.open('../../opinio/s?s='+s+'&opdata_ps='+getCookie('ecgplogin') );
}

function goToReport(s){
	window.open('../English/Account/reportWDYTN.php?surveyid='+s, 'WDYTN','menubar=0,scrollbars=1,status=0,toolbar=0,top=50,height=500,left=50,width=300');
}

function sendWDYTNQuestion(t){
	var i;
	var oRow;
	var sQuestion;
	var sActualStatus;

	nWDYTNSurveyId=0;

	for (i=1; i<document.getElementById(t).rows.length; i++ ){
		if (document.getElementById(t).rows.item[i].cells.item[3].getElementsByTagName('INPUT').item(0).checked){
			nWDYTNSurveyId = document.getElementById(t).rows.item[i].cells.item[3].getElementsByTagName('INPUT').item(0).id;
			break;
		}
	}

	if (nWDYTNSurveyId==0){
		alert(MSG_WDYTN03);
	}
	else{
		for (n=0; n<XML_SurveyData.getElementsByTagName('Survey').length; n++){
			if (XML_SurveyData.getElementsByTagName('Survey').item(n).getAttribute('id')==nWDYTNSurveyId){
				oNode = XML_SurveyData.getElementsByTagName('Survey').item(n);
				if (! (	oNode.getElementsByTagName("Project").item(0).getElementsByTagName("Status").item(0).getAttribute('id')=='0' || 
						oNode.getElementsByTagName("Project").item(0).getElementsByTagName("Status").item(0).getAttribute('id')=='19' || 
						oNode.getElementsByTagName("Project").item(0).getElementsByTagName("Status").item(0).getAttribute('id')=='27' )
					){
					nWDYTNSurveyId=0;
					sActualStatus = oNode.getElementsByTagName("Project").item(0).getElementsByTagName("Status").item(0).text;
				}
				break;
			}
		}
		
		if (nWDYTNSurveyId!=0){
			loadFriendContent('divCenter','Account/myaccount_4.inc.html');
		}
		else{
			alert(MSG_WDYTN04+' '+sActualStatus+' '+MSG_WDYTN05);
		}
	}
}

function WDYTNRefreshList2(){
	deleteRows("tblSentSurvey",1);
	var oRow=document.getElementById("tblSentSurvey").insertRow(document.getElementById("tblSentSurvey").rows.length);
	var oCell=document.getElementById("tblSentSurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
	oCell.height = '17';oCell.align = 'center';oCell.bgColor="#FFFFFF";oCell.vAlign="top";oCell.colSpan="4";
	oCell.innerHTML = '<p class="txt-gris-osc" style="font-size:small;color:red">&nbsp;'+MSG_WDYTN02+'</p>';
	setTimeout( 'WDYTNshowMyQuestionList2();', 50);
}

function WDYTNshowMyQuestionList2(){

	var oRow;
	var oCell;
	var sFiller;
	var bOnlyFromFriends = (document.getElementById('chk_OnlyFromFriend').checked?1:0);
	document.getElementById('divCenter').onlayoutcomplete = null;
	XML_SurveyData2	= Sarissa.getDomDocument();
	
	if ( SaveXML(XML_SurveyData2,'../ProcessSurvey.ecgp?ac=getSurvey&mode=Received&of='+bOnlyFromFriends+'&ps='+getCookie('ecgplogin')+'&pag='+gWDYTNPageTbl02+'&pagqtd='+gWDYTNPagQtd) ){
		deleteRows("tblSentSurvey",1);
		for (n=0;n< XML_SurveyData2.getElementsByTagName("Survey").length; n++ ){
			var oNode = XML_SurveyData2.getElementsByTagName("Survey").item(n);

			if (oNode.getAttribute('id').length>0){
				oRow=document.getElementById("tblSentSurvey").insertRow(document.getElementById("tblSentSurvey").rows.length);

				var oNodePerson = oNode.getElementsByTagName('Person').item(0);
				sFiller = '<p class="txt-gris-osc">' + oNodePerson.getElementsByTagName('Nickname').item(0).text;
				sFiller += '('+oNodePerson.getElementsByTagName('Country').item(0).getElementsByTagName('Name').item(0).text+')'+MSG_WDTYN01;
				sFiller += String(oNode.getElementsByTagName('EndDate').item(0).text).substring(0,10);
				sFiller += '<br/><span style="cursor:hand" onclick="goToSurvey('+oNode.getAttribute('id')+');">'+oNode.getElementsByTagName("Question").item(0).text+'</span></p>';
				oCell=document.getElementById("tblSentSurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'left';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML = sFiller;

				oCell=document.getElementById("tblSentSurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'center';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML = '<p class="txt-gris-osc"><img style="cursor:hand" onclick="goToReport('+oNode.getAttribute("id")+')" src="../img/WDYTN/resultados_05.gif"/></p>';

				oCell=document.getElementById("tblSentSurvey").rows[oRow.rowIndex].insertCell(oRow.cells.length);
				oCell.height = '17';oCell.align = 'center';oCell.bgColor="#FFFFFF";oCell.vAlign="center";
				oCell.innerHTML = '<p class="txt-gris-osc"><input type="checkbox" id="'+oNode.getAttribute('id')+'"/></p>';
			}
		}
	}

	WDYTNBehavior();
}


function createWDYTNQuestion(){
	var sPath = window.location.pathname;
	var sSurvey;

	if (sPath.indexOf('Portuguese/')>-1){
		sSurvey = 253;
	}
	else if (sPath.indexOf('Spanish/')>-1){
		sSurvey = 254;
	}
	else{
		sSurvey = 255;
	}
	window.open('../../opinio/s?s='+sSurvey+'&opdata_ps='+getCookie('ecgplogin'));
}

function checkform(lang) {
  	var dt_test = new Date(document.frmform.birth_year.value, document.frmform.birth_month.value-1, document.frmform.birth_day.value);
  	
  	var re_email = /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;
	if (document.frmform.email.value=="" || !re_email.test(document.frmform.email.value)) {
		alert(MSG_WDTYN_REGISTER01);
     	document.frmform.email.focus();
     	return false;
	}
	else if (document.frmform.firstname.value.length==0) {
		alert(MSG_WDTYN_REGISTER02);
     	document.frmform.firstname.focus();
		return false;    
	}
	else if (document.frmform.lastname.value.length==0) {
		alert(MSG_WDTYN_REGISTER03);
     	document.frmform.lastname.focus();
		return false;
	}
	else if (document.frmform.username.value.length==0 || re_email.test(document.frmform.username.value)) {
		alert(MSG_WDTYN_REGISTER04);
     	document.frmform.username.focus();
		return false;
	}
	else if ((document.frmform.password.value.length==0)) {
		alert(MSG_WDTYN_REGISTER05);
     	document.frmform.password.focus();
		return false;    
	}
	else if (document.frmform.confirmpassword.value.length==0) {
		alert(MSG_WDTYN_REGISTER05);
     	document.frmform.confirmpassword.focus();
		return false;    
	}
	else if ((document.frmform.gender[0].checked==false)&&(document.frmform.gender[1].checked==false)) {
		alert(MSG_WDTYN_REGISTER06);
		return false;
	}
	else if ((document.frmform.birth_year.value.length==0)||(document.frmform.birth_month.value.length==0)||(document.frmform.birth_day.value.length==0)) {
		alert(MSG_WDTYN_REGISTER07);
	    document.frmform.birth_day.focus();
    	return false;    
	}
	else if (isNaN(Number(document.frmform.birth_year.value)) || isNaN(Number(document.frmform.birth_month.value)) || isNaN(Number(document.frmform.birth_day.value))) {
		alert(MSG_WDTYN_REGISTER07);
     	document.frmform.birth_day.focus();
		return false;
	}
	else if (dt_test.getMonth() != document.frmform.birth_month.value - 1) {
		alert(MSG_WDTYN_REGISTER07);
		return false;
	}
	else if (document.getElementById("country").options.value == "") {
		alert(MSG_WDTYN_REGISTER08);
     	document.frmform.country.focus();
		return false;  
	}
	else if ((document.frmform.state.value.length==0)&&(document.frmform.otherstate.value.length==0)) {
		alert(MSG_WDTYN_REGISTER09);
     	document.frmform.state.focus();
		return false;    
	}
	else if ((document.frmform.city.value == "")&&(document.frmform.othercity.value.length==0)) {
		alert(MSG_WDTYN_REGISTER10);
     	document.frmform.city.focus();
		return false;
	}
	else if (document.frmform.country_origin.value == "") {
		alert(MSG_WDTYN_REGISTER11);
     	document.frmform.country_origin.focus();
		return false;
	}
	else if (document.frmform.agree.checked == false) {
		alert(MSG_WDTYN_REGISTER12);
		return false;
	}
	return true;
}

function OtherV() {
 	var otherv = document.getElementById("otherv");
	var othervtxt = document.getElementById("othervtxt");

	if (otherv.checked == true) {
		showOtherV(othervtxt);
	} else {
		hideOtherV(othervtxt);
	}
}

function hideOtherV(othervtxt) {
	othervtxt.setAttribute("size", 1);
	othervtxt.style.display = "none";
	othervtxt.value = "";
}

function showOtherV(othervtxt) {
	othervtxt.setAttribute("size", 7);
	othervtxt.style.display = "block";
	othervtxt.value = "";
}

function OtherW() {
 	var otherw = document.getElementById("otherw");
	var otherwtxt = document.getElementById("otherwtxt");

	if (otherw.checked == true) {
		showOtherW(otherwtxt);
	} else {
		hideOtherW(otherwtxt);
	}
}

function hideOtherW(otherwtxt) {
	otherwtxt.setAttribute("size", 1);
	otherwtxt.style.display = "none";
	otherwtxt.value = "";
}

function showOtherW(otherwtxt) {
	otherwtxt.setAttribute("size", 7);
	otherwtxt.style.display = "block";
	otherwtxt.value = "";
}

function loadWDYTNRegister(divId,pathToFile) {
	var loadingHTML = '<br/><br/><br/><p align="center"><img src="img/Common/Loading.gif"/></p>';

	if (divId == 'divCenter' ) {
		var s1 = document.getElementById('divCenter').innerHTML;
		if ( s1.indexOf('Common/loading.gif')>0){
			return false;
		}
	}

	var ajaxIndex = dynamicContent_ajaxObjects.length;
	if (divId=='divCenter'){
		document.getElementById(divId).innerHTML = loadingHTML;
	}
	
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	dynamicContent_ajaxObjects[ajaxIndex].method = "GET";
	dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;
	
	dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ showWDYTNRegister(divId,ajaxIndex,pathToFile); };  
	dynamicContent_ajaxObjects[ajaxIndex].runAJAX();
}

function showWDYTNRegister(divId,ajaxIndex,pathToFile){
	document.getElementById(divId).innerHTML = dynamicContent_ajaxObjects[ajaxIndex].response;
	dynamicContent_ajaxObjects[ajaxIndex] = null;
	WDYTNgetState(document.getElementById('prevstate').value);
	WDYTNgetCity(document.getElementById('prevstate').value, document.getElementById('prevcity').value);
	document.getElementById('divPoll').style.display = "none";
}

function getQuestionTxt(s){
	var sReturn = '';
	if (s>0){
		for (n=0; n<XML_SurveyData.getElementsByTagName('Survey').length; n++){
			if (XML_SurveyData.getElementsByTagName('Survey').item(n).getAttribute('id')==s){
				oNode = XML_SurveyData.getElementsByTagName('Survey').item(n);
				sReturn = oNode.getElementsByTagName('Questions').item(0).getElementsByTagName('Question').item(0).text;
				break;
			}
		}
	}
	return sReturn;
}

function showWDYTNTable(p){
	hideWDYTNTable(p==1?2:1);
	var sId = 'rowTable' + p;
	for (i=1; i<4 ; i++){
		document.getElementById(sId+i).style.display = "inline";
	}
}

function hideWDYTNTable(p){
	var sId = 'rowTable' + p;
	for (i=1; i<4 ; i++){
		document.getElementById(sId+i).style.display = "none";
	}
}

function blockWDYTNSender(){
	var i;
	var nSurveyId;
	var nBlockedPersonId = Array();
	var index = 0;
	var sXML;

	for (i=1; i<document.getElementById('tblSentSurvey').rows.length; i++ ){
		if (document.getElementById('tblSentSurvey').rows.item[i].cells.item[2].getElementsByTagName('INPUT').item(0).checked){
		
			nSurveyId = document.getElementById('tblSentSurvey').rows.item[i].cells.item[2].getElementsByTagName('INPUT').item(0).id;
			for (n=0; n<XML_SurveyData.getElementsByTagName('Survey').length; n++){
				if (XML_SurveyData2.getElementsByTagName('Survey').item(n).getAttribute('id')==nSurveyId){
					oNode = XML_SurveyData2.getElementsByTagName('Survey').item(n);
					nBlockedPersonId[++index] = oNode.getElementsByTagName('Person')[0].getAttribute('id');
					break;
				}
			}
		}
	}

	if (index>0){
		sXML = '<ROOT><Person id5="'+getCookie('ecgplogin')+'"><WDYTNBlockedPerson>';
		for (i=1; i<=index;i++ ){
			sXML += '<Person id="' + nBlockedPersonId[i] + '"></Person>';
		}
		sXML += "</WDYTNBlockedPerson></Person></ROOT>";

		var XML_Data = Sarissa.getDomDocument();
		XML_Data.loadXML(sXML);
		if (XML_Data.parseError.reason != "") {
			var e = XML_Data.parseError;
			alert(e.reason + "\n" + e.srcText);
		} 
		else{
			if ( SaveXML(XML_Data,'../ProcessPerson.ecgp?ac=setBlockedPerson')){
				alert(MSG_WDYTN07);
			}
		}

		WDYTNRefreshList2();

	}
}