
//j QUERY functions

var j = jQuery.noConflict();
function lookup(inputString) {
	
	if(inputString.length == 0) { 
	// Hide the suggestion box.
		j('#suggestions').hide(); 
	} else { 
		if(inputString.length > 2) {
			
			//setTimeout( 
				j.post("ajax/rpc.php", {queryString: ""+inputString+""},
					function(data){ 
						if(data.length > 113 ) {
							j('#suggestions').show();
							//$.Appear(j('#suggestions').show());
							j('#autoSuggestionsList').html(data);
						}
					}
				)
			//, 2*1000 );
		}
	} 
}

function editcontentrpc(inputString) {
	if(inputString.length == 0) { 
	// Hide the suggestion box.
		j('#suggestions').hide(); 
	} else { 
		if(inputString.length > 2) {
			
			//setTimeout( 
				j.post("ajax/editcontentrpc.php", {queryString: ""+inputString+""},
					function(data){ 
						if(data.length > 113 ) {
							j('#suggestions').show();
							//$.Appear(j('#suggestions').show());
							j('#autoSuggestionsList').html(data);
						}
					}
				)
			//, 2*1000 );
		}
	} 
}

function titlelookup(inputString) {
	
	if(inputString.length == 0) { 
	// Hide the suggestion box.
		j('#suggestions').hide(); 
	} else { 
		if(inputString.length > 2) {
			
				j.post("ajax/titlelookup.php", {queryString: ""+inputString+""},
					function(data){ 
						if(data.length > 113 ) {
							j('#suggestions').show();
							//$.Appear(j('#suggestions').show());
							j('#autoSuggestionsList').html(data);
						}
					}
				)
		}
	} 
}

function gotopage(alias) {
	var alias = alias;
	window.location = alias;
}
	// lookup

function fill(thisValue) {
	j('#inputString').val(thisValue);
	j('#suggestions').hide();
}

// old functions
function changetemplate (i) { 
alert(i);
	document.getElementById('css1').href = 'css/template'+i+'.css';
	reloadAsGet(i);
	//changetemplatesifr(i);
}

function reloadAsGet(i){
	var randomnumber=Math.floor(Math.random()*1000)
	var loc = window.location; window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search +'?template='+i+'&rand='+randomnumber;
}

function showoverlay(task, mediaid) {
	j('#overlay').show();
	url = 'ajax-overlayprocessor.php?task=' + task + '&mediaid=' + mediaid;
	xhttpreq( url, 'overlaycontent' );
	}
	
function showflickrimage(image) {
	j('#overlay').show();
	url = 'ajax-overlayprocessor.php?task=showflickrimage&mediaid=' + image;
	xhttpreq( url, 'overlaycontent' );;
}

function hideoverlay() {
	j('#overlay').hide();
	}

function validate(f) {
	var error = false;
	var theForm = f;
	var alertText = "Errors on this form: \n\n";
   for(i=0; i<theForm.elements.length; i++){
     //alertText += "Element Type: " + theForm.elements[i].type + "\n"
    	//if( theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" ){
    	if( theForm.elements[i].type == "text" ){
		  if (theForm.elements[i].value == "") {
			//alertText += "Blank Field\n"
      		alertText += theForm.elements[i].id + ": blank\n"
	  		error = true;
		  }
      }
      
   }
   if (error == false) { return true; } else { alert(alertText); return false; }
}

function showcalendar() {
	j('#overlay').show;
	url = 'calendar.php?';
	iframereq( url, 'overlaycontent', '900' );
	}



function addcontentform(userid) {
	j('#overlay').show;
	url = 'addcontent.php?task=addcontent&userid='+userid;
	iframereq( url, 'overlaycontent', '900' );
	}

function addcontent(f) {
	if (validate(f) == true) {;
		firstname = f.firstname.value;
		lastname = f.lastname.value;
		email = f.email.value;
		statement = f.statement.value;
		persontype = f.type.value;
		imagename = 'aaaa.jpg';
		url = 'addcontent.php?task=add&firstname=' + firstname + '&lastname=' + lastname + '&email=' + email +'&statement=' + statement +'&type=' + persontype +'&image=' + imagename;
		xhttpreq( url, 'overlaycontentcontainer' );
	}
}


function addpersonform() {
	j('#overlay').show;
	url = 'addperson.php?task=addperson';
	iframereq( url, 'overlaycontent', '900' );
	}

function addperson(f) {
	if (validate(f) == true) {;
		firstname = f.firstname.value;
		lastname = f.lastname.value;
		email = f.email.value;
		statement = f.statement.value;
		persontype = f.type.value;
		imagename = 'aaaa.jpg';
		url = 'addperson.php?task=add&firstname=' + firstname + '&lastname=' + lastname + '&email=' + email +'&statement=' + statement +'&type=' + persontype +'&image=' + imagename;
		xhttpreq( url, 'overlaycontentcontainer' );
	}
}

function showsubmitbutton(f) {
	// add more validation to this, like email or is num
var error = false;
var theForm = f;
var formlength = theForm.elements.length;
var textelements = 0;
var success = 0;
	for(i=0; i<formlength; i++){
		if( theForm.elements[i].className == "required" ){
			textelements++;
			if (theForm.elements[i].value == "" ) {
			} else {
				success++;
			}
		}
	}
	if (success == textelements) { document.stdform.submitbtn.disabled=false; } else { document.stdform.submitbtn.disabled=true; }
}

function addcomments(mediaid, f) {
	if (validate(f) == true) {
		name = f.addcommentname.value;
		url = 'ajax-comments.php?task=add&mediaid=' + mediaid +'&content=' + f.addcommentcontent.value +'&name=' + name;
		xhttpreq( url, 'comments' );
	}
}

function upscore(mediaid, score, userip) {
	//sessvars.$.debug();
	sessvars.pageCount=sessvars.pageCount||0;
	sessvars.pageCount++;
	if (sessvars.myObj == undefined) {
		sessvars.myObj = {mediaids: ',' };
	}
	
	var upscorego = true;
	if (sessvars.myObj != undefined ) {
	//	sessvars.myObj = {mediaids: sessvars.myObj.mediaids+','+mediaid }
		var mystring = sessvars.myObj.mediaids+',';
		if (mystring.search(',') >= 0 ) {
			var a1 = new Array();
			a1 = mystring.split(",");
			Array.find = function(ary, mediaid){
				for(var i=0; i<ary.length; i++){
					if(ary[i] == mediaid){
							upscorego = false;
							return i;
					} 
				} 
				return -1;
			}
		Array.find(a1, mediaid);
		sessvars.myObj = {mediaids: sessvars.myObj.mediaids+','+mediaid }
		}
	}
	
	if (upscorego == false) {
		alert('You have already scored this item.')
	}
		
	if (upscorego == true) {
			url = 'ajax-score.php?task=add&mediaid=' + mediaid +'&score=' + score;
			xhttpreq( url, 'score' );
	}
	upscorego = null;
	
}

	
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
	
	
function xhttpreq(url, div) {
	var randomnumber=Math.floor(Math.random()*10000);
	url = url + '&rand=' + randomnumber;
  xmlhttp.open("get", url, true);
  xmlhttp.onreadystatechange = function(){ useHttpResponse( div );}
  xmlhttp.send(null);
//document.getElementById('menuimage').innerHTML = '<img style="margin-top:30px;" src="images/menuimage'+param+'.png" />'; // update menu image
}

function useHttpResponse(div) {
  if (xmlhttp.readyState == 4) {
    var textout = xmlhttp.responseText;
    document.write.textout;
	document.getElementById(div).innerHTML = textout;
  }
}

function iframereq(url, div, height) {
	var randomnumber=Math.floor(Math.random()*10000);
	url = url + '&rand=' + randomnumber;
	textout = '<center><iframe src="'+url+'" width="710" height="'+height+'" frameborder="no" scrolling="no"></iframe></center>';
	document.getElementById(div).innerHTML = textout;	
}