// JavaScript Document

function updateCity(stateField,table){
	$('city').disabled = true;
	var state = stateField.value;
	var params = "table=" + table + "&action=city_list&state=" + state;
	new Ajax.Updater("city_list","/js/semjax.php",{parameters:params});
}

function updateState(regionField,table){
	$('state').disabled = true;
	$('city').disabled = true;
	var region = regionField.value;
	var params = "table=" + table + "&action=state_list&region=" + region;
	new Ajax.Updater("state_list","/js/semjax.php",{parameters:params, onComplete: function(){
			updateCity($('state'),table);
		}
	});
}

function listCities(stateField,table){
	$('cities').disabled = true;
	var state = Form.Element.serialize(stateField);
	state = state.gsub(/%2520/," ");
	var params = "table=" + table + "&action=cities_list&" + state;
	if(state != "invalid"){
		new Ajax.Updater("city_list","/js/semjax.php",{parameters:params});
	}
}

function listStates(regionField,table){
	$('states').disabled = true;
	$('cities').disabled = true;
	var region = Form.Element.serialize(regionField);
	region = region.gsub(/%2520/," ");
	var params = "table=" + table + "&action=states_list&" + region;
	if(region != "invalid"){
		new Ajax.Updater("state_list","/js/semjax.php",{parameters:params, onComplete: function(){
				listCities($('states'),table);
			}
		});
	}
}

function submitAction(formname, submitBtn, wait_msg){
	var process = true;
	if(formname == 'search_cvb'){
		var theform = document.forms[formname];
		if($F('query') != "" && $F('query').length < 4){
			Element.update('validator','Your search phrase was too short. It must consist of 4 characters or more.');
			Element.show('validator');
			process = false;
		}
		if($F('query') == "" && $F('city') == "" && $F('state') == ""){
			Element.update('validator','You must select a city and/or state, or enter keywords before searching. To see all directories, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(formname == 'search_soccer'){
		var theform = document.forms[formname];
		if($F('query') == "" && $F('city') == "" && $F('state') == "" && $F('region') == ""){
			Element.update('validator','You must select a city, state and/or region, or enter keywords before searching. To see all soccer fields, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
		if($F('query').length < 4 && $F('query') != ""){
			Element.update('validator','You search term is too short. Please enter a search word/phrase that is longer than 3 characters.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(formname == 'narrow_soccer'){
		var theform = document.forms[formname];
		if($F('indoor_fields') == "" && $F('outdoor_fields') == "" && $F('youth_fields') == "" && $F('regulation_fields') == "" && $F('seating_bleachers') == "" && $F('seating_stadium') == "" && $F('metro_area') == "" && $F('query') == ""){
			Element.update('validator','You must enter at least 1 value for the fields provided. To see all soccer fields, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
		if($F('query').length < 4 && $F('query') != ""){
			Element.update('validator','Your search word was too short. Please enter 4 letters or more for a successful search.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(formname == 'search_soccer_supply'){
		var theform = document.forms[formname];
		if($F('query') == "" && $F('city') == "" && $F('state') == ""){
			Element.update('validator','You must select a city and/or state, or enter keywords before searching. To see all soccer manufacturers &amp; supplies, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(formname == 'search_bbsb'){
		var theform = document.forms[formname];
		if($F('query') == "" && $F('city') == "" && $F('state') == "" && $F('region') == ""){
			Element.update('validator','You must select a city, state and/or region, or enter keywords before searching. To see all baseball/softball fields, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
		if($F('query').length < 4 && $F('query') != ""){
			Element.update('validator','Your search word was too short. Please enter 4 letters or more for a successful search.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(formname == 'narrow_bbsb'){
		var theform = document.forms[formname];
		if($F('total_bbfields') == "" && $F('total_sbfields') == "" && $F('lighted_bbfields') == "" && $F('lighted_sbfields') == "" && $F('youth_bbfields') == "" && $F('youth_sbfields') == "" && $F('hs_bbfields') == "" && $F('college_bbfields') == "" && $F('college_sbfields') == "" && $F('seating_bbbleachers') == "" && $F('seating_sbbleachers') == "" && $F('seating_bbstadium') == "" && $F('seating_sbstadium') == "" && $F('query') == ""){
			Element.update('validator','You must enter at least 1 value for the fields provided. To see all baseball/softball fields, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
		if($F('query').length < 4 && $F('query') != ""){
			Element.update('validator','You search term is too short. Please enter a search word/phrase that is longer than 3 characters.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(formname == 'search_bbsb_supply'){
		var theform = document.forms[formname];
		if($F('query') == "" && $F('city') == "" && $F('state') == ""){
			Element.update('validator','You must select a city and/or state, or enter keywords before searching. To see all baseball/softball manufacturers &amp; supplies, click the VIEW ALL LISTINGS button.');
			Element.show('validator');
			process = false;
		}
	}
	
	if(process){
		$(submitBtn).style.visibility = 'hidden';
		Element.show(wait_msg);
	}
	return process;
}

function toggleCopy(elm){
	if(elm.value == "included"){
		Element.show("included_copy");
	} else {
		Element.hide("included_copy");
	}
}

function swapCover(elm,img_src){
	elm.firstChild.src = img_src;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function switchTab(num){
	//hide all pages
	var the_pages = $$('.tab_content');
	the_pages.each(function(node){
		Element.hide(node);
	});
	//show current page
	Element.show('tab_content'+num);
	//blank all tabs
	var the_tabs = $$('.current');
	the_tabs.each(function(node){
		node.removeClassName("current");
	});
	//add class to current
	$('tab'+num).addClassName("current");
	//setup cookie
	createCookie("sem_tab_current",num,90);
	return false;
}

var storage = null;
function changeFocus(start,elm){
	if(start.checked == true){
		if($(elm)){
			if(storage){
				$(elm).value = storage;
			}
			$(elm).focus();
		}
	} else {
		if($F(elm) != '' && $F(elm) != null){
			storage = $(elm).value;
			$(elm).value = "";
		}
	}
}

function addFields(){
	if(total_rows <= 6){
		var content = ($('src_list').innerHTML);
		content = content.gsub(/ value=\"\w+\"/,"");	
		var newRow = total_rows + 1;
		
		var lastId = total_rows.toString();
		if($('removeLink'+lastId)){
			Element.hide('removeLink'+lastId);
		}
		
		content = '<div id="field_list'+newRow+'" class="adv_fields">' + content;
		content += ' <a href="#" onClick="return removeFields(\''+newRow+'\');" id="removeLink'+newRow+'"><img src="/img/minus.jpg" width="16" height="16" border="0" alt="+" align="absmiddle" /> remove row</a>';
		content += '</div>';
		
		var lastRow = $('field_list'+lastId);
		new Insertion.After(lastRow, content);
		total_rows++;
	}
	if(total_rows == 7){
		Element.hide('addLink');
	}
	return false;
}
function removeFields(rowId){
	if($('field_list'+rowId) && total_rows >= 2){
		Element.remove('field_list'+rowId);
		total_rows--;
		var previousId = total_rows.toString();
		if($('removeLink'+previousId)){
			Element.show('removeLink'+previousId);
		}
	}
	if(total_rows <= 6){
		Element.show('addLink');
	}
	return false;
}

function goTo(url){
	window.location.href = url;
	return false;
}


function swapEm(one,two){
	//Element.hide(one);
	document.getElementById(one).style.visibility = 'hidden';
	document.getElementById(one).style.position = 'absolute';
	Element.show(two);
}


function updatePayment(val){
	Element.update('total_payment', val);
}

/*
var win=null;
function newWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	win=window.open(mypage,myname,settings);}
	return false;
}
*/

var obj_window;
function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_center, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_directories, a_bool_resizable, a_bool_fullscreen){
	a_bool_scrollbars = (a_bool_scrollbars) ? a_bool_scrollbars : 0;
	a_bool_resizable = (a_bool_resizable) ? a_bool_resizable : 0;
	a_bool_menubar = (a_bool_menubar) ? a_bool_menubar : 0;
	a_bool_toolbar = (a_bool_toolbar) ? a_bool_toolbar : 0;
	a_bool_addressbar = (a_bool_addressbar) ? a_bool_addressbar : 0;
	a_bool_statusbar = (a_bool_statusbar) ? a_bool_statusbar : 0;
	a_bool_directories = (a_bool_directories) ? a_bool_directories : 0;
	a_bool_resizable = (a_bool_resizable) ? a_bool_resizable : 0;
	a_bool_fullscreen = (a_bool_fullscreen) ? a_bool_fullscreen : 0;
	
	if(a_bool_center){
		var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
		var int_windowTop = (screen.height - a_int_windowHeight) / 2;
	} else {
		var int_windowLeft = 100;
		var int_windowTop = 100;
	}
	
	var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',directories=' + a_bool_directories + ',fullscreen=' + a_bool_fullscreen;
	if(a_bool_resizable){
		str_windowProperties += ',resizable=1';
	}
	
	obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties);
	
	if(a_bool_resizable == 0){
		obj_window.onResize = function(){
			obj_window.resizeTo(a_int_windowWidth, a_int_windowHeight);
			return false;
		};
	}
	
	/*if (parseInt(navigator.appVersion) >= 4) {
		obj_window.window.focus();
	}*/
	return false;
}