// Page Load init()
function init(){
	hideChildren();
	
	// Create new validations if necessary
	var forms = document.getElementsByTagName('form');
	for (var i=0; i < forms.length; i++) {
		if (forms[i].className.indexOf("required") > -1) {
			new Validation(forms[i].id);
		};
	};
	
}

// 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=800,width=600,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, submit_button){
	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 != "") {
			$(update).show();
			$(submit_button).disabled = true;
			return false;
		}else{
			$(update).hide();
			$(submit_button).disabled = false;
			return true;
		};
	}});

}

function emailCheckNewsletter(email, submit_button){
	var update = "email_check";
	var url =  base_url + "/bin/check_dupe_email.php";
	var params = "email=" + email + "&type=newsletter";

	var aUpdater = new Ajax.Updater(update, url, {method: "post", parameters: params, onComplete: function(transport){
		if (transport.responseText != "") {
			$(update).show();
			$(submit_button).disabled = true;
			return false;
		}else{
			$(update).hide();
			$(submit_button).disabled = false;
			return true;
		};
	}});

}

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

function isOther (value) {
	if (value=="Other") {$('heard_from_other').show()};
}

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) {
			a[i].style.cssText = "font-weight:bold;color:#000;text-decoration:none;";
		};	
	};
}

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;
}

function activateSubmit (value) {
	if (value == true) {
		$('complete_sale').disabled = false;
	}else{
		$('complete_sale').disabled = true;
	};
}