var disablePopup = false;

function toggleEditable(field_name) {
	var $field 		= $('#' + field_name);
	var $value_div	= $('#' + field_name + '_value');
	
	if($field.css('display') == 'none') {
		if($field.attr('attach')) {
			var attached_fields = $field.attr('attach').split(',');
			for(var i in attached_fields) {
				$('#' + attached_fields[i]).parents('tr:first').show();
			}
		}
		
		$field.show();
		$value_div.hide();
	}
	else {
		if($field.attr('attach')) {
			var attached_fields = $field.attr('attach').split(',');
			for(var i in attached_fields) {
				$('#' + attached_fields[i]).parents('tr:first').hide();
			}
		}
		
		if($field.attr('type') != 'password') {
			$value_div.html($field.val());
		}
		else {
			$value_div.html('********');
		}
		
		$field.hide();
		$value_div.show();
	}
}

function toggleDisabled(checkbox_obj, field_id) {
	var oCheckBox = $(checkbox_obj);
	var oField = $('#' + field_id);
	
	if(oCheckBox.attr('checked')) {
		oField.attr('disabled', false);
	}
	else {
		oField.val('');
		oField.attr('disabled', true);
	}
}

function popupWindow(url, name, height, width) {
	if(!disablePopup) {
		var height 	= typeof(height) != 'undefined' ? height : 500;
  		var width 	= typeof(width) != 'undefined' ? width : 420;
		
		var w = window.open(url, name, 'height='+height+',width='+width+',location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,directories=no,menubar=no');
		w.focus(); 
	}
	else {
		disablePopup = false;
	}
}

function validateEmail(email) {
	if(!/^[a-zA-Z0-9][_a-zA-Z0-9\.\-]*@[_a-zA-Z0-9\.\-]+\.[a-zA-Z][_a-zA-Z0-9\.\-]*$/.test(email)) {	
		alert("Please enter a valid email address.");
		return false;
	}
	
	return true;
}

function refreshParent() {
	window.opener.location.href = window.opener.location.href;
	
	if (window.opener.progressWindow) {
		window.opener.progressWindow.close()
	}
	
	window.close();
}

function checkUncheckAll(theElement, checkboxGroupName) {
	var theForm = theElement.form, z = 0;
	for(z = 0; z < theForm.length; z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name == checkboxGroupName){
			theForm[z].checked = theElement.checked;
		}
	}
}

function showsummary(id, e, text) {
	var myLayer = document.getElementById(id);
	
	if(!myLayer || myLayer == 'undefined') {
		myLayer = document.createElement('div');
		myLayer.setAttribute('id', id);
		myLayer.className = 'summary_div';
		document.body.appendChild(myLayer);
	}
	
	myLayer.innerHTML = text;

	var w = parseInt(myLayer.style.width) ;
	var h = parseInt(myLayer.style.height);
	var browserX = 0;
	var x = 0;
	var y = 0;
	
    if (e != '') {
        if (isIE()) {
            x = e.clientX;
         	y = e.clientY;
            browserX = document.body.offsetWidth - 25;

            x += document.documentElement.scrollLeft - document.body.clientLeft ; // change made
            y += document.documentElement.scrollTop - document.body.clientTop; // change made
        }
        if (!isIE()) {
            x = e.pageX;
            y = e.pageY;
            browserX = window.innerWidth - 35;
        }
    }
 
	var x1 = x + 20;                // Move out of mouse pointer
	var y1 = y + 20;

	// Keep box from going off screen
	if (x1 + w > browserX){
			x1 = browserX - w;
 	}
    myLayer.style.left = parseInt(x1)+ "px";
    myLayer.style.top = parseInt(y1) + "px";
    myLayer.style.visibility = "visible";
		
}

function hideSummary(id) {
	var myLayer = document.getElementById(id);
	myLayer.style.visibility = 'hidden';
}

function isIE() {
	return document.all;
}

function getPageScroll() {
    var xScroll, yScroll;
    
	if (self.pageYOffset) {
    	yScroll = self.pageYOffset;
      	xScroll = self.pageXOffset;
    } 
	else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      	yScroll = document.documentElement.scrollTop;
      	xScroll = document.documentElement.scrollLeft;
    } 
	else if (document.body) {// all other Explorers
      	yScroll = document.body.scrollTop;
      	xScroll = document.body.scrollLeft;	
    }
	
    return new Array(xScroll,yScroll);
}

  // adapter from getPageSize() by quirksmode.com
function getPageHeight() {
	var windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
	  	windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	  	windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) { // other Explorers
	  	windowHeight = document.body.clientHeight;
	}
	
	return windowHeight;
}

function showDialogBox($dialogBox) {
	var pageScroll = getPageScroll();

	$dialogBox.css({
	  top:	pageScroll[1] + (getPageHeight() / 4),
	  left:	pageScroll[0]
	}).fadeIn('fast'); 
}

function loadDialogBoxContents($dialogBox, params) {
	var $title 		= $('.dialog_title:first', $dialogBox);
	var $content 	= $('.content:first', $dialogBox);
	var $errors		= $('.error:last', $dialogBox);

	$.ajax({
		type: "GET",
		url: "process.php",
		data: params,
		dataType: 'json',
		beforeSend: function(XMLHttpRequest) {
			$title.html('Loading...');
			$content.html('<img src="images/ajax-loading-wheel.gif"> loading...');
			$errors.html('');
			showDialogBox($dialogBox);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) { 
			$content.html('Error while requesting data<br>Params: ' + dumpParams(params));
		},
		success: function(data) {			
			$title.html(data.elements.title);
			
			if(data.elements.num_errors) {
				for(var i in data.elements.errors) {
					$content.html(data.elements.errors[i]);
				}
			}
			else {
				$content.html(data.elements.contents);
			}
		}
	});	
}

function dumpParams(oParams) {
	var output = '';
	
	for(var i in oParams) {
		output += i + '=' + oParams[i] + '&';
	}
	
	return output;
}



function limitChars(textarea, limit, infodiv) {
	var text = textarea.value; 
	var textlength = text.length;
	var info = document.getElementById(infodiv);

	if(textlength > limit) 	{
		info.innerHTML = 'You cannot write more then '+limit+' characters!';
		textarea.value = text.substr(0,limit);
		return false;
	}
	
	else	{
		info.innerHTML = '('+ (limit - textlength) +' chars)';
		return true;
	}
}

