// MyFujinon Script

var objURL = new Object();
window.location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) {
    objURL[$1] = $3;
}
);

function maskPhone(f) {
    tel = '';
    var val = f.value.split('');
    for (var i = 0; i < val.length; i++) {
        if (val[i] == '(') {
            val[i] = ''
        }
        if (val[i] == ')') {
            val[i] = ''
        }
        if (val[i] == '-') {
            val[i] = ''
        }
        if (val[i] == '') {
            val[i] = ''
        }
        if (val[i] == '.') {
            val[i] = ''
        }
    }
    //
    for (var i = 0; i < val.length; i++) {
        if (i == 2) { val[i] = val[i] + '-' }
        if (i == 6) { val[i] = val[i] + '-' }
        //if (i == 10) { val[i] = val[i] + '-' }
        tel = tel + val[i]
    }
    f.value = tel;
}

function getOrg(orgstring) {
    ajaxReq("<get><organization><![CDATA[" + orgstring + "]]></organization></get>");
}

function specialWarn() {
    var agree = confirm("This item can take up to 4-6 weeks for delivery. Click OK if you would like to continue ordering this item.");
    if (agree) {
        return true;
    } else {
        return false;
    }
}

function confirmSurvey(url) {
    var agree = confirm("Thank you for your order. Please help us improve the MyFujinon customer experience by completing the following brief survey. Click OK if you would like to take the survey.");
    if (agree) {
        document.getElementById('surveyFrame').src = url;
        //window.open(url);
    }
}

function setSelection(menuControl, value) {
    for (var i = 0; i < menuControl.options.length; i++) {
        if (menuControl.options[i].value == value) {
            menuControl.selectedIndex = i;
        }
    }
}

function completeOrder() {
    if (document.getElementById('tandc_agree').checked) {
        return true;
    } else {
        alert("You must agree to the terms and conditions before completing your order.");
        return false;        
    }
}

var orgText = "";
function findOrg() {
    var orgField = document.getElementById('meta_companyvalidate');
    if (orgField.value.length > 2) {
        ajaxReq("<get><findorg><![CDATA[" + orgField.value + "]]></findorg></get>");
    }
}

function getContracts() {
    ajaxReq("<get><contracts/></get>");    
}

function withoutValidateCompany() {
    setValidated()
}

function validateCompany(companyName) {
    var orgField = document.getElementById('meta_companyvalidate');
    orgField.value = companyName;
    getOrg(companyName);
}

function setValidated() {
    document.getElementById('validatebutton').style.display = 'none';
    document.getElementById('step2').style.display = '';
    document.getElementById('company').value = document.getElementById('meta_companyvalidate').value;
    //document.getElementById('meta_companyvalidate').disabled = true;
}

function setCompanyValue() {
    document.getElementById('company').value = document.getElementById('meta_companyvalidate').value;
    return true;
}

function unValidate() {
    document.getElementById('validatebutton').style.display = '';
    document.getElementById('step2').style.display = 'none';
    //document.getElementById('company').value = document.getElementById('meta_companyvalidate').value;
    //document.getElementById('meta_companyvalidate').disabled = false;
    document.getElementById('validmessage').innerHTML = "Enter at least the first three letters of your company name above, then click Find Organization.";
    document.getElementById('meta_companyvalidate').value = "";
}

function ajaxReq(xmlIn) {
    var objHTTP, strResult;
    
    var xmlUrl = 'default.aspx?pageid=446'; // set from server otherwise have to manually edit when publishing.
    
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
    {
        objHTTP = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) // if IE
    {
        try {
            objHTTP = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                objHTTP = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert('Could not initiate request.');
            }
        }
    }

    objHTTP.open("POST", xmlUrl + "&rnd=" + Math.random() * 4, true);
    objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


    objHTTP.onreadystatechange = function() {
        if (objHTTP.readyState == 4) {

            ajaxResponse(objHTTP);
        }
    };


    objHTTP.send("<?xml version='1.0' encoding='UTF-8'?>" + xmlIn);
}

function ajaxResponse(ajaxObject) {   

    var returnList = ajaxObject.responseXML.getElementsByTagName('return')[0].childNodes;

    for (var x = 0; x < returnList.length; x++) {
        var returnItem = returnList[x];

        if (returnItem.nodeName == 'organization') {
            var setNode = returnItem.getElementsByTagName('NewDataSet')[0];
            var foundOrg = false;

            if (setNode != null) {

                var orgNode = returnItem.getElementsByTagName('MF_ORGANIZATIONS')[0];

                if (orgNode != null) {
                    
                    var orgBillAddress1 = "";
                    var orgBillAddress2 = "";
                    var orgBillAddress3 = "";
                    var orgBillCity = "";
                    var orgBillState = "";
                    var orgBillZip = "";

                    if (orgNode.getElementsByTagName('bill_address_1')[0].hasChildNodes()) {
                        orgBillAddress1 = orgNode.getElementsByTagName('bill_address_1')[0].firstChild.nodeValue;
                    }
                    if (orgNode.getElementsByTagName('bill_address_2')[0].hasChildNodes()) {
                        orgBillAddress2 = orgNode.getElementsByTagName('bill_address_2')[0].firstChild.nodeValue;
                    }
                    if (orgNode.getElementsByTagName('bill_address_3')[0].hasChildNodes()) {
                        orgBillAddress3 = orgNode.getElementsByTagName('bill_address_3')[0].firstChild.nodeValue;
                    }
                    if (orgNode.getElementsByTagName('bill_city')[0].hasChildNodes()) {
                        orgBillCity = orgNode.getElementsByTagName('bill_city')[0].firstChild.nodeValue;
                    }
                    if (orgNode.getElementsByTagName('bill_state')[0].hasChildNodes()) {
                        orgBillState = orgNode.getElementsByTagName('bill_state')[0].firstChild.nodeValue;
                    }
                    if (orgNode.getElementsByTagName('bill_zip')[0].hasChildNodes()) {
                        orgBillZip = orgNode.getElementsByTagName('bill_zip')[0].firstChild.nodeValue;
                    }

                    document.getElementById('address_1').value = orgBillAddress1;
                    document.getElementById('address_2').value = orgBillAddress2;
                    document.getElementById('address_3').value = orgBillAddress3;
                    document.getElementById('city').value = orgBillCity;
                    document.getElementById('state').value = orgBillState;
                    document.getElementById('postal').value = orgBillZip;

                    foundOrg = true;

                    
                }
            }

            if (!foundOrg) {
                document.getElementById('validmessage').innerHTML = "<b style=\"color:red;\">We could not locate a company by that name. Please try again or you may continue without validation. </br>Please allow more time for registration review.</b>";
                document.getElementById('meta_withoutvalidate').style.display = '';
            } else {
                document.getElementById('validmessage').innerHTML = "<span>We found your company in our records. <strong style=\"color:red;\">Please confirm the billing information below then click Next.</strong></span>";
                setValidated();
                document.getElementById('orglist_container').style.visibility = 'hidden';
            }

        }

        if (returnItem.nodeName == 'contracts') {
            
            var contractList = returnItem.childNodes;

            for (var c = 0; c < contractList.length; c++) {
                if (contractList[c].nodeName == "contract") {
                    var contractPulldown = document.getElementById("meta_contract");
                    contractPulldown.options[contractPulldown.length] = new Option(contractList[c].firstChild.nodeValue, contractList[c].getAttribute("guid"));
                    //alert(contractList[c].getAttribute("guid") + " " + contractList[c].firstChild.nodeValue);
                }
            }

        }

        if (returnItem.nodeName == 'organizations') {
            var foundOrgs = false;
            document.getElementById('orgresults').innerHTML = "";
            if (returnItem.hasChildNodes()) {
                for (var i = 0; i < returnItem.childNodes.length; i++) {
                    var orgEl = document.createElement("a");
                    orgEl.className = "org_item";
                    if (returnItem.childNodes[i].nodeName == "organization") {
                        orgEl.setAttribute("href", "javascript:validateCompany('" + returnItem.childNodes[i].firstChild.nodeValue + "');");
                        orgEl.innerHTML = returnItem.childNodes[i].firstChild.nodeValue;
                        document.getElementById('orgresults').appendChild(orgEl);
                        foundOrgs = true;
                    }
                }
            }
            if (foundOrgs) {
                document.getElementById('orglist_container').style.visibility = 'visible';
            } else {
                if (confirm('Sorry, we could not find a matching organization in our records. Would you like to continue? Click OK to register as a new organization or click Cancel to search again.')) {
                    document.getElementById('validmessage').innerHTML = "";
                    setValidated();
                    document.getElementById('orglist_container').style.visibility = 'hidden';
                } else {
                
                }            
            }
        }     
        
    }

    //setValidated();


}