/*
	dom.functions.js
	DOM Altering Javscript Functions

	Created: Sept. 27, 2007
	Creator: Matt Kircher, Mainline Media
*/


//GENERAL DOM STUFF
//----------------------------------------------------------

function faqJump(){
	var s = document.getElementById('faq-quick-jump');
	var section = s.options[s.selectedIndex].value;
	window.location.hash = section;
}

function openPopUp(linkURL) {
	pop = window.open(linkURL,'popup','width=600,height=500,location=no,menubar=no,scrollbars=yes,status=no,titlebar=no,toolbar=no,resizable=yes');
}

function initPopUps(){
	// check to see that the browser supports the getElementsByTagName method
	// if not, exit the loop 
	if (!document.getElementsByTagName) {
		return false; 
	} 
	// create an array of objects of each link in the document 
	var popuplinks = document.getElementsByTagName("a");
	// loop through each of these links (anchor tags) 	
	for (var i=0; i < popuplinks.length; i++) {	
		// if the link has a class of "popup"...	
		if (popuplinks[i].getAttribute("class") == "popup") {	
			// add an onclick event on the fly to pass the href attribute	
			// of the link to our second function, openPopUp 	
			popuplinks[i].onclick = function() {	
			openPopUp(this.getAttribute("href"));	
			return false; 	
			} 	
		}
	}	
}

//FORM DOM STUFF
//----------------------------------------------------------

function toggleNewCustomerInfo(element){
	var div = document.getElementById('new-customer-info');
	if(element.name == "mailing_list_type"){
		div.style.display = (element.value == "New")?"block":"none";
	}
}

function toggleSample(row){
	var form = document.getElementById('quoteForm');
	var val = null;
	
	if(form){
		val = (form.elements['use_sample'+row].checked)?false:true;
		
		for(x=0; x<row; x++){
			if(form.elements['use_sample'+x]){
				if(!val){
					form.elements['use_sample'+x].checked = !val;
					form.elements['quote_size'+x].disabled = val;
					form.elements['quote_type'+x].disabled = val;
					form.elements['quote_quantity'+x].disabled = val;
				}
			}
		}
		form.elements['quote_size'+row].disabled = val;
		form.elements['quote_type'+row].disabled = val;
		form.elements['quote_quantity'+row].disabled = val;
	}
}

function toggleOtherRefBox(elementActedOn){
	var span = document.getElementById('other_ref_type');
	try{
		if(elementActedOn){
			var e = elementActedOn;
			if(e.checked){
				var textbox = document.createElement('input');
				textbox.id = "ref_type_other";
				textbox.name = "ref_type_other";
				textbox.size = "20";
				textbox.maxLength = "255";
				textbox.value = "(please specify)";
				
				span.innerHTML = "";
				span.appendChild(textbox);
			}
			
		} else {
			var textbox = document.getElementById('ref_type_other');
			span.removeChild(textbox);
			span.innerHTML = "Other";
		}
	} catch(e){
		return false;	
	}
}

function toggleOtherInterestBox(elementActedOn){
	var span = document.getElementById('other_interests_type');
	try{
		if(elementActedOn){
			var e = elementActedOn;
			if(e.checked){
				var textbox = document.createElement('input');
				textbox.id = "interests_other";
				textbox.name = "interests_other";
				textbox.size = "20";
				textbox.maxLength = "255";
				textbox.value = "(please specify)";
				
				span.innerHTML = "";
				span.appendChild(textbox);
			} else {
				var textbox = document.getElementById('interests_other');
				span.removeChild(textbox);
				span.innerHTML = "Other Services";
			}			
		}
	} catch(e){
		return false;	
	}
}

//** USES JQuery Library **//
function setupSurveys(){
	
	if($('#newCustomerSurveyForm').length > 0){
		
		var ta_text = "Please explain...";
		$('#q1text, #q2text, #q3text, #q4text, #q5text').text(ta_text);
		
		$('#q1b, #q2b, #q3b, #q4b, #q5b')
		.bind('click', function(){ $(this).parent().find('textarea').show().focus(); });
		
		$('#q1a, #q2a, #q3a, #q4a, #q5a')
		.bind('click', function(){ $(this).parent().find('textarea').hide(); });
	}
}


//INITIALIZATION
//----------------------------------------------------------

$(document).ready(function(){
	setupSurveys();
});

window.onload = function() {
	initPopUps(); 
}