
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_account_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 numbersonly(e){
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
if (unicode<48||unicode>57) //if not a number
return false //disable key press
}
}



function ValidateForm() {
    //get all the radio buttons named acctype
    var radio = document.getElementsByName("fx_sub_company");
    //make sure that one of the buttons is checked
    var rChecked = false;
    for(var i=0; i<radio.length; ++i){
        if (radio[i].checked) {
            rChecked = true;
        }
    }
    if (rChecked == false) {
        //no readio buttons were checked so alert the user
        alert("口座タイプを選んでください");
        document.demo_account_form.fx_sub_company1.focus();
        return false;
    }
    //make sure that the last name field is not blank.
    if ((document.demo_account_form.last_name.value.length == 0) || (isNumeric(document.demo_account_form.last_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("氏名（姓）を入力してください。");
        document.demo_account_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_account_form.first_name.value.length == 0) || (isNumeric(document.demo_account_form.first_name.value))){
        //it isn't so show the user an alert and go to that field
        alert("氏名（名）を入力してください。");
        document.demo_account_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_account_form.secondary_last_name.value.length == 0) || (isNumeric(document.demo_account_form.secondary_last_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("フリガナ（姓）を入力してください。");
        document.demo_account_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_account_form.secondary_first_name.value.length == 0) || (isNumeric(document.demo_account_form.secondary_first_name.value))) {
        //it isn't so show the user an alert and go to that field
        alert("フリガナ（名）を入力してください。");
        document.demo_account_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_account_form.email.value = trim(document.demo_account_form.email.value);
    //make sure that the email field is not blank.
    if (document.demo_account_form.email.value.length == 0)
    {
        //it isn't so show the user an alert and go to that field
        alert("メールアドレスを入力してください。");
        document.demo_account_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_account_form.email.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    //make sure the email addresses match
    if (document.demo_account_form.email.value != document.demo_account_form.email2.value)
    {
        alert("メールアドレスと同じ確認用メールアドレスを入力してください。");
        document.demo_account_form.email2.focus();
        //and return false so the form doesn't get submitted
        return false;
    }
    
    //make sure the contact button is checked
    var radio2 = document.getElementsByName("advise");
    //make sure that one of the buttons is checked
    var r2Checked = false;
    for(var i=0; i<radio2.length; ++i){
        if (radio2[i].checked) {
            r2Checked = true;
        }
    }
    if (r2Checked == false) {
        //no radio buttons were checked so alert the user
        alert("弊社からのサポートを希望しますか？");
        document.demo_account_form.advise1.focus();
        return false;
    }
    
    //make sure the identify company button is checked
    if (document.demo_account_form.advise[0].checked)
    {
    	var radio3 = document.getElementsByName("companyname");
      //make sure that one of the buttons is checked
      var r3Checked = false;
      for(var i=0; i<radio3.length; ++i){
          if (radio3[i].checked) {
              r3Checked = true;
          }
      }
      if (r3Checked == false) {
          //no radio buttons were checked so alert the user
          alert("ご連絡する場合に弊社の社名を名乗っても良いですか？");
          document.demo_account_form.companyname1.focus();
          return false;
      }
    }    	
    
    //make sure the phone number fields are filled out
    if ((document.demo_account_form.phonearea.value.length < 2) || (document.demo_account_form.phoneprefix.value.length < 1) || (document.demo_account_form.phonesuffix.value.length < 3))
    {
    	//one of the boxes is short, so send an alert
    	alert("電話番号を入力してください。");
    	
    	//now focus on the first bad phone number field
    	if (document.demo_account_form.phonearea.value.length < 2)
    	{
    		document.demo_account_form.phonearea.focus();
    	}
    	else if (document.demo_account_form.phoneprefix.value.length < 1)
    		{
    			document.demo_account_form.phoneprefix.focus();
    		}
    	else if (document.demo_account_form.phonesuffix.value.length < 3)
    		{
    			document.demo_account_form.phonesuffix.focus();
    		}    		
    	    	
    	//and return false so the form doesn't get submitted
    	return false;
    }
    
    
    if (document.getElementById("hear_about1") != null) {
        //make sure the user has picked a referrer
        if (document.demo_account_form.hear_about1 != null) {
            if ((document.demo_account_form.hear_about1.selectedIndex == 0) || (document.demo_account_form.hear_about1.selectedIndex == 1)) {
                //if statement failed so ask the user to pick one!
                alert("当社を知ったきっかけを選んでください。");
                document.demo_account_form.hear_about1.focus();
                //and return false so the form doesn't get submitted
                return false;
            }
            if ((document.demo_account_form.hear_about1.selectedIndex > 1) && (document.demo_account_form.hear_about1.value != 'Forex.com-Word of Mouth')) {
                if (document.getElementById("hear_about_alt") == null) {
                    checkHear();
                }
                if (document.demo_account_form.hear_about_alt.value == null) {
                    alert("どこで当社を知ったかを選んでください。");
                    document.demo_account_form.hear_about_alt.focus();
                    return false;
                }
                else if (document.demo_account_form.hear_about_alt.selectedIndex < 2) {
                    alert("どこで当社を知ったかを選んでください。");
                    document.demo_account_form.hear_about_alt.focus();
                    return false;
                }
                if (document.demo_account_form.hear_about_alt.value.length == 0) {
                    alert("どこで当社を知ったかを選んでください。");
                    document.demo_account_form.hear_about_alt.focus();
                    return false;
                }
            }
        }
        if (document.demo_account_form.hear_about1.value == "Forex.com-Other") {
            if (document.demo_account_form.hear_about_alt.value == "") {
                alert("どこで当社を知ったかを選んでください。");
                document.demo_account_form.hear_about_alt_other.focus();
                return false;
            }
            else {
                var otherVal = "DropDown.Other." + document.demo_account_form.hear_about_alt.value;
                document.demo_account_form.hear_about_alt.value = otherVal;
            }
        }
    }
    setwsEntityHigh("JP");
    return true;
}