/* Developed by Dubbele:punt Design - www.dubbelepunt.com */

/*** General helper functions ***/
	// Fix IE6 CSS dropdown menu (SuckerFish)
	sfHover = function(){
		if(document.getElementById('menu')){
			var sfEls = document.getElementById('menu').getElementsByTagName('li');
			for(var i = 0; i < sfEls.length; i++){
				sfEls[i].onmouseover = function(){
					this.className += ' sfhover';
				}
				sfEls[i].onmouseout = function(){
					this.className = this.className.replace(new RegExp(' sfhover\\b'), '');
				}
			}
		}
	}
	
	// Break frameset to avoid session problems
	if(window.self.location.href != window.top.location.href){
		// Only allow Google to frame
		if(window.top.location.href.indexOf('google') == -1){
			window.top.location.replace(window.self.location); 
		}
	}

	// Fix IE6 background image flicker
	try{
		document.execCommand('BackgroundImageCache', false, true);
	}
	catch(e){};
	


/*** Navigation functions ***/
	// Redirect to url
	function goTo(url){
		location.href = url;
	}
	
	// Swap images
	function swap(target, img){
		if(document.images[target]){
			document.images[target].src = img;
		}
	}



/*** Show/hide functions ***/
	// Show/hide DIV
	function showhidediv(target){
		layer_ref = document.getElementById(target);
		layer_ref.style.display = (layer_ref.style.display == 'block') ? 'none' : 'block';
	}
	
	// Show DIV
	function showdiv(target){
		layer_ref = document.getElementById(target);
		layer_ref.style.display = 'block';
	}
	
	// Hide DIV
	function hidediv(target){
		layer_ref = document.getElementById(target);
		layer_ref.style.display = 'none';
	}
	
	// Show target DIV
	function showtarget(target){
		// Close previous open DIV
		if(undefined === window.previous){
			previous = '';
		}else{
			layer_ref = document.getElementById(previous);
			layer_ref.style.display = 'none';
		}
	
		// Open current DIV
		layer_ref = document.getElementById(target);
		layer_ref.style.display = (layer_ref.style.display == 'block') ? 'none' : 'block';
		
		// Save current DIV
		previous = target;
	}



/*** External links ***/
	// Handle external links
	function externalLinks(){ 
		// Get links
		if (!document.getElementsByTagName) return; 
		var anchors = document.getElementsByTagName('a'); 
		
		// Loop through links
		for(var i = 0; i < anchors.length; i++) { 
			var anchor = anchors[i]; 
			
			// Add target to external link
			if(anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external'){
	
				// Add class only to non-image links
				if($(anchor).childElements() != '[object]' && $(anchor).childElements() != '[object HTMLImageElement]'){		
					anchor.className = 'external';
				}
				anchor.target    = '_blank'; 
				anchor.title     = anchor.title + ' [Pop-up]';
			}
		} 
	} 



/*** Validator functions ***/
	function validate(field){
		// Set script URL and set properties
		var url        = './includes/validator/validator.php';
		var properties = $w($(field).className);
		var check      = properties[1];
		var minimum    = properties[2]     ? properties[2] : 1;
		var maximum    = properties[3]     ? properties[3] : 255;
		var required   = properties[4]     ? properties[4] : true;
		var extra      = properties[5]     ? properties[5] : '';
		var extraValue = extra && $(extra) ? $F(extra)     : extra;
		
		// Make request
		new Ajax.Request(url, {
			method: 'post', 
			parameters: {
				id:       $(field).id,
				value:    $F(field),
				check:    check,
				minimum:  minimum,
				maximum:  maximum,
				required: required,
				extra:    extraValue
			},
			onSuccess: function(transport){ 
				if(transport.responseText == 'true'){
					// Set classnames
					$(field).className = 'input '+check+' '+minimum+' '+maximum+' '+required+' '+extra;
				}else if(transport.responseText == 'false'){
					// Set classnames
					$(field).className = 'input_error '+check+' '+minimum+' '+maximum+' '+required+' '+extra;
				}
			}
		});
	}
	
	// Handle validation
	function validateFields(){ 
		// Get input fields
		$$('input').each(function(input) {
			// Add the validate function to the field
			if(input.className){
				input.onblur = function(){
					validate(this);
				}
			}
			
		});
		
		// If the country changes, check the country dependant fields
		if($('country')){
			$('country').onchange = function(){
				// Only validate the fields that exist
				$('postalcode') ? validate('postalcode') : '';
				$('phone')      ? validate('phone')      : '';
				$('fax')        ? validate('fax')        : '';
			}
		}
		
		// If the creditcard type changes, check the card number field
		if($('creditcard_number') && $('creditcard_type')){
			$('creditcard_type').onchange = function(){
				validate('creditcard_number');
			}
		}
		
		// If the password changes, check the password validation
		if($('password') && $('password2')){
			$('password').onblur = function(){
				validate('password2');
			}
		}
	}
	
	
	
/*** Form functions ***/
	// Select/deselect all checkboxes
	function checkAll(form){
		var checked = (form.check_all.checked) ? true : false;
		var option  = form.check;
		
		// Loop through checkboxes
		if(option.length != undefined){
			for(var i = 0; i < option.length ; i++){
				option[i].checked = checked;
			}
		}else{
			option.checked = checked;
		}
	}
	
	// Select one checkbox
	function checkOne(id, message, form){
		// Confirm
		if(confirm(message)){
			var option  = form.check;
		
			// Loop through checkboxes
			if(option.length != undefined){
				for(var i = 0; i < option.length ; i++){
					option[i].checked = false;
				}
			}
			
			// Check this checkbox
			if(option.length != undefined){
				option[id].checked = true
			}else{
				option.checked = true
			}
				
			// Submit form
			form.submit();
		}
	}

	function encryptLogin(){ 
		// Set script URL and variables
		var url        = './includes/authenticator/salt_pepper.php';
		var username   = $F('username').strip();
		var password   = $F('password').strip();
		var challenge  = $F('challenge').strip();
		var response   = '';
		var pepper     = '';
		var salt       = '';
		
		// Make request
		new Ajax.Request(url, {
			method: 'post', 
			parameters: {
				username: username
			},
			onSuccess: function(transport){ 
				// Get salt and pepper
				response = transport.responseText.split(", ");
				salt     = response[0];
				pepper   = response[1];
				
				// Update form fields
				$('password').value   = hex_hmac_md5(hex_md5(salt + password + pepper), challenge);
				$('encryption').value = 'javascript';
			}
		});
		
		// Prevent submit
		return false;
	}
	
	function preventDoubleSubmit() {
		// loop through all forms
		$$('form').each(function(form) {
			
			// Disable all submit buttons after a submit
			form.observe('submit', function() {
				form.getInputs('submit').each(function(submit) {
					submit.disable();
				});
			}); 
		});
	}

/*** Execute functions ***/
	Event.observe(window, 'load', sfHover,             false);
	Event.observe(window, 'load', externalLinks,       false);
	Event.observe(window, 'load', validateFields,      false);
	Event.observe(window, 'load', preventDoubleSubmit, false);
	Event.observe(window, 'load', function(){
		if($('login')){
			Event.observe('login', 'submit', encryptLogin, false); 
		}
	}); 

/* Developed by Dubbele:punt Design - www.dubbelepunt.com */
