var gRefer = parent.location.href;
function autotab(original,destination)
{
    if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
    destination.focus();
}

function getCheckedValue(radioObj) {
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
        for(var i = 0; i < radioLength; i++) {
            if(radioObj[i].checked) {
                return radioObj[i].value;
            }
        }
    return "";
}

function checkemail(){
    var testresults;
    var str=document.demo_contact_form.email.value;
    var filter=/^(['_a-z0-9-+]+)(\.['_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/i
    if (filter.test(str))
        testresults=true
    else
        testresults=false
    return (testresults)
}

function trim(s) {
    while (s.substring(0,1) == ' ') {
        s = s.substring(1,s.length);
    }
    while (s.substring(s.length-1,s.length) == ' ') {
        s = s.substring(0,s.length-1);
    }
    return s;
}

function isAlpha(s) {
    if(s == null)
        return false;
    if(s.length == 0)
        return false;
    for(var i=0; i<s.length; ++i)
    {
        if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".indexOf(s.charAt(i)) < 0)
        {
            return false;
        }
    }
    return true;
}

function isNumeric(s) {
    if(s == null)
        return false;
    if(s.length == 0)
        return false;
    for(var i=0; i<s.length; ++i)
    {
        if("1234567890".indexOf(s.charAt(i)) < 0)
        {
            return false;
        }
    }
    return true;
}

function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal (j);
            i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function ValidateForm() {
    //make sure that the last name field is not blank.
    if ((document.demo_contact_form.last_name.value.length == 0) || (isNumeric(document.demo_contact_form.last_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("氏名（姓）を入力してください。");
        document.demo_contact_form.last_name.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that the first name is not blank.
    if ((document.demo_contact_form.first_name.value.length == 0) || (isNumeric(document.demo_contact_form.first_name.value))){
        //it isn't so show the user an alert and go to that field
        alert("氏名（名）を入力してください。");
        document.demo_contact_form.first_name.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that the last name field is not blank.
    if ((document.demo_contact_form.secondary_last_name.value.length == 0) || (isNumeric(document.demo_contact_form.secondary_last_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("フリガナ（姓）を入力してください。");
        document.demo_contact_form.secondary_last_name.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that the first name is not blank.
    if ((document.demo_contact_form.secondary_first_name.value.length == 0) || (isNumeric(document.demo_contact_form.secondary_first_name.value)))    {
        //it isn't so show the user an alert and go to that field
        alert("フリガナ（名）を入力してください。");
        document.demo_contact_form.secondary_first_name.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //trim leading and trailing spaces to help users with invalid emails
    document.demo_contact_form.email.value = trim(document.demo_contact_form.email.value);
    //make sure that the email field is not blank.
    if (document.demo_contact_form.email.value.length == 0)
    {
        //it isn't so show the user an alert and go to that field
        alert("メールアドレスを入力してください。");
        document.demo_contact_form.email.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure the email address is formatted properly
    if (checkemail()==false)
    {
        alert("メールアドレスを正しく入力してください。");
        document.demo_contact_form.email.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure the email addresses match
    if(document.demo_contact_form.email.value != document.demo_contact_form.email2.value){
        alert("メールアドレスと同じ確認用メールアドレスを入力してください。");
        document.demo_contact_form.email2.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    if (document.demo_contact_form.area.value == "") {
        alert("件名を選んでください");
        document.demo_contact_form.area.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    if (document.demo_contact_form.regarding.value == "") {
        alert("お問い合わせ内容を記入してください");
        document.demo_contact_form.regarding.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    setwsEntityLow("JP");
    return true;
}