<!--
function popNav2(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=697,height=600');");
}
function popNav3(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=390,height=240');");
}
// -->


var l_object = new Object();
function checkDefaultValue(obj, val) {
	var value = obj.value;
	if(val == undefined){
		if(value == ''){
			obj.value = l_object.value;
		}
	}else{
		if(value == val){
			obj.value = '';
		}
		l_object.value = val;
	}

}

///////////////////////////
// Live Search Functions //
///////////////////////////
var tDiff = 0;
var liveSearchClosed = false;
function liveSchoolSearch(ok, direct_link){
		
	var searchString = $F('school_name');

	if(ok && searchString.length > 0 && searchString != 'Type for a Live Search' && !liveSearchClosed){

		var school_type = document.getElementById('school_type').value;
		var link_type = (direct_link === true) ? 'direct' : 'field';

		var url		= '/student.php';
		var pars	= 'action=SearchSchools&school_type='+ school_type +'&s='+ searchString +'&link_type='+ link_type;

		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: showLiveSearch
			});
	}else{
		UpdateTimer(true, direct_link);
		
		if(searchString != '' && searchString != 'Type for a Live Search' && searchString.length > 0){
			$('liveSearchResults').style.display = 'block';
			$('liveSearchResults').innerHTML = 'Searching...';
		}else{
			$('liveSearchResults').style.display = 'none';
			$('liveSearchResults').innerHTML = '';
		}
	}
	
	liveSearchClosed = false;

}

function showLiveSearch(originalRequest){
	//put returned XML in the textarea
	$('liveSearchResults').style.display = 'block';
	$('liveSearchResults').innerHTML = originalRequest.responseText;
}

function closeLiveSearch(){
	$('liveSearchResults').style.display = 'none';
	$('liveSearchResults').innerHTML = '';
	StopTimer();
	liveSearchClosed = true;
}


var timerID = 0;
var tStart  = null;

function UpdateTimer(reset, direct_link) {
	if(timerID) {
		clearTimeout(timerID);
		clockID  = 0;
	}
	if(!tStart || reset){
		tStart   = new Date();
	}
	var   tDate = new Date();
	var   tDiff = tDate.getTime() - tStart.getTime();

	tDate.setTime(tDiff);

	if(tDiff > 700){
		StopTimer();
		liveSchoolSearch(true, direct_link);
	}else{
		timerID = setTimeout("UpdateTimer(false, "+ direct_link +")", 150);
	}
}

function StopTimer() {
	if(timerID) {
		clearTimeout(timerID);
		timerID  = 0;
	}
	tStart = null;
}
/////////
// End //
/////////


///////////////////
// Hide Function //
///////////////////

function checkSchoolSelect(chosen) {
	if(document.getElementById('school_select')){
		if (chosen != '') {
			var display = (document.all) ? 'block' : 'table-row';
			document.getElementById('school_select').style.display = display;
			document.getElementById('school_name').focus();
		}else{
			document.getElementById('school_select').style.display = 'none';
		}
		document.getElementById('school_name').value = '';
	}
}

function checkCountry(country){
	if(country == 'United States'){
		var display = (document.all) ? 'block' : 'table-row';
		document.getElementById('state_field').style.display = display;
	}else{
		document.getElementById('state_field').style.display = 'none';
	}
}

function selectSchool(school_id, school_name){
	document.register.school_id.value = school_id;
	document.register.school_name.value = school_name;
	document.getElementById('liveSearchResults').style.display = 'none';
}

function swapDisplay(obj, display){
	var obj = (typeof(obj) == 'string') ? document.getElementById(obj) : obj;
	if(display != undefined){
		obj.style.display = display;
	}else{
		if(obj.style.display == '' || obj.style.display == 'block' || obj.style.display == undefined){
			obj.style.display = 'none';
		}else{
			obj.style.display = 'block';
		}
	}
}

function swapText(obj, text1, text2){
	var obj = (typeof(obj) == 'string') ? document.getElementById(obj) : obj;
	if(obj.innerHTML == text1){
		obj.innerHTML = text2;
	}else{
		obj.innerHTML = text1;
	}
}

function swapISBNForm(){
	var display = (document.all) ? 'block' : 'table-row';
	if(!document.getElementById('book_isbn').disabled){
		document.getElementById('book_isbn').disabled = true;
		document.getElementById('isbn_row').style.display = 'none';
		document.getElementById('title_row').style.display =  display;
		document.getElementById('author_row').style.display =  display;
		document.getElementById('edition_row').style.display =  display;
		document.getElementById('binding_row').style.display =  display;
		document.getElementById('no_isbn_btn').innerHTML = 'I have an ISBN.';

		document.getElementById('book_title').focus();
	}else{
		document.getElementById('book_isbn').disabled = false;
		document.getElementById('isbn_row').style.display =  display;
		document.getElementById('title_row').style.display = 'none';
		document.getElementById('author_row').style.display = 'none';
		document.getElementById('edition_row').style.display = 'none';
		document.getElementById('binding_row').style.display = 'none';
		document.getElementById('isbn_btn').innerHTML = 'I don\'t have an ISBN.';
		
		document.getElementById('book_isbn').focus();
	}
}

function swapHousingForm(){
	if(document.getElementById('category_id').value == 24 || document.getElementById('category_id').value == 23){
		document.getElementById('housing_allow_dogs').disabled = true;
		document.getElementById('housing_allow_cats').disabled = true;
	}else{
		document.getElementById('housing_allow_dogs').disabled = false;
		document.getElementById('housing_allow_cats').disabled = false;
	}
}


function textCounter(field, cntfield, maxlimit) {
	if(field.value.length > maxlimit){ // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	}else{
		cntfield.value = maxlimit - field.value.length;
	}
}

function sendMail(email, no_link) {
	if(no_link){
		document.write(email);
	}else{
		document.write('<a href="mailto:'+ email +'">'+ email +'</a>');
	}
}

function confirmDelete(message) {
	if(confirm(message)){
		return true;
	}else{
		return false;
	}
}

function enlargeClassifiedImage(id, num){
	var wwidth = 575;
	var wheight = 470;
	window.open('/image.php?action=ClassifiedImage&id='+ id +'&num='+ num, id+num, 'width='+wwidth+',height='+wheight+',scrollbars=yes,resizable=no,menubar=no,statusbar=no,top='+((screen.height/2) - (wheight/2))+',left='+((screen.width/2) - (wwidth/2)));
}

function openPopup(url){
	var wwidth = 575;
	var wheight = 470;
	window.open(url, url, 'width='+wwidth+',height='+wheight+',scrollbars=yes,resizable=no,menubar=no,statusbar=no,top='+((screen.height/2) - (wheight/2))+',left='+((screen.width/2) - (wwidth/2)));
}
