// Page Load init()
function init(){
	hideChildren();
}

// This function will hide all lists with a class of child_list
function hideChildren(){
	
	var children = document.getElementsByTagName('ul');
	for (var i=0; i < children.length; i++) {
		if (children[i].className == "child_list" ) {
			children[i].style.cssText = "display:none;";
		};
	};
}

// This function will hide OTHER lists that don't belong to the parent that was just clicked
function hideOthers(parent){

	var children = document.getElementsByTagName('ul');
	for (var i=0; i < children.length; i++) {
		if (children[i].className == "child_list" && children[i].id != parent + "_child") {
			Element.toggle(children[i].id);
		};
	};
}
	
// This function will display the child_list and hide others
function showChildren(parent){
	hideOthers(parent);
	var child_list = $(parent + '_child'); // The $('') method is a prototype method that's the same as document.getElementById()
	Element.toggle(child_list.id); // Scriptaculous effect to show the child list
}

// show or hide elements onclick
function showHide(id) {
	var element = $(id)
	/*
	if (element.style.display == "none") {
		new Effect.BlindDown(element.id);
	} else {
		new Effect.BlindUp(element.id);
	}
	*/
	Element.toggle(id)
}

// hide all divs with a class of "solo"
function hideSolos(){

	var divs = document.getElementsByTagName('div');
	for (var i=0; i < divs.length; i++) {
		if (divs[i].className == "solo") {
			divs[i].style.display = "none";
			//new Effect.BlindUp(divs[i].id);
		};
	};
}

// alternate table row class (background color)
function alternateRows(){

	var rows = document.getElementsByTagName('tr');
	for (var i=0; i < rows.length; i++) {
		if (i % 2) {
					rows[i].className = "odd";
				} else {
					rows[i].className = "even";
				};
	};
}

// show or hide elements one at a time
function showHideSolo(id){
	if (id != "") {
		hideSolos();
		//new Effect.BlindDown(id);
		Element.toggle(id);
		document.getElementById('media_type_select').value = id;
	};
}

// popup window for guarantee
var newwindow;
function popwindow(url){
	newwindow=window.open(url,'name','height=600,width=370,scrollbars=1');
	if (window.focus) {newwindow.focus()}
}

// Form Validation
function validateForm(id){
	var validator = new Validation(id, {useTitles : true});
	return validator.validate();
}

// Check for Dupe E-Mail
function emailCheck(email){
	var update = "email_check";
	var url =  base_url + "/bin/check_dupe_email.php";
	var params = "email=" + email;

	var aUpdater = new Ajax.Updater(update, url, {method: "post", parameters: params, onComplete: function(transport){
		if (transport.responseText != "") {
			Element.toggle(update.id)
			return false;
		}else{
			return true;
		};
	}});

}

function isUSA(value, type){
	if (value == "United States") {
		$(type + '_province').disabled = true;
		$(type + '_state').disabled = false;
	}else{
		$(type + '_province').disabled = false;
		$(type + '_state').disabled = true;
	};
}

function checkSpelling(id){
	var area = $(id);
	var speller = new spellChecker(area);
	speller.openChecker();
	return false;
}

// show active link
function showActiveLink(){
	var a = document.getElementsByTagName('a');
	var url = location.href;
	
	for (var i=0; i < a.length; i++) {
		if (a[i].href == location.href) {
			if (a[i].className == 'header_nav') {
				a[i].style.cssText = "color:#112f62;";
			} else if (a[i].className == 'moving_subnav_button') {
				a[i].style.cssText = "color:#112f62;font-weight:bold;";
			}
			else{
				a[i].style.cssText = "font-weight:bold;color:#fbbd23;";
			}
		}
		
		//moving section nav
		else if (url.search('/moving/rates') != '-1' || url.search('/moving/estimate-info') != '-1' || url.search('/moving/estimate-form') != '-1' || url.search('/moving/insurance') != '-1') {
			$('nav_moving').style.cssText = "color:#112f62;";
		}
		else if (url.search('/moving/out-of-state') != '-1' || url.search('/moving/os-estimate-info') != '-1' || url.search('/moving/os-estimate-form') != '-1' || url.search('/moving/os-hints') != '-1' || url.search('/moving/os-insurance') != '-1') {
			$('moving_tabs').className = "moving_tabs_2";
			$('nav_moving').style.cssText = "color:#112f62;";
		}
		else if (url.search('/moving/office') != '-1' || url.search('/moving/office-estimate') != '-1' || url.search('/moving/office-tips') != '-1') {
			$('moving_tabs').className = "moving_tabs_3";
			$('nav_moving').style.cssText = "color:#112f62;";
		}
		else if (url.search('/moving/specialty') != '-1') {
			$('moving_tabs').className = "moving_tabs_4";
			$('nav_moving').style.cssText = "color:#112f62;";
		}
		
		//moving guide section nav
		if (url.search('/page/checklist') != '-1') {
			$('moving_guide_tabs').className = "moving_guide_tabs_2";
			$('nav_moving_guide').style.cssText = "color:#112f62;";
		}
		else if (url.search('/page/resources') != '-1' || url.search('/page/links') != '-1' || url.search('/page/service-providers') != '-1') {
			$('moving_guide_tabs').className = "moving_guide_tabs_3";
			$('nav_moving_guide').style.cssText = "color:#112f62;";
		}
		
		//packing section nav
		if (url.search('/page/supplies') != '-1') {
			$('packing_tabs').className = "packing_tabs_2";
			$('nav_packing').style.cssText = "color:#112f62;";
		}
		else if (url.search('/page/labels') != '-1') {
			$('packing_tabs').className = "packing_tabs_3";
			$('nav_packing').style.cssText = "color:#112f62;";
		}
		else if (url.search('/page/no-move') != '-1') {
			$('packing_tabs').className = "packing_tabs_4";
			$('nav_packing').style.cssText = "color:#112f62;";
		}
		
		//about us section nav
		if (url.search('/page/credentials') != '-1') {
			$('about_us_tabs').className = "about_us_tabs_2";
			$('nav_about_us').style.cssText = "color:#112f62;";
		}
		else if (url.search('/page/testimonials') != '-1') {
			$('about_us_tabs').className = "about_us_tabs_3";
			$('nav_about_us').style.cssText = "color:#112f62;";
		}
		else if (url.search('/page/our-family') != '-1') {
			$('about_us_tabs').className = "about_us_tabs_4";
			$('nav_about_us').style.cssText = "color:#112f62;";
		}
			
	}
}

function showActiveAdminLink(){
	var a = document.getElementsByTagName('a');
	var url = location.href;
	
	for (var i=0; i < a.length; i++) {
		if (a[i].href == url) {

			switch(a[i].className){
				case('nav_link'):
					a[i].className = 'active_nav_link';
					break
				case('pager_link'):
					a[i].className = 'active_pager_link';
					break
				case('image'):
					// do nothing
					break
				default:
					a[i].className = 'active_generic_link';
			}
		}
		else if (url.search('/admin-message/') != '-1') {
			$('nav_admin_message').className = 'active_nav_link';
		}
		else if (url.search('/user/') != '-1') {
			$('nav_user').className = 'active_nav_link';
		}
		else if (url.search('/admin-user/') != '-1') {
			$('nav_admin_user').className = 'active_nav_link';
		}
		
	};
}

function unCompProduct(product_id, user_id){
	var params = "product_id=" + product_id + "&user_id=" + user_id;
	var url = base_url + "/bin/remove_comped_product.php";
	var req = new Ajax.Request(url, {method:"post", parameters:params});
	new Effect.Fade($(product_id + '_removal').id);
	return false;
}