// JavaScript Document

var http=false;
var nav4=window.Event?true:false;

if(navigator.appName=="Microsoft Internet Explorer")
  http=new ActiveXObject("Microsoft.XMLHTTP");
else
  http=new XMLHttpRequest();

function LoadPage(metodo,ruta,parametros,div)
{
	var fecha=new Date();
	if(parametros!="")
		parametros+="&timestamp="+fecha.getTime();
	else
		parametros="timestamp="+fecha.getTime();	
	
	document.getElementById(div).innerHTML="Loading...";
	http.open(metodo,ruta,true);
  http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  http.onreadystatechange=function()
  {
    if(http.readyState==4)
		{
      document.getElementById(div).innerHTML=http.responseText;
    }
  }
	
  http.send(parametros);
}

function GetCountyList(id)
{
	document.getElementById("complaint_school").innerHTML="No Hospitals Found";
	document.getElementById(id+"combo_county").value="";	
	document.getElementById(id+"combo_school").value="";		
	document.getElementById(id+"school_other").value="";			
	var state=document.getElementById(id+"combo_state").value;
	var params="id="+id+"&state="+state;
	LoadPage("POST","complaints_include/getcountylist.php",params,"complaint_county");
}

function GetCountyList2(id)
{
	var state=document.getElementById(id+"combo_state").value;
	var params="id="+id+"&state="+state;
	LoadPage("POST","complaints_include/getcountylist2.php",params,"complaint_county");
}

function GetSchoolList(id)
{
	var county=document.getElementById("combo_county").value;
	document.getElementById(id+"combo_county").value=county;
	var params="id="+id+"&county="+county;
	LoadPage("POST","complaints_include/getschoollist.php",params,"complaint_school");
}

function UpdateSchoolValue(id)
{
	var school=document.getElementById("combo_school").value;
	if(school=="")
		document.getElementById("school_other").disabled="";
	else
		document.getElementById("school_other").disabled="disabled";	
	document.getElementById("school_other").value="";		
	document.getElementById(id+"school_other").value="";			
	document.getElementById(id+"combo_school").value=school;
}

function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
 
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
}

function CheckComment(id)
{
	var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	var input_comment_name=document.getElementById(id+"input_comment_name").value;
	if(input_comment_name=="")
	{
		alert('Please provide your name');
		return false;		
	}
	var input_comment_email=document.getElementById(id+"input_comment_email").value;	
	if(!filter.test(input_comment_email))
	{
		alert('Please provide a valid email address');
		return false;
	}		
	var input_comment_body=document.getElementById(id+"input_comment_body").value;
	if(input_comment_body=="")
	{
		alert('Please provide your comment');
		return false;		
	}	
	var readterms=document.getElementById(id+"input_readterms").checked;
	if(readterms=="")
	{
		alert('Check read the terms');
		return false;		
	}		
}

function CheckForm(id,user)
{
	var valid=true;
	var state=document.getElementById(id+"combo_state").value;
	var county=document.getElementById(id+"combo_county").value;	
	var school=document.getElementById(id+"combo_school").value;
	var school_other=document.getElementById(id+"school_other").value;	
	var teacher_name=document.getElementById(id+"input_teacher_name").value;		
	var author_name=document.getElementById(id+"input_author_name").value;			
	var author_email=document.getElementById(id+"input_author_email").value;			
	var title=document.getElementById(id+"input_title").value;						
	var description=document.getElementById(id+"input_description").value;							
	if(state=="" || county=="" || (school=="" && school_other=="") || teacher_name=="" || author_email=="" || title=="" || description=="")
	{
		valid=false;
	}
	if(valid && user==1)
	{
		valid=false;
	}
	if(!valid)
		alert("Please enter required fields");
	else
	{
		var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
		if(!filter.test(author_email))
		{
			alert('Please provide a valid email address');
			valid=false;
		}
	}
	if(valid)
	{
		if(strpos(author_name,'admin',0)!==false)
		{
			alert("You can't put the administrator word or part of it");
			valid=false;
		}
	}
	if(valid)
	{
		var readterms=document.getElementById(id+"input_readterms").checked;
		if(readterms=="")
		{
			alert('Check read the terms');
			valid=false;		
		}			
	}
	return valid;
}

/* GOOGLE MAP */
var map = null;
var geocoder = null;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GMapTypeControl()); 
		map.addControl(new GSmallMapControl());		
		map.setCenter(new GLatLng(37.4419, -122.1419), 13);
		geocoder = new GClientGeocoder();
	}
}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					document.getElementById("map_canvas").innerHTML="<br/><br/>Map not available<br/><br/>"+address;
				} else {
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}