//var readPage = "";
//var host = "http://192.168.2.2:8988";
//var requestHeader = "/account/jsp/WAppServerPage.jsp?TransID=";
//var selectLocation = "RLOCLIST";


var drag = false;
var selectTableSize = 10;
var scrollCount = 0;
var dragY = 0;
var scrollBeforeObj = null;
var scrollAfterObj = null;
var scrollBeforeHeight = 0;
var scrollAfterHeight = 0;

var lastScrollDelta = 0;
var lastScrollOffset = 0;
var scrollUnit = 0;
var maxScrollHeight = 0;

function valueChanged(label) {

    if (label == 'Country') {
        getLocationList(3, document.getElementById('Country').value, '', '', '');
    }
    else if (label == 'State') {
        getLocationList(5, document.getElementById('Country').value, '', document.getElementById('State').value, ''); 
    }
}

function toggleVisibility(xPos, yPos, obj) {

    label = obj.id;

    setPosition(label + 'Select', label , xPos, yPos); 
    if (document.getElementById(label + 'Select').style.visibility == 'visible') {
        document.getElementById(label + 'Select').style.visibility = 'hidden';
    }
    else {
        document.getElementById(label + 'Select').style.visibility = 'visible';
        if (document.getElementById(label + '.Search')) {
            document.getElementById(label + '.Search').focus();
        }
    }

    scrollCount = 0;
    lastScrollDelta = 0;
    lastScrollOffset = 0;
}

function scrollSelect(e) {

    if (drag) { 
        
        label = (scrollBeforeObj.id).substring(0, (scrollBeforeObj.id).lastIndexOf("."));

        if (((parseInt(scrollBeforeHeight, 10) + (e.clientY - dragY)) <= maxScrollHeight) && ((parseInt(scrollBeforeHeight, 10) + (e.clientY - dragY)) >= 0))  {
            scrollBeforeObj.style.height=(parseInt(scrollBeforeHeight, 10) + (e.clientY - dragY)) + 'px'; 
            var step = parseInt((((e.clientY - dragY) - lastScrollOffset + lastScrollDelta)/scrollUnit), 10);
            lastScrollDelta = (((e.clientY - dragY) - lastScrollOffset + lastScrollDelta)%scrollUnit)
            lastScrollOffset = (e.clientY - dragY);
            if (step > 0) {
                scrollDown(label, step, true);
            }
            else if (step < 0) {
                scrollUp(label, step * -1, true);
            }
            
        }
        if (((parseInt(scrollAfterHeight, 10) - (e.clientY - dragY)) >= 0) && ((parseInt(scrollAfterHeight, 10) - (e.clientY - dragY)) <= maxScrollHeight)) {
            scrollAfterObj.style.height=(parseInt(scrollAfterHeight, 10) - (e.clientY - dragY)) + 'px';
        }
        //lastScrollDelta + ((e.clientY - dragY) - lastScrollDelta)
    }

}

function scrollDown(label, step, noscroll) {

    //var count = 0;

    if (!noscroll) noscroll = false;

    scrollAfterObj = document.getElementById(label + '.ScrollAfter'); 
    scrollBeforeObj = document.getElementById(label + '.ScrollBefore'); 

    //while (true) {
    for (var count = 0; count < step; count++) {
        if ((document.getElementById(label + '.Row' + scrollCount)) && (document.getElementById(label + '.Row' + (scrollCount + selectTableSize)))) {
            //if ((document.getElementById(label + '.Row' + scrollCount).style.display == '') && (document.getElementById(label + '.Row' + (scrollCount + selectTableSize)).style.display == 'none')) {
                document.getElementById(label + '.Row' + scrollCount).style.display = 'none';
                document.getElementById(label + '.Row' + (scrollCount + selectTableSize)).style.display = '';
                
                //alert(parseInt(scrollBeforeObj.style.height, 10) + scrollUnit);
                
                if (!noscroll) {
                    if ((parseInt(scrollBeforeObj.style.height, 10) + scrollUnit) > maxScrollHeight) {
                        scrollBeforeObj.style.height = maxScrollHeight + 'px';
                    }
                    else {
                        scrollBeforeObj.style.height = (parseInt(scrollBeforeObj.style.height, 10) + scrollUnit) + 'px';
                    }
                    
                    if ((parseInt(scrollAfterObj.style.height, 10) - scrollUnit) < 0) {
                        scrollAfterObj.style.height = 0 + 'px';
                    }
                    else {
                        scrollAfterObj.style.height = (parseInt(scrollAfterObj.style.height, 10) - scrollUnit) + 'px';
                    }
                }               
                scrollCount++;
                //break;
            //}
        }
        else {
            break;
        }
        //scrollCount++;
    }

    //alert(parseFloat(scrollAfterObj.style.height));
    //alert (parseFloat(scrollBeforeObj.style.height));
}

function scrollUp(label, step, noscroll) {

    if (!noscroll) noscroll = false;

    //var count = document.getElementById(label + '.Table').rows.length - 1;
    scrollAfterObj = document.getElementById(label + '.ScrollAfter'); 
    scrollBeforeObj = document.getElementById(label + '.ScrollBefore'); 

    //while (true) {
    for (var count = 0; count < step; count++) {
        scrollCount--;
        if ((document.getElementById(label + '.Row' + scrollCount)) && (document.getElementById(label + '.Row' + (scrollCount + selectTableSize)))) {
            //if ((document.getElementById(label + '.Row' + scrollCount).style.display == 'none') && (document.getElementById(label + '.Row' + (scrollCount - selectTableSize)).style.display == '')) {
                document.getElementById(label + '.Row' + scrollCount).style.display = '';
                document.getElementById(label + '.Row' + (scrollCount + selectTableSize)).style.display = 'none';

                if (!noscroll) {
                    if ((parseInt(scrollAfterObj.style.height, 10) + scrollUnit) > maxScrollHeight) {
                        scrollAfterObj.style.height = maxScrollHeight + 'px';
                    }
                    else {
                        scrollAfterObj.style.height = (parseInt(scrollAfterObj.style.height, 10) + scrollUnit) + 'px';
                    }
                    
                    if ((parseInt(scrollBeforeObj.style.height, 10) - scrollUnit) < 0) {
                        scrollBeforeObj.style.height = 0 + 'px';
                    }
                    else {
                        scrollBeforeObj.style.height = (parseInt(scrollBeforeObj.style.height, 10) - scrollUnit) + 'px';
                    }
                }
                //break;
            //}
        }
        else {
            scrollCount++;
            break;
        }
        //count--;
    }
}

function getLocationList(type, country, region, state, county) {

    var requestHeader = "?TransID=RLOCLIST";
    var url = urlHost + requestHeader + '&Type=' + type + '&Country=' + country + '&Region=' + region + '&State=' + state + '&County=' + county;

    var changeL;

    if (type == 2) {

        var regionL = document.getElementById('Region');
        var stateL = document.getElementById('State');
        var countyL = document.getElementById('County');
        var cityL = document.getElementById('City');

        changeL = regionL;

        if (regionL) deleteAllInList(regionL);
        if (stateL) deleteAllInList(stateL);
        if (countyL) deleteAllInList(countyL);
        if (cityL) deleteAllInList(cityL);
        
    }
    else if (type == 3) {

        var stateL = document.getElementById('State');
        var countyL = document.getElementById('County');
        var cityL = document.getElementById('City');

        changeL = stateL;

        if (stateL) deleteAllInList(stateL);
        if (countyL) deleteAllInList(countyL);
        if (cityL) deleteAllInList(cityL);

    }
    else if (type == 4) {

        var countyL = document.getElementById('County');
        var cityL = document.getElementById('City');

        changeL = countyL;

        if (countyL) deleteAllInList(countyL);
        if (cityL) deleteAllInList(cityL);

    }
    else if (type == 5) {

        var cityL = document.getElementById('City');

        changeL = cityL;

        if (cityL) deleteAllInList(cityL);
    }

    ajaxCall(url + "&ChangeL=" + changeL.id, null, cGetLocationList);
}

function cGetLocationList(response) {

    var changeLid = response.substring(0, response.indexOf("\n"));
    response = response.substring(response.indexOf("\n") + 1);
    var count = response.substring(0, response.indexOf("\n"));
    response = response.substring(response.indexOf("\n") + 1);

    var changeL = document.getElementById(changeLid);

    var i = 0;
    addToList(changeL, "", "");
	while ((i = response.indexOf("\n")) > -1) {
		var element = response.substring(0, i).replace(/^\s*|\s*$/g,"");
		addToList(changeL, element, element);
		response = response.substring(i + 1);
	}
	
}

function deleteAllInList(selectInput) {
    selectInput.length = 0;
}

function addToList(selectInput, text, value) {
	var num = selectInput.options.length;
	selectInput.options[num] = new Option(value, text);
}

function deleteAllInTable(tbl) {
    while (tbl.rows.length > 0) {
        tbl.deleteRow(0);
    }
}

function addToTable(tbl, id, name) {

  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  row.className="ListTableRow";
  row.ondblclick = rowClick;
  
  var cellLeft = row.insertCell(0);
  //var textNode = document.createTextNode(id);
  //cellLeft.appendChild(textNode);
  cellLeft.innerHTML = id;
  
  // right cell
  var cellRight = row.insertCell(1);
  //var textNode2 = document.createTextNode(name);
  //cellRight.appendChild(textNode2);
  cellRight.innerHTML = name;
}

function rowClick(e) {
    var obj = eventTrigger (e);
    alert(obj.innerHTML);
    document.getElementById("ClubName").value = obj.cells[1];
    document.getElementById("ClubID").value = obj.cells[0];    
}

function eventTrigger (e) {
    if (! e) 
        e = event;
    return e.target || e.srcElement;
}


function selectOption(obj, text, keyCode) {
    
    if (keyCode == 8) {
        text = text.substring(0, text.length-1);
    }
    else {
        text = text + String.fromCharCode(keyCode);
    }
    
    if (text != '') {
        var select = document.getElementById(obj);
        for (var i=0; i < select.options.length; i++) {
            //alert(select.options[i].value.toUpperCase().substring(0, text.length));
            if (text.toUpperCase() == select.options[i].value.toUpperCase().substring(0, text.length)) {
                select.options[i].selected = true;
                break;
            }
        }
    }
}

function getkey(e) {
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}

