var ajax = new Array();

function safeAJAX(theterm){

theterm = theterm.replace(/&/, 'and');

return theterm;

}

function new_ajax(divid){

	if(typeof(ajax[divid]) == 'object'){ajax[divid].abort(); ajax[divid] = null;}

	try{
		ajax[divid]=new XMLHttpRequest();
	}

	catch (e){
		try{
			ajax[divid]=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				ajax[divid]=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

}

function ajax_innerHTML(divid, pageurl, postparams, url_from_root){

	if(!postparams){
		postparams = '';
	}

	if(!url_from_root){
		url_from_root = '';
	}

	ajax_loops = 0;

	new_ajax(divid);

	ajax[divid].onreadystatechange=function(){
		if(ajax[divid].readyState==4){

			document.getElementById(divid).innerHTML = ajax[divid].responseText;

		}
		else{
			if(ajax_loops == 0){

				document.getElementById(divid).innerHTML = "<div style='padding:5px;height:25px;position:relative;margin-bottom:-37px;opacity:.75;filter:alpha(opacity=75);background:white;text-align:center;border:solid rgb(175,175,175) 1px;'>Loading... <img src='/img/searching.gif' style='display:inline;' /></div>" + document.getElementById(divid).innerHTML;

				ajax_loops++;
			}			
		}

	}


	if(postparams != ""){
		ajaxmethod = "POST";
	}
	else{
		ajaxmethod = "GET";
	}


	if(url_from_root != 'true'){

		pageurl = "http://www.thywordnetwork.org/bin/" + pageurl;

	}

	ajax[divid].open(ajaxmethod, pageurl, true);

	if(postparams != ""){
		ajax[divid].setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	}

	ajax[divid].send(postparams);
}


function main_dynamic_search(term){

	divid = "dynamic_search";

	new_ajax(divid);

	ajax[divid].onreadystatechange=function(){
		if(ajax[divid].readyState==4){

			if(ajax[divid].responseText != "" && ajax[divid].responseText != "No Results"){
				document.getElementById(divid).innerHTML = ajax[divid].responseText;
				document.getElementById(divid).style.display = 'block';
			}
			else if(ajax[divid].responseText == "No Results"){

				document.getElementById(divid).innerHTML = '';
				document.getElementById(divid).style.display = 'none';

			}



		}


	}

		ajaxmethod = "GET";

		pageurl = "http://www.thywordnetwork.org/bin/dynamic_search.php?search="+term;

	ajax[divid].open(ajaxmethod, pageurl, true);

	ajax[divid].send('');
}


function thumb_rate(objTYPE, objID, objRATE){

	new_ajax('thumbrate');

	ajax['thumbrate'].onreadystatechange=function(){
		if(ajax['thumbrate'].readyState==4){

			update_song();



		}
		else{
			
		}

	}

	ajax['thumbrate'].open("GET", "http://www.thywordnetwork.org/bin/ratings.php?type="+objTYPE+"&id="+objID+"&thumb="+objRATE, true);
	ajax['thumbrate'].send('');

}

function update_song(){

	new_ajax('song');

	ajax['song'].onreadystatechange=function(){
		if(ajax['song'].readyState==4){

			var ajaxresponse;
			ajaxresponse = ajax['song'].responseText;

			if(ajaxresponse && ajaxresponse !== ""){
				eval(ajaxresponse);
			}

			var onairtimeout;
			onairtimeout = setTimeout("update_song()", 20000);



		}
		else{
			
		}

	}

	ajax['song'].open("GET", "http://www.thywordnetwork.org/bin/songsearch.php", true);

	ajax['song'].send('');

}

function tags(kwrd){

	kwrd = noHTML(kwrd);

	new_ajax('tags');


	ajax['tags'].onreadystatechange=function(){
		if(ajax['tags'].readyState==4){


			document.getElementById('tagtext').innerHTML = ajax['tags'].responseText;



		}
		else if(ajax['tags'].readyState==1){

			document.getElementById('tags').style.display = 'block';
			document.getElementById('tagtext').innerHTML = "Requesting Content... <img src='/img/searching.gif' style='display:inline;' />";

		}

	}

	ajax['tags'].open("GET", "http://www.thywordnetwork.org/bin/tags.php?keyword=" + kwrd, true);

	ajax['tags'].send('');
}


function entercontest(){


	var cname = noHTML(document.getElementById('contest_name').value);
	var cage = noHTML(document.getElementById('contest_age').value);
	var cphone = noHTML(document.getElementById('contest_phone').value);
	var cemail = noHTML(document.getElementById('contest_email').value);
	var cadd1 = noHTML(document.getElementById('contest_address1').value);
	var cadd2 = noHTML(document.getElementById('contest_address2').value);
	var centry = noHTML(document.getElementById('contest_entry').value);


	if(document.getElementById('contest_signup').checked == 1){
		csign = 'yes';
	}
	else{
		csign = 'no';
	}


	var error = '';

	if(cname.replace(/\s/g, '') == ''){
		error += "Please enter your name.\n";
	}
	if(cage.match(/^\d{1,2}$/) == null){
		error += "Please enter your age.\n";
	}

	if(cphone.match(/\(?\d\d\d\)?[- \.]?\d\d\d[- \.]?\d\d\d\d/) == null){
		error += "Please enter a valid phone number with area code.\n";
	}


	if(cemail.match(/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i) == null){
		error += "Please enter a valid email address.\n";
	}

	if(cadd1.replace(/\s/g,'') == ''){
		error += "Please enter your address.\n";
	}
	else if(cadd2.replace(/\s/g,'') == ''){
		error += "Please enter the second line of your address.\n";
	}
	else if(cadd2.match(/\d{5}/) == null){
		error += "Please remember your zip code.\n";
	}

	if(error != ''){
		alert(error);
		return false;
	}


	ajax_innerHTML('contesttext', "mail.php?do=contest", "name=" + cname + "&age=" + cage + "&phone=" + cphone + "&email=" + cemail + "&add1=" + cadd1 + "&add2=" + cadd2 + "&entry=" + centry + "&sign=" + csign);

	return false;
}


function faith_promise(){

	form = document.getElementById('donation_form');
	
	var frequency;

	if(form.frequency[0].checked==true){
		frequency = form.frequency[0].value;
	}
	if(form.frequency[1].checked==true){
		frequency = form.frequency[1].value;
	}
	if(form.frequency[2].checked==true){
		frequency = form.frequency[2].value;
	}
	if(form.frequency[3].checked==true){
		frequency = form.frequency[3].value;
	}

	ajax_innerHTML('donation_form_content', "donationform.php?name="+safeAJAX(form.name.value)+"&email="+safeAJAX(form.email.value)+"&frequency="+safeAJAX(frequency)+"&onetimeamount="+safeAJAX(form.onetimeamount.value)+"&monthlyamount="+safeAJAX(form.monthlyamount.value)+"&date="+safeAJAX(form.date.value)+"&occassion="+safeAJAX(form.occassion.value)+"&street="+safeAJAX(form.street.value)+"&city="+safeAJAX(form.city.value)+"&state="+safeAJAX(form.state.value)+"&zip="+safeAJAX(form.zip.value)+"&phone1="+safeAJAX(form.phone1.value)+"&phone2="+safeAJAX(form.phone2.value)+"&phone3="+safeAJAX(form.phone3.value));

}