
var feedbackDiv;
function showFeedback() {
    
    zindex = 5;
    feedbackDiv = new ShowSurvey('/feedback.txt', zindex);
    //var feedback = new cweb.widgets.ModalWindow("feedback", zindex);

    feedbackDiv.render();
  }

function ShowSurvey(p_url, p_zindex) {
  
  this.surveyDiv = document.createElement('div');
  this.surveyDiv.className = 'feedback';
  this.surveyDiv.contentDiv = addDiv(this.surveyDiv, "position: relative; height: 100%; width: 100%");
  var zindex = p_zindex;
   if (!zindex) {
     zindex = this.surveyDiv.style.zIndex + 5;
   }
   var height = document.body.scrollHeight;
   if (!height) {  
     height = document.body.offsetHeight;
   }
  
  
  this.surveyDiv.blockingDiv = addDiv(document.body, "background-color: #cccccc; top: 0px; left: 0px; position: absolute; width: 100%; height: "+height+"px; z-index: "+(zindex-1)*1+"; opacity: .75; filter: alpha(opacity=75);");
   document.body.appendChild(this.surveyDiv);
   this.surveyDiv.style.cssText = 'width: 395px; height: auto; position: absolute; left: 300px; top: 150px; background-color: #ffffff; border: solid 1px #737377; padding: 20px; z-index: '+zindex;
   this.surveyDiv.center = function() {
     var winWidth = 0, winHeight = 0;
     try {
          if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            winWidth = window.innerWidth;
            winHeight = window.innerHeight;
          }
          else
            throw "";
      } catch (e) {
        if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            winWidth = document.documentElement.clientWidth;
            winHeight = document.documentElement.clientHeight;
          } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            winWidth = document.body.clientWidth;
            winHeight = document.body.clientHeight;
          }
      }
      
      if (winWidth > 0)
      {
         var newLeft = (winWidth - this.offsetWidth)/2;
         var newTop = (winHeight - this.offsetHeight)/2;
     
         this.style.left = newLeft + "px";
         //this.style.top = newTop + "px";
      }
   }
  
  this.surveyDiv.contentDiv.style.margin = '0px';
  this.surveyDiv.contentDiv.style.padding = '0px';
  this.surveyDiv.url = p_url;
  
  this.surveyDiv.render = function() {
    var xmlhttp = getXMLHttpObject();
    xmlhttp.open("GET", this.url, false);
    xmlhttp.send(null);
    if (xmlhttp.responseText) {
        this.contentDiv.innerHTML = xmlhttp.responseText;
    }
    this.getFooter();
    this.center();   
  }


  this.surveyDiv.getFooter = function() {
    this.sbutton = document.getElementById('fdSub');
    this.sbutton.disabled = true;
    this.sbutton.onclick = function() { 
    var body = null;
    
    for(var i=1; i<=6; i++) {
      var id = 'q' + i;
      var inputText = document.getElementById(id);
      var value = inputText.value;      
      if(value && value != '') {
        if(!body) {
          body = inputText.name+"="+value;
        } else {
          body = body + "&" + inputText.name+"="+value;
        }
      }
    }
    var result = submitFeedback('/feedback.aspx',body);
    this.sbutton.disabled = true;
    //alert("Thank you for taking the time to fill out our survey!"); 
    this.close();
  }.bind(this);
  
  }

  this.surveyDiv.close = function() {
    document.body.removeChild(this.blockingDiv);
    this.contentDiv.innerHTML = "";
    this.removeChild(this.contentDiv);
    document.body.removeChild(this);
  }

  return this.surveyDiv;

}


function submitFeedback(url, body) {
  var xmlhttp, result;
  //var sbutton = document.getElementById('fdSub');
  //sbutton.disabled = true;
  
  xmlhttp = getXMLHttpObject();
  xmlhttp.open('POST', url, false);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send(body);
  result=xmlhttp.responseText;
  
  return result;
  //alert("Thank you for taking the time to fill out our survey!");
	
}

 
 // To check if the input fields have any value or not and enabling submit button
 // if there are any values given
  function enableSubmit() {
    var val = String(this.value);
    var sbutton = document.getElementById('fdSub');
    
    if (val.length == 0) {      sbutton.disabled = true;
    } else {
      sbutton.disabled = false;
    }
  }
  
 function addDiv(parent, style, strText) {
    var ele = document.createElement('div');
    setStyle(ele, style);
    if (parent) {
        parent.appendChild(ele);
    }
    if (strText) {
        addText(ele, strText);
    }
    return ele;
 }
        
 function addText(parent,strText){
    var txtNode=document.createTextNode(strText);
    parent.appendChild(txtNode);
    return txtNode;
 }
 
 function setStyle(ele,styleText){
    if(ele.style&&styleText){
        if(typeof styleText=="string"){
            if(styleText.indexOf(":") > 0){
                ele.style.cssText=styleText;
            }else{
                ele.className=styleText;
            }
        }else{
            for(attr in styleText){
                try{
                    if(ele.style[attr]!=styleText[attr]){
                        ele.style[attr]=styleText[attr];
                    }
                }
                catch(e){
                }
            }
        }
    }
}

function getXMLHttpObject()
{
    var xmlhttp;
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        var MSXML_XMLHTTP_PROGIDS = new Array(
            'MSXML2.XMLHTTP.5.0',
            'MSXML2.XMLHTTP.4.0',
            'MSXML2.XMLHTTP.3.0',
            'MSXML2.XMLHTTP',
            'Microsoft.XMLHTTP'
        );
        var success = false;
        for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
            try {
                xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                success = true;
            } catch (e) {}
        }
            /*if ( !success ) {
                alert('Cant create XMLHttpRequest - not supported');
            }*/
    }
    return xmlhttp;
}

Function.prototype.bind=function(objTarget){
    var objSrc = this;
    return function(){
        return objSrc.apply(objTarget,arguments);
    };
};


window.onresize = function () {
	if (feedbackDiv)
		feedbackDiv.center();
};