// JavaScript Document
// August 17, 2007 ANW
// This is the Ajax and other JS added to the any page utilizing the Rate This Page Form
//

var my_obj;
xl_AttachEvent(window, "load", init);
xl_AttachEvent(window, "load", setMaxLength);

    function init() 
        {
         my_obj = xl_GetObj("ajaxsubmit");
         xl_AttachEvent(my_obj, 'click', ClickHandler);
         // xl_AttachEvent(document.RateThisPageEntry.ajaxsubmit, 'click', ClickHandler);
        }
     
        
     function ClickHandler(e) 
     	{

     	  var rpnum = document.RateThisPageEntry.RPNUM.value;
     	  var rptype = document.RateThisPageEntry.RPTYPE.value;
     	  var info = document.RateThisPageEntry.info.value;			
		  
		  // the info field is actually hidden on the page 
		  // if it is filled out then it is likely spam
		  // so we exit the program
		  if(info)
		  {
			return;
		  }
		  
     	  for (var i=0;i<=2;i++) {
        	if(document.RateThisPageEntry.RPSOPR[i].checked == true) {
        	   var rpsopr = document.RateThisPageEntry.RPSOPR[i].value;
        	}
        }
        
         //Kim 20110419: KB task pages were getting javascript errors with Object Null - xl_GetObj was using "ajaxsubmit"
	 //which didn't exist in the document - changed that in kb_task.pgm and add missing form inputs email, searchvalue and prodname info below
	 //also added return false to the onClick in the form so it didn't submit twice.

     	  var rpcomm = document.RateThisPageEntry.RPCOMM.value +  '<br/>Contact info: ' +  document.RateThisPageEntry.email.value + '<br/>Domain: ' + document.domain;
     	  var searchval = document.RateThisPageEntry.searchvalue.value;
     	  var product = document.RateThisPageEntry.prodname.value;
     	  
     	  
     	  url = 'http://esdi.excelsystems.com/wsexmp/ratepga.pgm?task=ajaxupdate&RPNUM='+rpnum+'&RPTYPE='+rptype+'&RPSOPR='+rpsopr+'&RPCOMM='+rpcomm+'&searchvalue='+searchval+'&prodname='+product;
     	  var div = xl_GetObj('rateThisPage');
     	  xl_AjaxUpdate(url,div,testload);
		}
		
	function testload(div,response)
		{
  		  div.innerHTML = response;		 
        }
  
    var clickCounter = 1;
    
  	function replaceText() {
  		if (clickCounter == 1)
		{
  			document.getElementById('textarea').value = "";
  		}
  		clickCounter++;
  		}
	function setMaxLength() 
	{
		var x = document.getElementsByTagName('textarea');
		var counter = document.createElement('div');
		counter.className = 'counter';
		for (var i=0;i<x.length;i++)
		{
			if (x[i].getAttribute('maxlength')) 
			{
				var counterClone = counter.cloneNode(true);
				counterClone.relatedElement = x[i];
				counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
				x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
				x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
				x[i].onkeyup = x[i].onchange = checkMaxLength;
				x[i].onkeyup();
			}
		}
	}
  function checkMaxLength() 
  {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
	{
		this.relatedElement.className = 'toomuch';
		alert("You've exceeded the maximum number of charcters for this feedback");
	}
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	
}
