function formatPhone (textField) {
  textField.value = textField.value.replace(/^(?:1-)?\(?(\d{3})[\)-]?[\s]?(\d{3})[\)\s-]?(\d{4})$/, "$1-$2-$3");
}

function showShadeDiv() {
  // show shade and dialog box
  document.getElementById("shade_div").style.display = 'block';
}

function hideShadeDiv() {
  // show shade and dialog box
  document.getElementById("shade_div").style.display = 'none';
}

function showShadeDivDialog(ajaxUrl, zones) {
	showShadeDiv();
	if (zones == null) zones = "dialog";
	ajaxAnywhere.getAJAX(ajaxUrl, zones);	
}

function showYesNoDialog(actionUrl, objectId) {	
	showShadeDiv();
	document.getElementById("yes_no_div").style.display = 'block';
	length_actionUrl = actionUrl.length;
	last_char_actionUrl = actionUrl.charAt(length_actionUrl-1);
	if (last_char_actionUrl == '&') {
		document.getElementById("dialog_yes").href=actionUrl+"objectId="+objectId;
	} else {
		document.getElementById("dialog_yes").href=actionUrl+"?objectId="+objectId;
	}
	
}

function showYesNoDialogUrl(actionUrl) {	
	showShadeDiv();
	document.getElementById("yes_no_div").style.display = 'block';	
	document.getElementById("dialog_yes").href=actionUrl;
}

function hideYesNoDialog() {
	hideShadeDiv();
	document.getElementById("yes_no_div").style.display = 'none';
	document.getElementById("dialog_yes").href='';
}

function hideShadeDivDialog() {
	hideShadeDiv();
	var dialog = document.getElementById('aazone.dialog');
	if (dialog != null) dialog.innerHTML = "";
}

function hideConfirmation() {
	var dialog = document.getElementById('aazone.confirmation');
	if (dialog != null) dialog.innerHTML = "";
}

function showAlert(message, headerWidth) {	
	showShadeDiv();
	document.getElementById("alert_div").style.display = 'block';	
	document.getElementById("alert_mes").innerHTML = message;	
	if(headerWidth != undefined) {
		document.getElementById("alert_header_div").style.width = headerWidth;
	}	
}
function hideAlert() {	
	hideShadeDiv();
	document.getElementById("alert_div").style.display = 'none';	
}

function _showAlert(message, headerWidth, locationHref) {
	showShadeDiv();
	document.getElementById("_alert_div").style.display = 'block';	
	document.getElementById("_alert_mes").innerHTML = message;
	document.getElementById("_alert_button").href =  locationHref;
	if(headerWidth != undefined) {
		_headerWidth = headerWidth - 10;
		document.getElementById("_alert_header_div").style.width = _headerWidth + 'px';
		document.getElementById("_alert_div").style.width = headerWidth + 'px';
	}	
}

function _showAlertLogin(message, headerWidth) {
	showShadeDiv();
	document.getElementById("_alert_div_login").style.display = 'block';	
	document.getElementById("_alert_mes_login").innerHTML = message;
	if(headerWidth != undefined) {
		_headerWidth = headerWidth - 10;
		document.getElementById("_alert_header_div_login").style.width = _headerWidth + 'px';
		document.getElementById("_alert_div_login").style.width = headerWidth + 'px';
	}	
}

function _hideAlert() {	
	hideShadeDiv();
	document.getElementById("_alert_div").style.display = 'none';	
}

function prepareTextFieldValues(){
	var inputElemList = document.getElementsByTagName('input');  
	for (var i = 0; i < inputElemList.length; i++) {
		var inputEl = inputElemList[i]; 
		if (inputEl.getAttribute('type') != 'text' || inputEl.getAttribute('value') == null) {			
			continue;
		}
		inputEl.value = inputEl.getAttribute('value').replace(/&(lt|gt);/g, function (strMatch, p1){
		 	return (p1 == "lt")? "<" : ">";
		});
	} 
}

function  textCounter(field, countfield, maxlimit) {
 if (field.value.length > maxlimit) // if too long...this trims it!
  field.value = field.value.substring(0, maxlimit);
 // otherwise, update 'characters left' counter
 else 
  countfield.value = maxlimit - field.value.length;
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}

function _setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + "; path=/";
	document.cookie=c_name + "=" + c_value;
}

function _trim (s) {
  var l=0;
  var r=s.length -1;
  while(l < s.length && s[l] == ' ') {
	  l++;
  }
  while(r > l && s[r] == ' ') {
	  r-=1;
  }
  return s.substring(l, r+1);
}

function getPositionX(obj){
	   var leftValue = 0;      
	   while(obj){       
	    leftValue+= obj.offsetLeft;
	    obj= obj.offsetParent;   
	   }								      
	   return leftValue;
	  }

	  function getPositionY(obj){
	   var topValue = 0;      
	   while(obj){
	    topValue+= obj.offsetTop;
	    obj= obj.offsetParent;   
	   }								   
	   return topValue;
	  }
	  
	  function stripXmlSpecialChars(inputText) {
		  var outputText = inputText.replace(/amp/g, "&")
		  	.replace(/quote]/g, "qote]").replace(/quot/g, "\"").replace(/qote]/g, "quote]")
			.replace(/apos/g, "\'")
			.replace(/lt/g, "<")
			.replace(/gt/g, ">");
		  
		  return outputText;
	  }
