var cs = 1;
var bGeoCodedLocation = false;

function genprev(){
    if (document.getElementById("section" + (cs - 1)) != null){
        document.getElementById("section" + cs).style.display = "none";
        cs--;
        document.getElementById("section" + cs).style.display = "";        
        document.getElementById("btnSave").disabled = true;
        document.getElementById("btnNext").disabled = false;
    }    
    if (document.getElementById("section" + (cs - 1)) == null){
        document.getElementById("btnPrevious").disabled = true;
    }
}
function gennext(){
    if (document.getElementById("section" + (cs + 1)) != null){
        if (genval(cs))
        {
            document.getElementById("section" + cs).style.display = "none";
            cs++;
            document.getElementById("section" + cs).style.display = "";        

            /*for (var x=0;x<flds.fields.length;x++){
                var fld = flds.fields[x];
                if (fld.sec == cs && fld.type == "location")
                {
                    if (document.getElementById(fld.id + "Lat").value != "" && document.getElementById(fld.id + "Long").value != "")
                    {
                        var point = new GLatLng(parseFloat(document.getElementById(fld.id + "Lat").value), parseFloat(document.getElementById(fld.id + "Long").value));
                        map.setCenter(point, 9); 
                        map.checkResize();
                        setMarker(point);
                    }
                    else
                    {
                        //???????????????????????????????? NEED TO REPLACE THIS WITH DYNAMIC VALUE
                        map.setCenter(new GLatLng(gLat, gLong), gLevel); 
                    }
                    map.checkResize();
                    break;
                }
            }
            */
        }
    }
    if (document.getElementById("section" + (cs + 1)) == null){
        document.getElementById("btnSave").disabled = false;
        document.getElementById("btnNext").disabled = true;
        document.getElementById("btnPrevious").disabled = false;
    }
    else{
        document.getElementById("btnPrevious").disabled = false;        
    }
}
function gensave(){
    if (genval(null))
    {
        var o = {};
    	for (var x=0;x<flds.fields.length;x++){
    	    var fld = flds.fields[x];
    	    var nm = fld.name.replace(" ", "");
            switch (fld.type)
            {
                case "string":
                    o[nm] = document.getElementById(fld.id).value;
                    if (o[nm] == "") o[nm] = null;
                    break;
                case "smalldatetime":
                    o[nm] = document.getElementById(fld.id).value;
                    if (o[nm] == "") o[nm] = null;
                    break;
                case "bool":
                    o[nm] = document.getElementById(fld.id).checked;
                    break;
                case "status":
                    o[nm] = document.getElementById(fld.id).options[document.getElementById(fld.id).selectedIndex].value;
                    break;
                case "location":
                    o[nm + "Lat"] = document.getElementById(fld.id + "Lat").value;
                    o[nm + "Long"] = document.getElementById(fld.id + "Long").value;
                    o[nm + "Address"] = document.getElementById(fld.id + "Address").value;
                    o[nm + "City"] = document.getElementById(fld.id + "City").value;
                    o[nm + "StateProvince"] = document.getElementById(fld.id + "StateProvince").value;
                    o[nm + "PostalCode"] = document.getElementById(fld.id + "PostalCode").value;
                    break;
                case "category":
                    o[nm] = getSelectedCategories();
                    break;
                case "uid":
                    o[nm] = document.getElementById("txtuid").value;
                    break;
                case "participation":
                    if (document.getElementById("chkParticipation").checked)
                    {
                        o["ParticipationChallengeID"] = -1;
                        o["ParticipationChallengeDescription"] = trim(document.getElementById("txtParticipationDesc").value);
                        o["ParticipationChallengeName"] = trim(document.getElementById("txtParticipationName").value);                                                
                    }
                    break;
                case "performance":
                    if (document.getElementById("optType2").checked)
                    {
                        o["PerformanceChallengeID"] = -1;
                        o["PerformanceChallengeDescription"] = trim(document.getElementById("txtParticipationDesc").value);
                        o["PerformanceChallengeName"] = trim(document.getElementById("txtParticipationName").value);                                                
                    }
                    break;
            }    	    
        }
        var email = document.getElementById("txtEmailList").value;
        rpc.startRequest(postSave, "There was an unexpected failure trying to save your data.  Please try again.  This problem has been logged for evaluation by our support team.  If you wish to provide more feedback on this problem you can create a forum entry in the 'Bugs and Issues' forum.", _SaveURL + "?email=" + email, "data=" + escape(YAHOO.lang.JSON.stringify(o)));
    }
}
function postSave(response){
    if (response == "1"){
        if (_SaveType == 1)
        {
            if (window.confirm("Thank you for your suggestion, it will be reviewed by our staff shortly.  Once approved it will start appearing in the search results for the site.  Do you wish to make another suggestion")){
                window.location.href = window.location.href;
            }
            else{
                window.location.href = "../";
            }
        }
        else
        {
            alert("Your changes have been saved successfully.  After pressing OK you will be returned to the main page.");
            window.location.href = _SaveRetURL;
        }
    }
}
function genval(section){
	var ret = true;
	    for (var x=0;x<flds.fields.length;x++){
	        var fld = flds.fields[x];
            if (section == null || fld.sec == section)
            {
                switch (fld.type)
                {
                    case "string":
                        var v = document.getElementById(fld.id).value;
                        if (v == "" && fld.req == true){
                            alert("You must enter a value for '" + fld.name + "' in order to proceed.");
                            ret = false;
                        }
                        else if (v.length > fld.max){
                            alert("You cannot enter a value longer than '" + fld.max + "' characters for '" + fld.name + "'.");
                            ret = false;
                        }
                        break;
                    case "smalldatetime":
                        var v = document.getElementById(fld.id).value;
                        if (!isValidDate(v) || (v=="" && fld.req)){
                            alert("You must enter a valid date for '" + fld.name + "' in order to proceed.");
                            ret = false;
                        }
                        break;
                    case "bool":
                        //do nothing
                        break;
                    case "location":
                        var v = document.getElementById(fld.id + "Lat").value;
                        if (v == "" && fld.req == true){
                            alert("You must enter a valid location and have it validated by pressing the 'Find Address On Map' in order to proceed.");
                            //alert("The system has not verified the location of your event yet.  In order to save this event you must enter a location that can be verified.  As soon as you press Ok 
                            // be able to determine the location of your event.  Please verify that you have entered the correct location.");
                            ret = false;
                        }
                        break;
                    case "status":
                        //do nothing
                        break;
                    case "uid":
                        break;
                    case "category":
                        if (fld.req == true){
                            if (getSelectedCategories().length == 0){
                                alert("You must select at least one value for '" + fld.name + "' in order to proceed.");
                                ret = false;
                            }
                        }
                        break;
                    case "participation":
                        if (document.getElementById("chkParticipation").checked && trim(document.getElementById("txtParticipationDesc").value) == "")
                        {
                            alert("If you wish for a participation goal to be created you must provide a description.");
                            ret = false;
                        }
                        break;
                    case "performance":
                        if (document.getElementById("optType2").checked)
                        {
                            if (trim(document.getElementById("txtPerformanceDesc").value) == "")
                            {
                                alert("If you wish for a performance goal to be created you must provide a description.");
                                ret = false;
                            }
                            else if (document.getElementById("txtCatID").value == "")
                            {
                                alert("If you wish for a performance goal to be created you must provide an item for the goal to be applied to.");
                                ret = false;
                            }
                        }
                        break;
                    default:
                        alert(fld.type);
                        ret = false;
                        break;
                }
                if (!ret) break;
            }
	    }
	return ret;
}
//google maps specific code
var map = null; 
var geocoder = null; 

function initDataEntry()
{
    var fMap = -1;
    var fCategory = -1;
    var mapid = "";
    for (var x=0;x<flds.fields.length;x++){
        switch (flds.fields[x].type)
        {
            case "location":
                fMap = x;
                mapid = flds.fields[x].id;
                break;
            case "category":
                fCategory = x;
            default:
                break;
        }
    }

    if (fCategory > -1) buildCategoryTree(flds.fields[fCategory].cl,"f" + (fCategory + 1));
    if (fMap > -1 && GBrowserIsCompatible()) { 
        map = new GMap2(document.getElementById("map")); 
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder();

        if (document.getElementById(mapid + "Lat").value != "" && document.getElementById(mapid + "Long").value != "")
        {
            var point = new GLatLng(parseFloat(document.getElementById(mapid + "Lat").value), parseFloat(document.getElementById(mapid + "Long").value));
            map.setCenter(point, 9); 
            map.checkResize();
            setMarker(point);
        }
        else
        {
            map.setCenter(new GLatLng(gLat, gLong), gLevel); 
        }
        map.checkResize();
    } 
    
    YAHOO.util.Event.addListener(window, "unload", GUnload);
}
function initEditScreen()
{
    initDataEntry();
    rpc.startRequest(postRetrieve, "Unable to retrieve the data requested.  Please try again.  This problem has been logged for evaluation by our support team.  If you wish to provide more feedback on this problem you can create a forum entry in the 'Bugs and Issues' forum.", _GetURL, "uid=" + escape(window.location.search.substring(5)));
}
function postRetrieve(response){
    var o = eval('(' + response + ')');
	for (var x=0;x<flds.fields.length;x++){
	    var fld = flds.fields[x];
	    var nm = fld.name.replace(" ", "");
        switch (fld.type)
        {
            case "uid":
                document.getElementById("txtuid").value = o[nm];
                break;
            case "string":
                document.getElementById(fld.id).value = o[nm];
                break;
            case "smalldatetime":
                document.getElementById(fld.id).value = (o[nm].getUTCMonth()+1) + "/" + (o[nm].getUTCDate()) + "/" + o[nm].getUTCFullYear();
                break;
            case "bool":
                document.getElementById(fld.id).checked = o[nm];
                break;
            case "status":
                document.getElementById(fld.id).selectedIndex = o[nm] - 1;
                break;
            case "location":
                document.getElementById(fld.id + "Lat").value = o[nm + "Lat"];
                document.getElementById(fld.id + "Long").value = o[nm + "Long"];
                document.getElementById(fld.id + "Address").value = o[nm + "Address"];
                document.getElementById(fld.id + "City").value = o[nm + "City"];
                document.getElementById(fld.id + "StateProvince").value = o[nm + "StateProvince"];
                document.getElementById(fld.id + "PostalCode").value = o[nm + "PostalCode"];
                break;
            case "category":
                setSelectedCategories(o[nm]);
                break;
            case "participation":
                if (o["ParticipationChallengeID"] > 0)
                {
                    document.getElementById("chkParticipation").checked = true;
                    document.getElementById("trParticipationDesc").style.display = "";
                    document.getElementById("trParticipationName").style.display = "";
                    document.getElementById("txtParticipationDesc").value = o["ParticipationChallengeDescription"];
                    document.getElementById("txtParticipationName").value = o["ParticipationChallengeName"];
                }
                break;
            case "performance":
                if (o["PerformanceChallengeID"] > 0)
                {
                    document.getElementById("chkPerformance").checked = true;
                    document.getElementById("trPerformanceDesc").style.display = "";
                    document.getElementById("trPerformanceName").style.display = "";
                    document.getElementById("txtPerformanceDesc").value = o["PerformanceChallengeDescription"];
                    document.getElementById("txtPerformanceName").value = o["PerformanceChallengeName"];
                }
                break;
            default:
                alert("1AD3:" + fld.type);
                break;
        }    	    
    }    
}
function validateLocation(id){
    geoCodeLocation(document.getElementById("f" + id + "Address").value, document.getElementById("f" + id + "City").value, document.getElementById("f" + id + "StateProvince").value, document.getElementById("f" + id + "PostalCode").value, id, 0);
}
function geoCodeLocation(a, c, s, z, id, l){
    if (l > 2){
        alert("The address you entered could not be validated.");
        return;
    }
    var address = "";
    switch (l){
        case 0:
            address = a + ", " + c + ", " + s + ", " + z;
            break;
        case 1:
            address = z;
            break;
        case 2:
            address = c + ", " + s;
            break;
    }    
    geocoder.getLatLng(address,
        function(point) { 
            if (point) { 
                setLocation(point, id);
            }
            else{
                l++;
                geoCodeLocation(a,c,s,z,id, l);
            }
        }
    );
}
function setLocation(point, id){
    document.getElementById("f" + id + "Lat").value = point.lat();
    document.getElementById("f" + id + "Long").value = point.lng();
    setMarker(point);
}
function setMarker(point){
    var marker = new GMarker(point); 
    map.addOverlay(marker);
    map.setCenter(point);
    bGeoCodedLocation = true;
}
