// JavaScript Document

function escapeIt(textarea){
	textarea.value = escape(textarea.value)
}

function unescapeIt(textarea){
	textarea.value = unescape(textarea.value)
}

function setCaretPosition(ctrl, pos) { 
	if(ctrl.setSelectionRange) { 
		ctrl.focus(); 
		ctrl.setSelectionRange(pos,pos);
	 } else if (ctrl.createTextRange) { 
	 	var range = ctrl.createTextRange(); 
		range.collapse(true); 
		range.moveEnd('character', pos); 
		range.moveStart('character', pos); 
		range.select(); 
	} 
}

function trimText(textarea) {
	textarea.value=escape(textarea.value);

	var a;
	do {
		l = textarea.value.length;
		a=0;
		for(i=0; i<=textarea.value.length; i++){
			if(textarea.value.indexOf("%09") > -1){
				textarea.value=textarea.value.replace("%09","");
				a=1;
			}
		}
	}
	while (a == 1);
	a = textarea.value.length;
	if (a == 3 ) {
		if(textarea.value.indexOf("%20") > -1){
			textarea.value=textarea.value.replace("%20","");
		}
	}
	if (a == 6 ) {
		if(textarea.value.indexOf("%20%0A") > -1){
			textarea.value=textarea.value.replace("%20%0A","");
		}
	}

	if (a == 9 ) {
		if(textarea.value.indexOf("%20%0D%0A") > -1){
			textarea.value=textarea.value.replace("%20%0D%0A","");
		}
	}

	textarea.value = unescape(textarea.value); 

	setCaretPosition(textarea, textarea.value.length  );  
}

/* The above code will remove hex copntrol cahracters from a textarea, needed with firefox for some reason.
	functions used are:
		escape 	- The escape() function encodes special characters, with the exception of: * @ - _ + . /
		unescape - decode strings encoded with escape	
		setCaretPosition - find the last text position and set the caret (cursor) after that character
		trimText - drives the process by finding the control characters and replacing them with a null 
*/		

var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var textarea; 
var currId; 
var http;

function createRequestObject() {
    var tmpXmlHttpObject;
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) { 
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return tmpXmlHttpObject;
}

function processResponse() {
    //check if the response has been received from the server
    if(http.readyState == 4){
        //read and assign the response from the server
		
    	var response = http.responseText;
		if (response == 1 ) {
			document.getElementById('answerWaiting').innerHTML = 'Your technician has replied to your question.  <a href="answerList.php" ><span>Please click here to see the response</span></a>';
		} else {
			document.getElementById('answerWaiting').innerHTML = '';
        }                 
    }
}

function sendCheckRequest (){
	http = createRequestObject();
    // Set the length of the timer, in seconds
    http.open('get', './checkIt.php?id=' + currId, true);
    //assign a handler for the response
    http.onreadystatechange = processResponse;
    //actually send the request to the server
    http.send(null);
}

function InitializeTimer() {
    // Set the length of the timer, in seconds
    secs = 60;
    loopTimer();
}

function loopTimer() {
   sendCheckRequest ();
//   var tm = valueOf();
//   alert ('time is '+ tm ); 
   StopTheClock();
	secs = 60;
    StartTheTimer()
}
function StopTheClock(){
    if(timerRunning){
        clearTimeout(timerID);
	}
    timerRunning = false;
}

function StartTheTimer() {
    if (secs==0)    {
        StopTheClock();
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
		loopTimer();
    }  else   {
        self.status = secs;
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", delay);
    }
}


function setTimer(textareaName, id) {
	currId = id;
	textarea = document.getElementById(textareaName);
	trimText(textarea);
	InitializeTimer();
}

function alertFunction() {
	alert ("\nYou must be logged in as a member to use this function\n");
}

function acknowledge() {
	alert ("\nThank you for your submission.  Your technician will answer it as soon as possible.\n\nWhen your technician responds; the following message will be displayed on the home page:\n\t\"Your technician has replied to your question.\"\n\nAn email message will also be sent to you notifying you that your technician has responded to your question.\n");
}

function awaitAproval() {
	alert ("\n Thank you for providing this article\n Your article has been referred to the site administrator for approval.\n");
}

function submit_form() {
	document.answerList.submit(); 

}

function submit_question(choice) {
	document.askquestion.mSubmit.value= choice; 
	document.askquestion.submit(); 
}

function submitUpdate() {
	document.updateItem.closeIt.value = 1;
	document.updateItem.submit(); 

}

function load_function(choice) {
	document.review.submit(); 

}
