// Global variables
// timerFlag is a global timer set to clear the flag's message window
var timerFlag;
// timerAjaxFlag is a global timer set to keep track of how long it is
// taking for the Geogad server to respond. If it is longer than some
// prefered value, the program redirects the user input to another operation.
var timerAjaxFlag;


function isAjaxSupportedFlag() {
// Check if request can be created by is not used
   var request = null;
   if(typeof window.XMLHttpRequest != 'undefined')
   {
     request = new XMLHttpRequest();
   }
   else if(typeof window.ActiveXObject != 'undefined')
   {
     try { request = new ActiveXObject('Microsoft.XMLHTTP'); }
     catch(err) { request = null; }
   }
   if (request != null)
   	return(true); 
   else 
   	return(false); 

}

function flagSuccess(id) {
	window.clearTimeout(timerFlag);
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="flagBox"><div style="text-align: center" ><span class="blueText">Thanks for flagging!<br/>Geogad will check it out.</span></div></div>';
	timerFlag = window.setTimeout('flagClearMessage("'+id+'")', 7000); 
}

function flagWaitMessage(id) {
	window.clearTimeout(timerFlag);
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="flagBox"><div style="text-align: center" >Sending....</span></div></div>';
}

function flagProblemMessage(id) {
	window.clearTimeout(timerFlag);
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="flagBox"><div style="text-align: center" ><span class="yellowBackground redfont">Problem sending flag. Please try again later.</span></div></div>';
	timerFlag = window.setTimeout('flagClearMessage("'+id+'")', 7000); 
}

function flagSelectMessage() {
	var rt = document.getElementById("dynamicMenuBox_error");
	rt.innerHTML = 'Select the reason for flagging.';
}


function flagClearMessage(id) {
	var rt = document.getElementById(id);
	rt.innerHTML = '';
}


function flagClearTimers() {
// free callback timer here
   window.clearTimeout(timerFlag);
   window.clearTimeout(timerAjaxFlag);
}


function ChangeFocusFlag(switchFocus2, value) {
   if (value != null) {
	// User has selected a valid value; Switch focus 
        setfocus(switchFocus2);
   }
}

// Handler function for AJAX call
function print2DynamicMenuBox4Flag(data) {

// free callback timer here
   window.clearTimeout(timerAjaxFlag);
// check for returned data
   if ((data == "") || (data == null)) {
   	flagSuccess("dynamicMenuBox")
   }  else {
   	flagProblemMessage("dynamicMenuBox");
   }

return false;

}

function sendFlag(nickname, tID, ctID, tsID, box) { 

   // old browsers technique - seems to work fine
   var selectBox = document.getElementById("flagSelect");
   var flag = selectBox.options[selectBox.selectedIndex].value;

   if (flag == null) {
	flagSelectMessage();
	return false;
   }
   if (flag.indexOf("Reason") < 0)  {
   	if (isAjaxSupportedFlag()) {
		window.clearTimeout(timerAjaxFlag);
   		JGetFlagInfo.createFlagJS(nickname, tID, ctID, tsID, flag, print2DynamicMenuBox4Flag);
		timerAjaxFlag = window.setTimeout('flagProblemMessage("'+box+'")', 30000); 
		flagWaitMessage(box);
		return false;
	} else { 
		return true ; 
	} 
   } else {
	flagSelectMessage();
	return false;
   }

}






function DisplayFlagBox(thisNickname, thisTID, thisCTID, thisTSID, elem) {

   flagClearMessage(elem);
   var box = document.getElementById(elem);
   var flagBoxInfo = "<div class='flagBox'>"+
		"<div style='text-align: right' >(<a class='easyLink' title='Close this box' href='#' onclick='flagClearMessage(\"dynamicMenuBox\"); return false;' >Close</a>)</div>"+
                "<div style='text-align: center' >"+
                "<span class='flagTitle'>Flag As Unacceptable</span></div>"+
		"<div style='text-align: left' >"+
                "<p>Please indicate your reason for flagging below. We will review your submission.</p>"+
                "<p>If you are the copyright owner of material in this tour stop and believe that it "+
                "is being used without your permission, please let us know by following <a href='copyright'>these directions</a> "+
                "to submit a copyright infringement notice to Geogad.</p></div>"+                
                "<FORM  ENCTYPE='application/x-www-form-urlencoded' ACTION='./NewFlag' METHOD=POST name='FlagForm' id='FlagForm' "+
		//" onsubmit='sendFlag(\""+thisNickname+"\", \""+thisTSID+"\", \""+elem+"\");' >"+    
		" onsubmit='return sendFlag(\""+thisNickname+"\", \""+thisTID+"\", \""+thisCTID+"\", \""+thisTSID+"\", \""+elem+"\"); ' >"+    
                "<div style='text-align: center' >"+
                "<span id='dynamicMenuBox_error' name='dynamicMenuBox_error' class='loudred'> "+
                "</span><br/>"+
                "<select name='flagSelect' id='flagSelect' "+
                "onChange='ChangeFocusFlag(sendFlagButton, this.value);' >"+
                "<option value='Reason'>-Select a Reason-</option>"+
                "<option value='graphicV'>Graphic Violence</option> "+
                "<option value='hate'>Hate Speech</option> "+
                "<option value='TOUV'>Other Terms of Use Violation</option> "+
                "<option value='mature'>Mature (over 18 only) Content</option>"+
                "<option value='sexExp'>Sexually Explicit</option>"+
                "<option value='commercial'>Too Commercial</option>"+
                "</select> <br/>"+
                "<INPUT TYPE='submit' VALUE='Send Flag' name='sendFlagButton' id='sendFlagButton'  style='margin:1em 0em 1em 0em' />"+
                "<div style='text-align: left'></div>"+
                "</div>"+
                "<INPUT TYPE=hidden NAME='tidFlag' id='tsidFlag' VALUE="+thisTID+">"+
                "<INPUT TYPE=hidden NAME='ctidFlag' id='tsidFlag' VALUE="+thisCTID+">"+
                "<INPUT TYPE=hidden NAME='tsidFlag' id='tsidFlag' VALUE="+thisTSID+">"+
                "<INPUT TYPE=hidden NAME='cidFlag' id='cidFlag' VALUE='"+thisNickname+"'>"+
                "</FORM> </div>";
   box.innerHTML += flagBoxInfo;

}