var formid = 'nhb';
SqueezeBox.parsers.swf = function(preset) {
  return (preset || this.url.test(/\.swf/)) ? this.url : false;
};
 
SqueezeBox.handlers.swf = function(url) {
  var size = this.options.size;
  return new Swiff(url, {
    id: 'sbox-swf',
    width: size.x,
    height: size.y,
    params: {
      allowScriptAccess:'always',
      allowFullScreen:'true'      
    }
  });
};

window.addEvent('domready', function() { 

	// GFX
	var inputFields = $$('#'+formid+' input[type=text], #'+formid+' select, #'+formid+' input[type=checkbox], #'+formid+' input[type=radio]');
	inputFields.each(function(element) { 
		element.addEvent('focus', function(event){ 
			element.getParent('tr').tween('background-color', '#E0E8EF'); 
		}); 
		element.addEvent('blur', function(event){ 
			element.getParent('tr').tween('background-color', '#fff'); 
		});
	});
	
	var textFields = $$('#'+formid+' textarea');
	textFields.each(function(element) { 
		element.addEvent('focus', function(event){ 
			element.getParent('tr').tween('background-color', '#E0E8EF'); 
			element.tween('height', 100); 
		}); 
		element.addEvent('blur', function(event){ 
			element.getParent('tr').tween('background-color', '#fff'); 
		});
	});
	
	var tipyourname = $('tip-yourname');
	var tipcolemail = $('tip-colemail');
	var tipshowsend = $('tip-showsend');
	var tipcontent = $('tip-content');
	var tipinnercontent = $('tip-innercontent');
	var clearFields = $$('.clearfield');
	clearFields.each(function(element) {
		element.addEvent('focus',function(event){
			element.value="";
		});
	});
	if(tipyourname) {
		tipyourname.addEvent('focus', function(event){ 
			if (tipyourname.value == "Dit navn") {
				tipyourname.value="";
			}
		}); 
		tipyourname.addEvent('blur', function(event){ 
			if (tipyourname.value == "") {
				tipyourname.value="Dit navn";
			}
		});
	}
	if(tipcolemail) {
		tipcolemail.addEvent('focus', function(event){ 
			if (tipcolemail.value == "Din kollegas e-mail") {
				tipcolemail.value="";
			}
		}); 
		tipcolemail.addEvent('blur', function(event){ 
			if (tipcolemail.value == "") {
				tipcolemail.value="Din kollegas e-mail";
			}
		}); 
	}
	if(tipshowsend) {
		tipshowsend.addEvent('click', function(event){
			event.stop();
			// height = tipinnercontent.offsetHeight;
			curval = tipcontent.getStyle('height');
			if (curval == "0px") {
				tipcontent.setStyle('height', 'auto');
			} else {
				tipcontent.setStyle('height', 0);
			}
		});
	}
	// LANG
	var errorLang = []
	if (typeof errorMessages != "undefined") {
		errorLang = errorMessages;
	} else { 
		errorLang[0] = "Du skal udfylde dette felt.";
		errorLang[1] = "Denne e-mail adresse blev ikke godkendt.";
		errorLang[2] = "Du har ikke afkrydset denne tjek-boks.";
		errorLang[3] = "Du har ikke valgt noget.";
		errorLang[4] = "Dette er et unormalt telefonnummer - er du sikker?";
		errorLang[5] = "henter...";
		errorLang[6] = "Advarsel: ";
	}
	// VALIDATION
		// TIP Validation
		if ($('nhb-tip')) {
			tipvalidator = new Form.Validator.Inline('nhb-tip');
			tipvalidator.errorPrefix = errorLang[6];
			tipvalidator.add('valEmail', {
				errorMsg: errorLang[1],
				test: function(element){
					email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
					val = element.value;
					if(!email.test(val)) return false;
					else return true;
				}
			});
			tipvalidator.add('required', {
				errorMsg: errorLang[0],
				test: function(element){
					if(element.value.length ==0) return false;
					else return true;
				}
			});
		}
			
		
		// CONTACT Validation
		if ($('nhb')) {
			contactvalidator = new Form.Validator.Inline('nhb');
			contactvalidator.errorPrefix = errorLang[6];
			contactvalidator.warningPrefix = "";
			contactvalidator.add('required', {
				errorMsg: errorLang[0],
				test: function(element){
					if(element.value.length ==0) return false;
					else return true;
				}
			});
			contactvalidator.add('valEmail', {
				errorMsg: errorLang[1],
				test: function(element){
					email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
					val = element.value;
					if(!email.test(val)) return false;
					else return true;
				}
			});
			contactvalidator.add('valCheckbox', {
				errorMsg: errorLang[2],
				test: function(element){
					if(!element.checked) return false;
					else return true;
				}
			});
			contactvalidator.add('valSelect', {
				errorMsg: errorLang[3],
				test: function(element){
					val = element.value;
					if(val == "0") return false;
					else return true;
				}
			});
			contactvalidator.add('valPhone', {
				errorMsg: errorLang[4],
				test: function(element){
					phone = /^\+?[0-9 ()-]+[0-9]$/;
					val = element.value.replace(' ', '');
					var numdigits = 0;
					for (var j=0; j<val.length; j++)
						if (val.charAt(j)>='0' && val.charAt(j)<='9') numdigits++;
		
					if(!phone.test(val) || 8>numdigits) return false;
					if(numdigits>12) return false;
					else return true;
				}
			});
		}
	/**/
	//AJAX
	if ($('nhb-tip')) {
		$('nhb-tip').addEvent('submit', function(e) {
			//Prevents the default submit event from loading a new page.
			e.stop();
			//Empty the log and show the spinning indicator.
			var log = $('tip_log_res');
			if(tipvalidator.validate()) {
				$('tip-iframe').value = "true";
				tipvalidator.reset();
				log.empty().set('html', '<div style="padding:8px;">'+errorLang[5]+'</div>');
				log.morph({opacity:[0,1]});
				//Set the options of the form's Request handler. 
				//("this" refers to the $('myForm') element).
				this.set('send', {onComplete: function(response) { 
					log.removeClass('ajax-loading');
					log.set('html', response);
					//if ($('eventUpdateSuccess')) {
						// GET NEW SPAMID THROUGH AJAX REQUEST
						var spamid = new Request({
							url:'ajax_getspamid.asp',
							method:'post',
							onSuccess: function(txt){
								txtarr = txt.split(";;;");
								$('tipspamid').value = txtarr[0];
								$('tipdate').value = txtarr[1];
							},
							onFailure: function(){}
						});
						spamid.send();
					//}
				}});
				//Send the form.
				this.send();
			} else {
				log.set('html', '');
				log.setStyle('display','none');
			}
		});
	}
	if ($('nhb')) {
		$('nhb').addEvent('submit', function(e) {
			var log = $('nhb_log_res');
//			if(contactvalidator.validate()) {
				contactvalidator.reset();
				log.empty();
				// SEND
//			} else {
//				e.stop();
//				log.set('html', '');
//				log.morph({height:0});
//			}
		});
	}/**/
    /*=SQUEEZEBOX*/
    SqueezeBox.assign($$('a.modal-button'), { parse: 'rel' });
    SqueezeBox.assign($$('a.modal-image'));
    SqueezeBox.assign($$('a.modal-contact'), {handler: 'iframe', size: {x: 330, y:525}});
    SqueezeBox.assign($$('a.modal-material'), {handler: 'iframe', size: {x: 520, y:525}});/**/
});
