﻿// JScript File
var preCounty;
var http_request_C = false;
var http_request_S = false;

var http_request_extra = false;
var pmcontrol;

function getCounty(cValue, cCounty) {
    //var cValue = document.getElementById('state').value;
    preCounty = cCounty;
    pmcontrol = 'county';
    myRand = parseInt(Math.random() * 99999999); // cache buster
    var getstr = "?state=" + cValue + "&cb=" + myRand;
    //alert(getstr);
    //prompt('Here\'s the remote call:','/inc/fetchCounties.asp' + getstr);
    makeCountyRequest('/fetchCounties.asp', getstr);

}
function getCountySelect(cValue, cCounty) {
    //var cValue = document.getElementById('state').value;
    preCounty = cCounty;
    pmcontrol = 'countyselect';
    myRand = parseInt(Math.random() * 99999999); // cache buster
    var getstr = "?state=" + cValue + "&cb=" + myRand;
    //alert(getstr);
    //prompt('Here\'s the remote call:','/inc/fetchCounties.asp' + getstr);
    makeCountyRequest('/fetchCounties.asp', getstr);

}

function makeCountyRequest(url, parameters) {
    http_request_C = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request_C = new XMLHttpRequest();
        if (http_request_C.overrideMimeType) {
            http_request_C.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request_C = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request_C = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }

    if (!http_request_C) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
    http_request_C.onreadystatechange = alertCounty;
    http_request_C.open('GET', url + parameters, true);
    //alert(url + parameters);
    http_request_C.send(null);

}

function alertCounty() {
    if (http_request_C.readyState == 4) {
        if (http_request_C.status == 200) {
            //alert(http_request_C.responseText);
            result = http_request_C.responseText;
            var parts = result.split("|");
            //alert(preCounty);
            var currentValue = document.getElementById(pmcontrol).value;

            if (parts != null && parts != "") {
                changeList(document.getElementById(pmcontrol), parts, preCounty);
            } else {
                emptyList(document.getElementById(pmcontrol));
            }
        } else {
            result = 'There was a problem with the request.' + http_request_C.status + '';
            alert(result);
        }
    }
}


function getSlogans() {
    pmcontrol = 'domains';
    var val = document.getElementById('keywords').value;
    var ary = val.split('_');
    var cat = ary[0];
    var key = ary[1];
    var ex = document.getElementById('isexclusive').value;

    myRand = parseInt(Math.random() * 99999999); // cache buster
    var getstr = "?key=" + key + "&cat=" + cat + "&ex=" + ex + "&cb=" + myRand;
    //alert(getstr);
    //prompt('Here\'s the remote call:','/inc/fetchCounties.asp' + getstr);
    makeSloganRequest('/fetchslogans.asp', getstr);



}

function makeSloganRequest(url, parameters) {
    http_request_S = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request_S = new XMLHttpRequest();
        if (http_request_S.overrideMimeType) {
            http_request_S.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request_S = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request_S = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }

    if (!http_request_S) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }
    http_request_S.onreadystatechange = alertSlogan;
    http_request_S.open('GET', url + parameters, true);
    //alert(url + parameters);
    http_request_S.send(null);

}

function alertSlogan() {
    if (http_request_S.readyState == 4) {
        if (http_request_S.status == 200) {
            //alert(http_request_S.responseText);
            result = http_request_S.responseText;
            var parts = result.split("|");
            //alert(preCounty);
            var currentValue = document.getElementById(pmcontrol).value;

            if (parts != null && parts != "") {
                changeList(document.getElementById(pmcontrol), parts);
            } else {
                emptyList(document.getElementById(pmcontrol));
            }
        } else {
            result = 'There was a problem with the request.' + http_request_S.status + '';
            alert(result);
        }
    }
}

function emptyList(box) {
    // Set each option to null thus removing it
    //while ( box.options.length ) box.options[0] = null;
    box.options.length = 0;
}

// This function assigns new drop down options to the given
// drop down box from the list of lists specified

function fillList(box, arr, currentValue) {

    //option = new Option("","");
    //box.options[box.length] = option;

    var idx;
    var foundIt = false;

    for (i = 0; i < arr.length; i++) {

        var opts = arr[i].split("=");

        // Create a new drop down option with the
        // display text and value from arr
        //option = new Option( opts[1], opts[0] + '|' + currentValue );
        //alert('currentValue:' + currentValue + '\nopts[1]:' + opts[1] + '\nopts[0]:' + opts[0]);
        option = new Option(opts[1], opts[0]);
        idx = box.length;

        // Add to the end of the existing options
        box.options[idx] = option;

        if (currentValue == opts[0]) {
            //alert('found it');
            foundIt = true;
            box.selectedIndex = idx;
        }
    }

    // Preselect option 0
    if (box.length > 0 && !foundIt) {
        box.selectedIndex = 0;
    }

}

// This function performs a drop down list option change by first
// emptying the existing option list and then assigning a new set

function changeList(box, list, currentValue) {

    // Next empty the slave list
    emptyList(box);

    // Then assign the new list values
    //alert(currentValue);
    fillList(box, list, currentValue);
}

function valSel() {
    var st = document.getElementById("state").value;
    var count = document.getElementById("county").value;

    if (st == 0 || count == 0) {
        alert('You need to select a state and county to continue');
        return false;
    } else { return true; }
}

function doSubmit() {
    var s = document.getElementById("state").value;
    var c = document.getElementById("county").value;

    alert('You have selected ' + c + ' in the state of ' + s + '.\nThank You.');
}
