var htmlValidatorUrl = 'http://qa-dev.w3.org/wmvs/HEAD/check?output=soap12&uri=';
var cssValidatorUrl = 'http://qa-dev.w3.org/css-validator-proxy/validator?output=soap12&uri=';

function getHTTPObject() {
  var xmlhttp=false;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp=false;
    }
  }
  if (!xmlhttp && window.createRequest) {
    try {
      xmlhttp = window.createRequest();
    } catch (e) {
      xmlhttp=false;
    }
  }
  return xmlhttp;
}

// from http://erik.eae.net/archives/2005/07/03/20.19.18/
if (typeof DOMParser == "undefined") {
  DOMParser = function () {}
  
    DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
	var d = new ActiveXObject("MSXML.DomDocument");
	d.loadXML(str);
	return d;
      } else if (typeof XMLHttpRequest != "undefined") {
	var req = new XMLHttpRequest;
	req.open("GET", "data:" + (contentType || "application/xml") +
		 ";charset=utf-8," + encodeURIComponent(str), false);
	if (req.overrideMimeType) {
	  req.overrideMimeType(contentType);
	}
	req.send(null);
	return req.responseXML;
      }
    }
}

var htmlHttp = getHTTPObject();
var cssHttp = getHTTPObject();

function getParameter(param) {
  var uri = document.URL;
  var index;

  index = uri.indexOf("?");
  if (index == -1) {
    ret = null;
  }

  uri = uri.substring(index + 1);

  index = uri.indexOf(param + "=");

  if (index == -1) {
    return null;
  }

  index =  index + param.length + 1;
  var end  = uri.indexOf("&", index);

  if (end == -1) {
    return uri.substring(index);
  } else {
    return uri.substring(index, end);
  }
}

function doRequest(http, validatorUrl, uriToValidate, f) {
  var url = validatorUrl + uriToValidate;
  http.open("GET", url, true);
  http.onreadystatechange = f;
  http.send(null);
}

function validate() {
  var uriToValidate = getParameter('uri');
  if (uriToValidate == null) {
    updateInfo("No URI specified", "report");
    return;
  }
  updateInfo("In progress...", "html");
  doRequest(htmlHttp, htmlValidatorUrl, uriToValidate, handleHtmlResponse);
  updateInfo("In progress...", "css");
  doRequest(cssHttp, cssValidatorUrl, uriToValidate, handleCssResponse);
}

function handleHtmlResponse() {
  handleHttpResponse(htmlHttp, "http://www.w3.org/2005/10/markup-validator", "html", "markup");
}

function handleCssResponse() {
  handleHttpResponse(cssHttp, "http://www.w3.org/2005/07/css-validator", "css", "CSS");
}

function handleHttpResponse(http, ns, id, what) {
  if (http.readyState == 4) {
    if (http.status == 200) {
      var xmlParser = new DOMParser();
      var soapResponse = xmlParser.parseFromString(http.responseText, "application/xml");
      var validity;
      if (soapResponse.getElementsByTagNameNS) {
	validity = soapResponse.getElementsByTagNameNS(ns, "validity");
      } else {
	validity = soapResponse.getElementsByTagName("m:validity");
      }
      if (!validity || !validity[0]) {
	updateInfo("Error", id);
	return;
      }
      var isValid = validity[0].firstChild.nodeValue;
      if (isValid == "true" || isValid == 1) {
	updateInfo("Valid " + what, id);
      } else {
	updateInfo("Invalid " + what, id);
      }
    } else {
      updateInfo("Error while checking " + what + " validity!", id);
    }
  }
}

function updateInfo(value, id) {
  document.getElementById(id).innerHTML = value;
}

