/**************************************************************************************************
*
**************************************************************************************************/
function ValidateConditionForm()
	{
		//check for empty required fields
		if (document.getElementById("conditioncomments").value == "")
		{
			alert("The Condition Comments field is required.");
			return false;
		}
		//form is valid
		else {
			return true;
		}
	}
	
	
	
/**************************************************************************************************
*
**************************************************************************************************/
function ApplyViewMode(viewmode)
{
    var i = 1;
	
	if (viewmode == 'all')
	{
		//show everything
		while(document.getElementById('entry_'+i))
		{   
		    document.getElementById('entry_'+i).style.display = '';
		    i = i+1;
		}
	}
	else 
	{
		while(document.getElementById('entry_'+i))
		{
		    if(i<=2)
		    {
		        document.getElementById('entry_'+i).style.display = '';
		    }
		    else
		    {
		        document.getElementById('entry_'+i).style.display = 'none';
		    }
		    
		    i = i+1;
		}
	}
}


/**************************************************************************************************
*
**************************************************************************************************/
function ToggleViewMode()
{
    var i = 1;
	
	if (viewmode == 'all')
	{
		//we are currently showing everything - so only show default
		while(document.getElementById('entry_'+i))
		{
		    if(i<=2)
		    {
		        document.getElementById('entry_'+i).style.display = '';
		    }
		    else
		    {
		        document.getElementById('entry_'+i).style.display = 'none';
		    }
		    
		    i = i+1;
		}
		
		//update the viewmode
		viewmode = 'default';
		
		//update the link
		document.getElementById('viewmodelink').innerHTML = 'View All';

	}
	else 
	{
	    //we are currently showing the default amount - so show everything
        while(document.getElementById('entry_'+i))
		{   
		    document.getElementById('entry_'+i).style.display = '';
		    i = i+1;
		}
		
		//update the viewmode
		viewmode = 'all';
		
		//update the link
		document.getElementById('viewmodelink').innerHTML = 'View Less';
	}
}
	
	
/**************************************************************************************************
*
**************************************************************************************************/
function SaveComment(FeatureId){
    var xmlHttp;
    var paramstring;        //parameter string to send
    
    var conditioncomments;
    var tripdate;
    var route;
    var activity;
    var submittedby;
    
    var i=1;                //temp counter variable
    
    // make sure the inputs are valid
    if (!ValidateConditionForm())
    {
        return false;
    }
    
    
    // create xmlhttp object
    xmlHttp = null;
    
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
        xmlHttp=new XMLHttpRequest()
    }
    // code for IE
    else if (window.ActiveXObject)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    else
    {
        alert("Your browser does not support XMLHTTP.")
    }


    //wait for and catch response from server
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4) 
        {
            if(xmlHttp.responseText=='Failed')
            {
                //do nothing
                alert('Sorry, we could not save your comments.');
            }
            else if(xmlHttp.responseText=='Date')
            {
                alert('Trip Date must be in the format of mm/dd/yyyy.');
            }
            else if(xmlHttp.responseText=='ConditionComments')
            {
                alert('Condition Comments is a required field and cannot be blank.');
            }
            else
            {
                //clear the form
                document.getElementById('conditioncomments').value = '';
                document.getElementById('tripdate').value = '';
                document.getElementById('route').value = '';
                document.getElementById('activity').value = '';
                document.getElementById('submittedby').value = '';
                
                //update the entries to include this newest one
                document.getElementById('conditionreport').innerHTML = xmlHttp.responseText;
                
                //apply view setting (either default or view all)
                ApplyViewMode(viewmode);
                
                //if there are now more than 2 Condition Report entries, then show the 'View More' link
                //first we have to count how many entries we have
                while (document.getElementById('entry_'+i))
                {
                    i = i + 1;
                }
                
                if (i-1 > 2)
                {
                    document.getElementById('viewmodelink').style.display = '';
                }
            }
        }
    }

    //open request
    xmlHttp.open("POST","/mountains/_insertconditionreport.asp",true);
    
    //create parameter strings
    conditioncomments = encodeURIComponent(document.getElementById("conditioncomments").value);
    tripdate = encodeURIComponent(document.getElementById("tripdate").value);
    route = encodeURIComponent(document.getElementById("route").value);
    activity = encodeURIComponent(document.getElementById("activity").value);
    submittedby = encodeURIComponent(document.getElementById("submittedby").value);
    
    paramstring = "fid=" + FeatureId + "&conditioncomments=" + conditioncomments + "&tripdate=" + tripdate;
    paramstring = paramstring + "&route=" + route + "&activity=" + activity + "&submittedby=" + submittedby;

    //set some properties
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //this one is required
    xmlHttp.setRequestHeader("Content-length", paramstring.length);
    
    //send request
    xmlHttp.send(paramstring);
    
}