/*
  This function is used to check the status of the Editor
  If the value is open then the Editor will be kept open on page Load   
  else it will be closed on page Load
*/

function checkEditorStatus(){
if(document.getElementById('editorStatus').value!="" && document.getElementById('editorStatus').value == "open"){
	document.getElementById('threadEditor').style.display = "block";
	document.getElementById('floatRight').style.display = "none";
	document.getElementById('createThread').innerHTML = "";
}
}

/*
  This function is used to open the Text Editor while Creating a Thread, 
  Will be called on click of Create Thread Button
  Following fields are updated using this function
    -> Change the value of editorStatusField to open
    -> Change the display of threadEditor div to block
    -> Hide the CreateThread Button
    -> Set the Focus of cursor to Thread title Field
*/

function showEditor(){
document.getElementById('editorStatus').value = "open";
document.getElementById('threadEditor').style.display = "block";
document.getElementById('floatRight').style.display = "none";
document.getElementById('createThread').innerHTML = "";
document.getElementById('paginationTD').className = "toolPadB20LR25";
document.getElementById('threadTitle').focus();
}


/*
This function return the object of form element 
@param elementId, Unique id of the form
@retrun form object
*/

function getElement(elementId) {
if (document.getElementById) {
return document.getElementById(elementId);
} else if (document.all) {
return document.all[elementId];
} else if (document.layers){
return document.layers[elementId];
} else {
return false;
}
}


/**
 * DHTML date validation script.
 */
// Declaring valid date character, minimum year and maximum year

var dtCh= "/";
var minYear=8;
var maxYear=99;

/*
*  This function is used to check whether the passed value is Integer
*  Will return true if the value is integer, false otherwise
*/

function isInteger(s)
{ 
  for(var i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
        return false;
    }
    // All characters are numbers.
    return true;
}


function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

/*
*  This function is used to check the days in the Feb month for the passed value of year
*/

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/*
*  This function is used to create an Array for days based on the passed month value
*/

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

/*
*  This function is used to check the validity of a date 
*  Will return true if the date is valid, false otherwise 
*/


function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	
	if (strDay.charAt(0)=="0" && strDay.length>1) 
	   strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
	   strMonth=strMonth.substring(1);
	
	
	for (var i = 1; i <= 2; i++) {
	    if (strYr.charAt(0)=="0" && strYr.length>1) {
		strYr=strYr.substring(1);
		}
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 2 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}if(year==minYear && month < 6){
	    return false;
	}
return true;
}


/*
*  This Funtion is called to check blankspaces in a Field
*/


function isEmpty(mytext) {
var re = /^\s{1,}$/g; //match any white space including space, tab, form-feed, etc.
if ((mytext.length==0) || (mytext==null) || ((mytext.search(re)) > -1)) {
return true;
}else {
return false;
}
}



/**
*  This function is called to enable the Textbox of durationDate
*  Will be called on the change of the value of Radio Button 
*  @param, value takes the boolean value and changes the value accordingly
*/

function enableTextBox(value)
{
if(value){
document.getElementById('durationDate').disabled = false;
  if(document.getElementById('durationDate').value == "dd/mm/yy" || document.getElementById('durationDate').value == ""){
     document.getElementById('durationDate').value = "dd/mm/yy";
  }
}else{
document.getElementById('durationDate').disabled = true;
document.getElementById('durationDate').value = "";
}
}


/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer

        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}


/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, responseXmlHandler) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      // Check that a successful server response was received
      if (req.status == 200) {
        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseXML);
      } else {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

/**
*  This function will be called to open the modalpopup
*  @param type, type based on which the page in the popup is to be shown
*
*/


function showPollPopup(type){
var windowEdit;
var windowCreate;
var windowDelete;

if(type == "edit"){
windowEdit=window.open('/en_CA/savePoll/mode/edit', '','height=400,width=568,status=0,scrollbars=1');
windowEdit.moveTo(250,150);
}else if(type == "delete"){
windowDelete=window.open('/en_CA/tools/forum/includes/popup_poll_delete.jsp', '','height=128,width=285');
windowDelete.moveTo(250,150);
}else if(type == "create"){
windowCreate=window.open('/en_CA/createPoll', 'CreatePoll','height=400,width=568,status=0,scrollbars=1');
windowCreate.moveTo(250,150);
}
}

/*
*  This function is called to close the modalPopup
*/

function hidePollpopUp(){
window.top.hidePopWin();
}

/**
*  This function will make an AJAX request for getting the updated data of Poll
*  Will change the innerHTML of the poll HTML to the new response sent by the server
*/

function getnewPollData(){
// Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();
  req.open("GET", "/en_CA/savePoll/mode/get", true);
  req.send(null);
  req.onreadystatechange = function(){
   if(req.readyState == 4){
     if(req.status == 200) {
          if(window.parent.document.getElementById('changedData')!=null){
          window.parent.document.getElementById('changedData').innerHTML = req.responseText;
          }
      }
    }
  }  
}

/**
*  This function will make an AJAX request for getting the updated data of Poll
*  Will change the innerHTML of the poll HTML to the new response sent by the server
*/

function deletePost(postId){
windowDelete=window.open('/en_CA/tools/forum/includes/popup_post_delete.jsp?postId='+postId, '','height=128,width=285');
windowDelete.moveTo(250,150);
}

function deleteAndHidePost(postId){
// Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();
  req.open("GET", "/en_CA/postsUpdate/mode/deletePost/postId/"+postId,true);
  req.send(null);
  req.onreadystatechange = function(){
   if(req.readyState == 4){
     if(req.status == 200) {
        document.getElementById(postId).innerHTML="";   
      }
    }
  }  
}

function reportAbusePost(postId,locale){
// Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();
  req.open("GET", "/"+locale+"/postsUpdate/mode/abusePost/postId/"+postId,true);
  req.send(null);
  req.onreadystatechange = function(){
   if(req.readyState == 4){
     if(req.status == 200) {
        document.getElementById(postId).innerHTML="";   
      }
    }
  }  
}

/**
// On Load of Edit Poll Popup will disable the durationdate value
*/

function changeRadioButtonValues(){
document.getElementById('durationDate').disabled = false;
}

function setDurationDate(){
document.getElementById('durationDate').value = "";
}

