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_brochure_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_brochure_form.last_name.value.length == 0) || (isNumeric(document.demo_brochure_form.last_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("氏名（姓）を入力してください。");
        document.demo_brochure_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_brochure_form.first_name.value.length == 0) || (isNumeric(document.demo_brochure_form.first_name.value))){
        //it isn't so show the user an alert and go to that field
        alert("氏名（名）を入力してください。");
        document.demo_brochure_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_brochure_form.secondary_last_name.value.length == 0) || (isNumeric(document.demo_brochure_form.secondary_last_name.value)))    {
        //it isn't so show the user an alert and go to that field
        alert("フリガナ（姓）を入力してください。");
        document.demo_brochure_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_brochure_form.secondary_first_name.value.length == 0) || (isNumeric(document.demo_brochure_form.secondary_first_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("フリガナ（名）を入力してください。");
        document.demo_brochure_form.secondary_first_name.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that a gender is selected
    if (document.getElementById("gender_male").checked == false && document.getElementById("gender_female").checked == false) {
        alert("性別を選んでください");
        document.demo_brochure_form.gender_male.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that the phone number is numeric
    if (!isNumeric(document.demo_brochure_form.phonearea.value)||document.demo_brochure_form.phonearea.value.length <= 1) {
        alert("性別を選んでください");
        document.demo_brochure_form.phonearea.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that the phone prefix is numeric
    if (!isNumeric(document.demo_brochure_form.phoneprefix.value) || document.demo_brochure_form.phoneprefix.value.length < 3) {
        alert("電話番号を入力してください");
        document.demo_brochure_form.phoneprefix.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that the phone suffix is numeric
    if (!isNumeric(document.demo_brochure_form.phonesuffix.value) || document.demo_brochure_form.phonesuffix.value.length < 3) {
        alert("電話番号を入力してください");
        document.demo_brochure_form.phonesuffix.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_brochure_form.email.value = trim(document.demo_brochure_form.email.value);
    //make sure that the email field is not blank.
    if (document.demo_brochure_form.email.value.length == 0)
    {
        //it isn't so show the user an alert and go to that field
        alert("メールアドレスを入力してください。");
        document.demo_brochure_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_brochure_form.email.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure the email addresses match
    if(document.demo_brochure_form.email.value != document.demo_brochure_form.email2.value){
        alert("メールアドレスと同じ確認用メールアドレスを入力してください。");
        document.demo_brochure_form.email2.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that a zip code has been entered
    if (!isAlpha(document.getElementById("zip1").value)) {
        alert("郵便番号を記入してください");
        document.demo_brochure_form.zip1.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure that a zip code has been entered
    if (!isAlpha(document.getElementById("zip2").value)) {
        alert("郵便番号を記入してください");
        document.demo_brochure_form.zip2.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    document.getElementById("zip").value = document.getElementById("zip1").value + "-" + document.getElementById("zip2").value;
    //make sure the state has been selected
    if (document.getElementById("state").value == ""){
        alert("郵便番号を記入してください");
        document.demo_brochure_form.state.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure the address has been entered
    if ((document.getElementById("address_1").value.length == 0) || (isNumeric(document.getElementById("address_1").value))) {
        alert("アドレスを入力してください。");
        document.demo_brochure_form.address_1.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    if (!document.getElementById("forex_experience_yes").checked && !document.getElementById("forex_experience_no").checked) {
        //if statement failed so ask the user to pick one!
        alert("経験を選んでください");
        document.demo_brochure_form.forex_experience_yes.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    setwsEntityLow("JP");
    return true;
}