var xmlhttpSmartphone;

function getSmartphoneBrandList() {
    xmlhttpSmartphone = getXmlHttpRequestObject();
    var smartphoneOsId = document.getElementById('smartphoneOsId').value;
    if(smartphoneOsId == "0") {
        clearSmartphoneBrandList();
        return;
    }
    
    var url = 'getSmartphoneBrandList';
    if(xmlhttpSmartphone != null) {
        xmlhttpSmartphone.onreadystatechange = changeSmartphoneBrandList;
        xmlhttpSmartphone.open("POST", url, true);
        xmlhttpSmartphone.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttpSmartphone.send('id=' + smartphoneOsId);
    } else {
        alert(error_no_ajax_support);
    }
}

function checkPersonalInfoAgreement() {
    acceptTermsAndCondCtrl = document.getElementById("acceptTermsAndCond");
    if(!acceptTermsAndCondCtrl.checked) {
        alert(must_accept_to_terms_and_cond);
    } else {
        document.doRegisterMobileAccountStepTwo.submit();
    }
}

function clearSmartphoneBrandList(hasItems) {
    var smartphoneBrandListCtrl = document.getElementById("smartphoneBrandList");
    smartphoneBrandListCtrl.options.length = 0;
    
    if(!hasItems) {
        var option = document.createElement('option');
        option.value = 0;
        option.appendChild(document.createTextNode(select_smartphone_os_item));
        smartphoneBrandListCtrl.appendChild(option);
    }
}

function changeSmartphoneBrandList() {
    if (xmlhttpSmartphone.readyState == 4 && xmlhttpSmartphone.status == 200) {
        if(xmlhttpSmartphone.responseText == "0") {
            clearSmartphoneBrandList(true);
            return;
        }
        var smartphoneBrandList = xmlhttpSmartphone.responseText.split('\n');
        if(smartphoneBrandList.length < 2) {
            clearSmartphoneBrandList(false);
        } else {
            clearSmartphoneBrandList(true);
        }

        var smartphoneBrandListCtrl = document.getElementById("smartphoneBrandList");

        for(var i=0; i< smartphoneBrandList.length; i++) {
            var option = document.createElement('option');
            var keyValueArray = smartphoneBrandList[i].split("|");
            if(keyValueArray.length != 2) {
                continue;
            }
            option.appendChild(document.createTextNode(keyValueArray[1]));
            option.value = keyValueArray[0];
            smartphoneBrandListCtrl.appendChild(option);
        }
    } else {
        clearSmartphoneBrandList(false);
    }
}
