/* page.js|customers */
// inits script
function initPageJs() {
	iframeCalc();
	accessibleInputs();
}

var iFrames = document.getElementsByTagName('iframe');

function iframeCalc(){
	if ($.browser.safari || $.browser.opera){
		// Start timer when loaded.
		$('iframe').load(function(){
			setTimeout(iResize, 0);
		});

		// Safari and Opera need a kick-start.
		for (var i = 0, j = iFrames.length; i < j; i++){
			var iSource = iFrames[i].src;
			iFrames[i].src = '';
			iFrames[i].src = iSource;
		}
	} else {
	// For other good browsers.
		$('iframe').load(function() {
			// Set inline style to equal the body height of the iframed content.
			this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
		});
	}
}

function iResize(){
	// Iterate through all iframes in the page.
	for (var i = 0, j = iFrames.length; i < j; i++){
		// Set inline style to equal the body height of the iframed content.
		iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
	}
}

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

/* accessibleInputs.js|global */
//Accessible Inputs (requires jQuery)

// moves labels value to inputs if class 'mdValueToInput' is present & then adds focus/blur to inputs
function accessibleInputs(){
	$("label.mdValueToInput[for]").each(function(i){
		// fill input fields with labeltext - html tags
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var newVal = this.innerHTML.replace(regexp,"");
		//if el is type=input or textarea
		if($("#"+this.htmlFor).is("input") || $("#"+this.htmlFor).is("textarea")){
			if(($("#"+this.htmlFor).val() == "") || ($("#"+this.htmlFor).val() == newVal)){
				$("#"+this.htmlFor).attr("value",newVal);
			}
			// create onclick/blur functionality
			$("#"+this.htmlFor).focus(function(){if(this.value == newVal) this.value = "";});
			$("#"+this.htmlFor).blur(function(){if(this.value == "") this.value = newVal;});
		// if el is select	
		} else if($("#"+this.htmlFor).is("select")){
			var orgOptions = $("#"+this.htmlFor).html();
			var newOptions = '<option value="">'+newVal+'</option>'+orgOptions;
			// IE special Kung Fu
			if($.browser.msie){
				var go=0;
				$("#"+this.htmlFor).find("option").each(function(i){
					if($(this).get(0).defaultSelected){
						go=1;
					}
				})
				if(go==0){
					newOptions = newOptions.replace(/selected>/g,">");
				}
			}
			$("#"+this.htmlFor).html(newOptions);
		}
		// hide label
		$(this).hide();
	})
	cleanForms();
}

// makes sure that label values are not submitted to forms
function cleanForms(){
	$("form:has(label.mdValueToInput)").submit(function(){
		$("label.mdValueToInput[for]").each(function(){
			// check if value is same as label
			var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
			var newVal = this.innerHTML.replace(regexp,"");
			if($("#"+this.htmlFor).attr("value") == newVal){
				$("#"+this.htmlFor).attr("value","");
			}
		})
	})
}

