function openWindow(loc)
{
  window.open(loc, 'window',
	     'toolbar=yes,location=yes,directories=yes,status=yes,dependent=yes,' +
	     'menubar=yes,scrollbars=yes,resizable=yes,width=600,height=400') ;
  return false ;
}

function openWindowL(loc)
{
  window.open(loc, 'window',
	     'toolbar=yes,location=yes,directories=yes,status=yes,dependent=yes,' +
	     'menubar=yes,scrollbars=yes,resizable=yes,width=960,height=640') ;
  return false ;
}

function hi(obj) { obj.style.backgroundColor = '#f0f0f0'; }
function hs(obj) { obj.style.backgroundColor = '#ffffff'; }

var msgdiv = null ;
var triangleOn = null ;
var triangleOff = null ;

function hilight(obj, src, msg)
{
  if( triangleOn != null ) {
    triangleOn.src = triangleOff ;
  }
  if( msg != null ) {
    triangleOn = obj ;
    triangleOff = obj.src ;
  } else {
    triangleOn = null ;
    triangleOff = null ;
  }
  obj.src = src ;


    if(msgdiv != null) {
      msgdiv.style.display = 'none' ;
    }
    if(msg != null) {
    var div = document.getElementById(msg) ;
    if(div != null) {
      div.style.display = '' ;
      msgdiv = div ;
    }
  }
}

function hilightOff() {
  if(triangleOn != null && triangleOff != null) {
    triangleOn.src = triangleOff ;
  }
}

function overviewOn(obj) {hilight(obj, '/WSR/overview2.gif', "overview2") ;}
function specOn(obj) {hilight(obj, '/WSR/spec2.gif', "spec2") ;}
function configurationOn(obj) {hilight(obj, '/WSR/configuration2.gif', "configuration2") ;}
function eventSeminarOn(obj) {hilight(obj, '/WSR/eventSeminar2.gif', "eventSeminar2") ;}
function casestudyOn(obj) {hilight(obj, '/WSR/casestudy2.gif', "casestudy2") ;}
function publicationsOn(obj) {hilight(obj, '/WSR/publications2.gif', "publications2") ;}
function partnerOn(obj) {hilight(obj, '/WSR/partner2.gif', "partner2") ;}
function supportOn(obj) {hilight(obj, '/WSR/support2.gif', "support2") ;}
function aboutusOn(obj) {hilight(obj, '/WSR/aboutus2.gif', "aboutus2") ;}

function mousedown()
{
  if(triangleOn != null) {
    triangleOn.src = triangleOff ;
  }
}

function topOnLoad()
{
  var ix = document.getElementById("overview1Img") ;
  if(ix != null) {
    overviewOn(ix) ;
  }
}

function createXHTTPRequest()
{
  // although IE supports the XMLHttpRequest object, but it does not work on local files.
  var forceActiveX = (window.ActiveXObject && location.protocol === "file:") ;
  if (window.XMLHttpRequest && ! forceActiveX) {
    return new XMLHttpRequest();
  } else {
    try {
      return new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ;
    }
  }
}


function getMessageBody(form)
{
  var data = "" ;
  var cnt = form.elements.length ;
  for (var i = 0 ; i < cnt ; i++) {
    var elem = form.elements[i];
    if (elem.name) {
      var nodeName = elem.nodeName.toLowerCase ();
      var type = elem.type ? elem.type.toLowerCase () : "";

      // if an input:checked or input:radio is not checked, skip it
      if (nodeName === "input" && (type === "checkbox" || type === "radio")) {
        if (!elem.checked) {
          continue;
        }
      }
      var param = "";
      // select element is special, if no value is specified the text must be sent
      if (nodeName === "select") {
        for (var j = 0 ; j < elem.options.length ; j++) {
          var option = elem.options[j] ;
          if (option.selected) {
            var valueAttr = option.getAttributeNode("value");
            var value = (valueAttr && valueAttr.specified) ? option.value : option.text;
            if (param != "") {
              param += "&";
            }
            param += encodeURIComponent (elem.name) + "=" + encodeURIComponent (value);
          }
        }
      } else {
        param = encodeURIComponent (elem.name) + "=" + encodeURIComponent (elem.value);
      }

      if(data != "") {
        data += "&";
      }
      data += param;                  
    }
  }
  return data;
}


function sendRequest(formName, mailServiceURL)
{
  var httpreq = createXHTTPRequest() ;
  if(httpreq == null) {
    return -1 ;
  }
  var form = document.getElementById(formName) ;
  if(form == null) {;
    return -2 ;
  }
  var formData = getMessageBody(form) ;
  try {
    httpreq.open("post", mailServiceURL, false);   // synchronous
    httpreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpreq.send (formData);
    if(httpreq.status != 200) {
      // confirm(httpreq.status) ;
      return -4 ;
    }
  } catch (e) {
    // alert ("Cannot connect to the server! " + e);
    return -3 ;
  }
  return 0 ;
}

function trim(s)
{
  if(s == null) {
    return "" ;
  }
  return s.replace(/^\s+|\s+$/g, "");
}

function toHalfWidth(str)
{
  if(str == null) {
    return "" ;
  }
  var result = "" ;
  var len = str.length ;
  for (i = 0 ; i < len ; i++) {
    var code = str.charCodeAt(i);
    if ((0xff0f < code) && (code < 0xff1a)) {
      result += String.fromCharCode(code - 0xfee0) ;
    } else if ((0xff20 < code) && (code < 0xff3b)) {
      result += String.fromCharCode(code - 0xfee0) ;
    } else if ((0xff40 < code) && (code < 0xff5b)) {
      result += String.fromCharCode(code - 0xfee0) ;
    } else {
      result += String.fromCharCode(code);
    }
  }
  // confirm(result) ;
  return result ;
}

function startsWith(str, prefix)
{
  return (str.lastIndexOf(prefix, 0) == 0) ;
}


function numbersOnly(obj)
{
  // confirm(obj) ;
  var str = obj.value ;
  if(str == null) {
    return ;
  }
  str = toHalfWidth(str) ;
  obj.value = str.replace(/[^0-9]/g, "") ;
}

function displayErrorMessage(fieldName, msg)
{
  var errmsg = document.getElementById(fieldName + "_error") ;
  if(errmsg != null) {
    errmsg.innerHTML = msg ;
    errmsg.style.display = "block" ;
  }
}

function eraseErrorMessage(fieldName)
{
  var errmsg = document.getElementById(fieldName + "_error") ;
  if(errmsg != null) {
    errmsg.innerHTML = "" ;
    errmsg.style.display = "none" ;
  }
}

function checkNotEmpty(fieldName)
{
  return checkNotEmpty2(fieldName, "ご記入ください") ;
}

function checkNotEmpty2(fieldName, msg)
{
  eraseErrorMessage(fieldName) ;
  var obj = document.getElementById(fieldName) ;
  if(obj == null) {
    return 1 ;
  }
  var str = trim(obj.value) ;
  if(str != null && str.length > 0) {
    obj.value = toHalfWidth(str) ;
    return 0 ;
  }
  displayErrorMessage(fieldName, msg) ;
  return 1 ;
}

function checkMailAddress(fieldName)
{
  var err = checkNotEmpty(fieldName) ;
  if(err != 0) {
    return err ;
  }
  var str = document.getElementById(fieldName).value ;
  // confirm(str) ;
  if(! str.match(/^[A-Za-z0-9]+[\w\Q.!#$%&'*+-/=?^_`{|}~\E]+@[\w\.-]+\.\w{2,}$/)){
    displayErrorMessage(fieldName, "記入内容をお確かめください") ;
    return 1 ;
  }
  return 0 ;
}

function checkPhoneNumber(fieldName)
{
  var err = checkNotEmpty(fieldName) ;
  if(err != 0) {
    return err ;
  }
  var str = document.getElementById(fieldName).value ;
  var nums = str = str.replace(/[^0-9]/g, "") ;
  if(startsWith(str, "070") || startsWith(str, "080") || startsWith(str, "090")) {
    if(str.length != 11) {
      displayErrorMessage(fieldName, "記入内容をお確かめください") ;
      return 1 ;
    }
  } else {
    if(str.length != 10) {
      displayErrorMessage(fieldName, "記入内容をお確かめください") ;
      return 1 ;
    }
  }
  return 0 ;
}

function checkZipCode(zip1, zip2)
{
  // confirm(zip1 + "  " + zip2) ;
  var err = checkNotEmpty(zip1) ;
  if(err != 0) {
    return err ;
  }
  err = checkNotEmpty(zip2) ;
  if(err != 0) {
    return err ;
  }
  var str = document.getElementById(zip1).value ;
  if(str.length != 3) {
    displayErrorMessage(zip1, "記入内容をお確かめください") ;
    return 1 ;
  }
  str = document.getElementById(zip2).value ;
  if(str.length != 4) {
    displayErrorMessage(zip2, "記入内容をお確かめください") ;
    return 1 ;
  }
  return 0 ;
}


function changeFormMode(fieldName, x, br)
{
  var form = document.getElementById(fieldName) ;
  var conf = document.getElementById(fieldName + "_confirm") ;
  if(form != null && conf != null) {
    if(x) {
      conf.style.display = "inline" ;
      form.style.display = "none" ;
      var val = form.value ;
      if(br && val != null) {
        val = val.replace(/\r\n/g, "<br />");
        val = val.replace(/\r|\n/g, "<br />");
      }
      conf.innerHTML = val ;
    } else {
      conf.style.display = "none" ;
      form.style.display = "inline" ;
    }
  }
}


function replaceContents(formMode, confMode, toConfirm)
{
  var form = document.getElementById(formMode) ;
  var conf = document.getElementById(confMode) ;
  if(form != null && conf != null) {
    if(toConfirm) {
      conf.style.display = "block" ;
      form.style.display = "none" ;
    } else {
      conf.style.display = "none" ;
      form.style.display = "block" ;
    }
  }
}


function showHideCheckbox(field, hide)
{
  var checkbox = document.getElementById(field) ;
  var checklabel = document.getElementById(field + "_label") ;
  var checkconfirm = document.getElementById(field + "_confirm") ;
  if(checkbox != null && checklabel != null && checkconfirm != null) {
    if(! hide) {
      checkbox.style.display = "inline" ;
      checklabel.style.display = "inline" ;
      checkconfirm.style.display = "none" ;
    } else {
      checkbox.style.display = "none" ;
      checklabel.style.display = "none" ;
      if(checkbox.checked == true) {
        checkconfirm.innerHTML = checklabel.innerHTML ;
        checkconfirm.style.display = "inline" ;
      } else {
        checkconfirm.style.display = "none" ;
      }
    }
  }
}


function showHideAsterisk(x)
{
  var el = document.getElementsByTagName("span");
  if(el == null) {
    return ;
  }
  for (var i = 0 ; i < el.length ; i++) {
    if(el[i].className == "required") {
      el[i].style.display = x ;
    }
  }
}




