document.onclick = ClosePreview;
var loadingPreview =0;

String.prototype.replaceAll = function(
 strTarget, // The substring you want to replace
 strSubString // The string you want to replace in.
 ){
 var strText = this;
 var intIndexOfMatch = strText.indexOf( strTarget );
  
 // Keep looping while an instance of the target string
 // still exists in the string.
 while (intIndexOfMatch != -1){
 // Relace out the current instance.
 strText = strText.replace( strTarget, strSubString )
  
 // Get the index of any next matching substring.
 intIndexOfMatch = strText.indexOf( strTarget );
 }
 
 // Return the updated string with ALL the target strings
 // replaced out with the new substring.
 return( strText );
 }
function WhatIsRatings(from, to, rating_system)
{
	rating_system = rating_system.replaceAll("|", "\n");
	var alert_txt = "";
	alert_txt += "We have created a rating system that reflects how many violations were found in the last complete year  by the Department of Social Services.  We are in no way responsible for rating system and request that all families and parents do their own due diligence.  We have simply created a user friendly search tool to find public information provided by states.";
	alert_txt += "\n\n";
	alert_txt += "5 Wagons represents the fewest violations in the past year and 1 Wagon represents the most violations. \n\n";
	alert_txt += "\nCheckChildCare's current system.\n\n";
	alert_txt += rating_system+"\n\n";
	alert_txt += "The rating system counts all violations found within "+GetReadableDate(from)+" to "+GetReadableDate(to);
	alert(alert_txt);
}
function GetReadableDate(str)
{
	if(str.indexOf("-")>0)
	{
		var arr = str.split("-");
		return arr[1]+"/"+arr[2]+"/"+arr[0];
	}
	return "";
}
function ShowSearch()
{
	xmlhttpPost2("templates/search_template_horizontal.html.php", "option_search");
}
function ClosePreview()
{
	var div = document.getElementById("preview");
	if(typeof div != "undefined" && div != null && loadingPreview == 0)
	{
		div.style.visibility = "hidden";
	}

}
function PreviewPage(url,date)
{
	var div = document.getElementById("preview");
	if(typeof div == "undefined" || div == null)
	{
		creatediv("preview", " ", 250, 250, 700, 200);
	}
	else
	{
		div.style.visibility = "visible";
		div.style.height = 250;
		div.style.overflow= "scroll";
		div.style.backgroundColor= "#FFFFFF";
	}

	if(loadingPreview == 0)
	{
		loadingPreview =1;
		previewHttpPost("scripts/loadPage.php?url="+url+"&title="+date, "preview");
	}
}
function creatediv(id, html, width, height, left, top) {

   var newdiv = document.createElement('div');
   newdiv.setAttribute('id', id);
   
   if (width) {
       newdiv.style.width = 300;
   }
   
   if (height) {
       newdiv.style.height = 300;
   }
   
   if ((left || top) || (left && top)) {
       newdiv.style.position = "absolute";
       
       if (left) {
           newdiv.style.left = left;
       }
       
       if (top) {
           newdiv.style.top = top;
       }
   }
   
   //newdiv.style.background = "#00C";
		newdiv.style.backgroundColor= "#FFFFFF";
   newdiv.style.border = "1px solid #ccc"; 
newdiv.style.overflow= "scroll";  
   
   if (html) {
       newdiv.innerHTML = html;
   } else {
       newdiv.innerHTML = "nothing";
   }
   
   document.body.appendChild(newdiv);

} 
function clearSearchForm(){
	var name_obj = document.getElementById("center_name");
	var city_obj = document.getElementById("city");
	var zip_obj = document.getElementById("zip");
	
	if(typeof name_obj != "undefined" && name_obj != null){
		name_obj.value = "";
	}
	if(typeof city_obj != "undefined" && city_obj != null){
		city_obj.value = "";
	}
	if(typeof zip_obj != "undefined" && zip_obj != null){
		zip_obj.value = "";
	}
}
function explainThis(what)
{
	if(what == "flag"){
		alert('If you click the flag you are indicating that you believe that \nthe information provided may be false or inaccurate.  \n\nPlease flag with care.');
	}
	else if(what == "rating")
	{
		alert("Rating system takes in consideration all of the companies violations and ranks them according to the total number. \n\nRange scales from 1 wagon, being the worst, to 5 wagons being top of the line!");
	}
}
var this_state = "";
function makeCities(obj)
{
	if(obj.value != "")
	{
		this_state = obj.value;
		xmlhttpPost2("scripts/cities.script.php?state="+obj.value, "city_div");
	}
}

function makeCities2(obj)
{
	if(obj.value != "")
	{
		this_state = obj.value;
		xmlhttpPost2("../scripts/cities.script.php?state="+obj.value+"&rem=1&prov=1", "city_div");
	}
}
function makeProviders2(obj)
{
	if(obj.value != "")
	{
		xmlhttpPost2("../scripts/providers_select2.script.php?state="+this_state+"&city="+obj.value, "provider_div");
	}
}

function makeProviders(obj)
{
	if(obj.value != "")
	{
		xmlhttpPost2("scripts/providers_select.script.php?state="+this_state+"&city="+obj.value, "provider_div");
	}
}

function showProviderValue(obj)
{
	if(typeof obj != "undefined" && obj != null)
	{
		alert(obj.value);
	}
}

function checkPosted(str){	
	if(str != ''){
		var obj = document.getElementById('city');
		if(typeof obj != "undefined" && obj != null){
			if(typeof obj.options != "undefined" && obj.options != null)
			{
				for(i=0; i< obj.options.length; i++){
					if(obj.options[i].text == str){
						obj.options[i].selected = true;
						break;
					}
				}	
			}
		}
	}
}

function submitSearch(){
	var chk = document.getElementById("agree");
	if(typeof chk != "undefined" && chk != null){
		if(chk.checked){
			document.searchform.submit();
		}
		else{ alert("Please agree to the terms & conditions."); }
	}
}

function getId(str)
{
	var str_arr = str.split('_');
	return str_arr;
}

function hoverWagon(obj)
{
	if(typeof obj != "undefined" && obj != null)
	{	
		obj.style.border = "solid 2px #FF0000";
		obj.style.filter = "alpha(opacity=100)";
		obj.style.opacity = "1";


		var obj2 = document.getElementById("rank_text");
		if(typeof obj2 != "undefined" && obj2 != null)
		{
			var arr = getId(obj.id);
			obj2.innerHTML = "Rate them a "+arr[1];
		}
	}
}
var justSelected=0;
function unHoverWagon(obj, isNotOpaque)
{

	if(typeof obj != "undefined" && obj != null)
	{	
		obj.style.border = "";
		if(isNotOpaque == 0 && justSelected==0)
		{
			obj.style.filter = "alpha(opacity=50)";
			obj.style.opacity = ".5";
		}
	}
	justSelected=0;
}
function showRatingSelection(obj)
{
	var arr= getId(obj.id);
	var company_id = arr[0];
	var wagon_num = arr[1];
	for(i=1; i<=wagon_num; i++)
	{	
		var this_obj = document.getElementById(company_id+"_"+i);
		if(typeof this_obj != "undefined" && this_obj != null)
		{
			this_obj.style.filter = "alpha(opacity=100)";
			this_obj.style.opacity = "1";			
		}
	}
	var setObj = document.getElementById("the_rank");
	if(typeof setObj != "undefined" && setObj != null)
	{
		setObj.value = wagon_num;
	}	
	justSelected=1;
}
function selectRating(obj)
{
	var arr= getId(obj.id);
	if(arr[0]>0 && arr[1]>0)
	{
		var obj2 = document.getElementById("all_rank");
		if(typeof obj2 != "undefined" && obj2 != null)
		{
			obj2.innerHTML = "";
		}
		xmlhttpPost2("scripts/ranking.script.php?cid="+arr[0]+"&r="+arr[1], "all_rank");
	}

}
function whatsRanking()
{
	alert("Rating System:\n\nRegistered users can log on to provide their ratings \nto the daycare centers they attend!\n\nSimply hover over the wagon you'd like to score and select. \n\n1 - poor\n2 - below average\n3 - average\n4 - above average\n5 - BEST DAYCARE EVER!");
}

var last_color = "";
function unHighlightOption(obj)
{
	obj.style.background = last_color;
	obj.style.color = "#000000";
	last_color = "";
}

function highlightOption(obj)
{
	if(typeof obj != "undefined" && obj != null)
	{
		last_color = obj.style.background ;
		obj.style.color = "#FFFFFF";
		obj.style.background = "#0000FF";
	}
	
}
function setID(obj)
{
	var prov = document.getElementById("providers");
	if(typeof prov != "undefined" && prov != null)
	{
		setField("provider_id", obj.id);		
		var output = removeTags(obj.innerHTML);
		var output_arr = output.split('|');
		setField("provider_name", output_arr[0]);
		prov.style.visibility = "hidden";
	}
	
}
function removeTags(str)
{
	return str.replace(/<[^>]+>/g, "");
}
function setField(field_id, value_to_set)
{	
	var field = document.getElementById(field_id);
	
	if(typeof field != "undefined" && field != null)
	{
		field.value = value_to_set;
	}	
}
function getProviderList(obj)
{
	var prov = document.getElementById("providers");
	if(typeof prov != "undefined" && prov != null)
	{
		prov.style.visibility = "visible";
		xmlhttpPost("scripts/list_providers.script.php?p="+obj.value, "providers");
	}
}
function removeList()
{
	var prov = document.getElementById("providers");
	if(typeof prov != "undefined" && prov != null)
	{
		prov.style.visibility = "hidden";
	}
}
function previewHttpPost(strURL, div) {
	document.getElementById(div).innerHTML = "<img src='images/indicator.gif' align='left'><span style='line-height: 14px; font-weight: bold; font-size: 10px;'> loading preview...</span>";
	var xmlHttpReq = false;
	var self = this;
	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		//document.getElementById(div).innerHTML = self.xmlHttpReq.status;
		if (self.xmlHttpReq.readyState == 4) {
		    donepreviewing(self.xmlHttpReq.responseText, div);
		}
	}
	self.xmlHttpReq.send(null);
}
function xmlhttpPost2(strURL, div) {
	var start = "";
	if(strURL.indexOf("../") >=0)
	{
		start = "../";
	}
	document.getElementById(div).innerHTML = "<img src='"+start+"images/indicator.gif' align='left'><span style='line-height: 14px; font-weight: bold; font-size: 10px;'> processing...</span>";
	var xmlHttpReq = false;
	var self = this;
	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		//document.getElementById(div).innerHTML = self.xmlHttpReq.status;
		if (self.xmlHttpReq.readyState == 4) {
		    updatepage(self.xmlHttpReq.responseText, div);
		}
	}
	self.xmlHttpReq.send(null);
}
function xmlhttpPost(strURL, div) {
	document.getElementById(div).innerHTML = "<img src='images/indicator.gif' align='left'><span style='line-height: 14px; font-weight: bold; font-size: 10px;'>Searching database...<br><br>Please be patient while we search the state of Virginia database.  Please remember by registering with us we will send you email alerts when a new state inspection has been posted for your provider.</span>";
	var xmlHttpReq = false;
	var self = this;
	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		//document.getElementById(div).innerHTML = self.xmlHttpReq.status;
		if (self.xmlHttpReq.readyState == 4) {
		    updatepage(self.xmlHttpReq.responseText, div);
		}
	}
	self.xmlHttpReq.send(null);
}
function UpdateCompanies(company_ids)
{
	var div = "is_updated";
	document.getElementById(div).innerHTML = "<img src='images/indicator.gif' align='left'><span style='line-height: 14px; font-weight: bold; font-size: 10px;'>Looking for company updates...</span>";
	var xmlHttpReq = false;
	var self = this;
	var strURL = "proxy.php?company_ids="+company_ids+"&state=TX";
	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	self.xmlHttpReq.open('GET', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() 
	{
		if (self.xmlHttpReq.readyState == 4) {
		    companies_updated(self.xmlHttpReq.responseText, div);
		}
	}
	self.xmlHttpReq.send(null);

}
function UpdateCompany(company_id)
{
	var div = "is_updated";
	document.getElementById(div).innerHTML = "<img src='images/indicator.gif' align='left'><span style='line-height: 14px; font-weight: bold; font-size: 10px;'>Looking for company updates...</span>";
	var xmlHttpReq = false;
	var self = this;
	var strURL = "proxy.php?proxy_url=http://www.dfps.state.tx.us/Child_Care/Search_Texas_Child_Care/ppFacilityDetails.asp&ptype=DC&fid="+company_id;
	//alert(strURL);
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	self.xmlHttpReq.open('GET', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() 
	{
		if (self.xmlHttpReq.readyState == 4) {
		    page_updated(self.xmlHttpReq.responseText, div);
		}
	}
	self.xmlHttpReq.send(null);
}
function companies_updated(response, div)
{
	var obj = document.getElementById(div);
	if(typeof obj != "undefined" && obj != null)
	{	
		obj.innerHTML = response;
	}
}
function page_updated(response, div)
{
	var obj = document.getElementById(div);
	if(typeof obj != "undefined" && obj != null)
	{	
		obj.innerHTML = "<span style='font-size: 10px;'>"+response+"</span>";
	}
}
function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}
function donepreviewing(str, div)
{
	document.getElementById(div).innerHTML = str;		
	loadingPreview = 0;
}
function updatepage(str, div)
{
	document.getElementById(div).innerHTML = str;	
}
