// General generic (cross browser and cross site) js functions


var ns4 = document.layers;
var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) 
	||(navigator.userAgent.indexOf("Opera/5")!=-1);
var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) 
	||(navigator.userAgent.indexOf("Opera/6")!=-1);
var agt=navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);
var ie = (agt.indexOf("msie") != -1); 
var mac_ie = mac && ie;


/**
 * Format phone number function
 */
function formatPhone(curPhone) {
	curPhone.value = formatPhoneStr(curPhone.value);
}

/**
 * Returns a formatted phone number -- source: Software Anywhere Team
 */
function formatPhoneStr(phoneNumber) {
	var tempPhone = phoneNumber.replace(/[^0-9xX]/g,"");
	tempPhone = tempPhone.replace(/[xX]/g,"x");
	var extension = ""
	if(tempPhone.indexOf("x") > -1) {
		extension = " "+tempPhone.substr(tempPhone.indexOf("x"));
		tempPhone = tempPhone.substr(0,tempPhone.indexOf("x"));
	}
	switch(tempPhone.length) {
		case(10):
			return tempPhone.replace(/(...)(...)(....)/g,"($1) $2-$3")+extension;
		case(11):
			if(tempPhone.substr(0,1) == "1") {
				return tempPhone.substr(1).replace(/(...)(...)(....)/g,"($1) $2-$3")+extension;
			}
			break;
		default:
	}
	return phoneNumber;
}

function isValidPhone(curPhone) {
	return isValidPhoneStr(curPhone.value);
}

function isValidPhoneStr(phoneNumber) {
	var re = /(...) (...)-(....)/;
	return re.test(phoneNumber);
}

function checkValidSession() {
	var url = "/youmail/validSessionCheck.do";
    var pars = "";
	var content;
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars,
			asynchronous: true,
			onComplete: function(originalRequest) {
				//put returned XML in the textarea
				content = originalRequest.responseText;
				if (content.indexOf('false') >= 0) {
					// stop if the user had started recording
					// we need to notify the user here
					top.alert('Your session has timed out');
					top.location = top.location;
				}				
			}
		});
}

/**
 * Returns the first child that matches the searchId,
 * earch immediate children first, then recursively for each child.
 */
function getChildElementById(searchId, parent)
{
  if (parent == null) {
     return document.getElementById(searchId);
  }
  if (parent.id == searchId) {
     return parent;
  }
  var allMyChildren = parent.childNodes;
  var len = allMyChildren.length
  for (var i = 0; i < len; i++) {
      var aNode = allMyChildren[i];
      if (aNode.id == searchId) {
          return aNode;
      }
  }
  for (var i = 0; i < len; i++) {
      var foundNode = getChildElementById(searchId, allMyChildren[i]);
      if (foundNode != null) {
          return foundNode;
      }
  }
  return null;  
}


/**
 * Returns the first child that matches the className,
 * search immediate children first, then recursively for each child.
 */
function getElementByClassName(className, parent)
{
  if (parent == null) {
     parent = document.body;
  }
  if (parent.className == className) {
     return parent;
  }
  var allMyChildren = parent.childNodes;
  var len = allMyChildren.length
  for (var i = 0; i < len; i++) {
      var aNode = allMyChildren[i];
      if (aNode.className == className) {
          return aNode;
      }
  }
  for (var i = 0; i < len; i++) {
      var foundNode = getElementByClassName(className, allMyChildren[i]);
      if (foundNode != null) {
          return foundNode;
      }
  }
  return null;  
}


/**
 * Removes the first instance of removeClassName from the element removeFrom,
 * also remove one space if the removeClassName was surrounded by spaces.
 */
function removeCssClassName(removeClassName, removeFrom)
{
    var curClassName = removeFrom.className;
    var index = curClassName.indexOf(removeClassName);
	if (index > -1) {
		var firstPart = curClassName.substr(0,index);
		var secondPart = curClassName.substr(index+removeClassName.length, curClassName.length);
		
		// check if there are spaces around the removed className, remove one space
		if (firstPart.length > 0 && secondPart.length > 0 
			&& firstPart.charAt(firstPart.length - 1) == ' ' && secondPart.charAt(0) == ' ') {
			secondPart = secondPart.substr(1, secondPart.length);
		}
		removeFrom.className = firstPart + secondPart;
	}
}

/**
 * Changes the elementId to block if none and vice versa.
 */
function toggleDisplay(elementId)
{
	var elem = document.getElementById(elementId);
	if(elem != null) {
		if (elem.style.display == "none" || elem.style.display == "NONE" || elem.style.display == null || elem.style.display == "") {
       		elem.style.display="block";
    	} else {
       		elem.style.display="none";
     	}
	}
}

/**
 * Get the current upper left X position of the specified object
 */
function getUpperLeftX(objname){
    var obj = document.getElementById(objname);
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
        }
    }
    return curleft;
}

/**
 * Get the current upper left X position of the specified object relative to the body
 */
function getBodyUpperLeftX(objname) {
    var o = document.getElementById(objname);
    var l=0, s=o;
    while (o.offsetParent &&
           o.offsetParent.tagName.toLowerCase() != 'html' &&
           o.offsetParent.tagName.toLowerCase() != 'body')
    {
        l += o.offsetLeft;
        o = o.offsetParent;
    }
    //l += o.offsetLeft;
    while (s = s.parentNode) {
        if ((s.scrollLeft > 0) && s.tagName.toLowerCase() == 'div') {
            l -= s.scrollLeft;
        }
    }
    return l;
}

/**
 * Get the current upper left Y position of the specified object
 */
function getUpperLeftY(objname){
    var obj = document.getElementById(objname);
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curtop += obj.offsetTop
        }
    }
    return curtop;
}

/**
 * Get the current upper left Y position of the specified object relative to the body
 */
function getBodyUpperLeftY(objname){
	return getUpperLeftY(objname);
}

/**
 * Get the current upper left X,Y coordinate of the specified object
 */
function getUpperLeft(objname){
    var obj = document.getElementById(objname);
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return new Array(curleft, curtop);
}

/**
 * Get the height of the specified element
 */
function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function checkScreenBounds(elementId) {
	// make sure it stays on-screen
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	  //Non-IE
	  myWidth = window.innerWidth;
	  myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  myWidth = document.documentElement.clientWidth;
	  myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  //IE 4 compatible
	  myWidth = document.body.clientWidth;
	  myHeight = document.body.clientHeight;
	}			

    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
      //Netscape compliant
      scrOfY = window.pageYOffset;
      scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      //DOM compliant
      scrOfY = document.body.scrollTop;
      scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      //IE6 standards compliant mode
	      scrOfY = document.documentElement.scrollTop;
	      scrOfX = document.documentElement.scrollLeft;
	    }
  
		myHeight = myHeight + scrOfY;
		myWidth = myWidth + scrOfX;
				
		var el = document.getElementById(elementId);		
		var dialogY = getBodyUpperLeftY(elementId);
		var dialogHeight = getElementHeight(elementId);
		var dialogX = getBodyUpperLeftX(elementId);
        var dialogWidth = getElementWidth(elementId);
		
		//alert('y='+dialogY+' height='+dialogHeight+' screen='+myHeight);
	if (dialogY + dialogHeight > myHeight) {
		// move the dialog up to stay on screen
		//alert('moving to '+(myHeight - dialogHeight));
		el.style.top = (myHeight - dialogHeight - 16) + 'px';
	}
	
    // verify dialog not off right hand side of window.
    // note: if window is very narrow it'll possibly disappear off the left
    if (dialogX + dialogWidth > myWidth) {
        // move the dialog left to stay on screen
        //alert('moving to '+(myWidth - dialogWidth));
        el.style.left = (myWidth - dialogWidth - 16) + 'px';
    }
}

function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}	

function getElementWidth(Elem) {
    if (ns4) {
        var elem = getObjNN4(document, Elem);
        return elem.clip.width;
    } else {
        if(document.getElementById) {
            var elem = document.getElementById(Elem);
        } else if (document.all){
            var elem = document.all[Elem];
        }
        if (op5) { 
            xPos = elem.style.pixelWidth;
        } else {
            xPos = elem.offsetWidth;
        }
        return xPos;
    } 
}

function URLEncode(sStr) {
    return escape(sStr)
       .replace(/\+/g, '%2B')
       .replace(/\/\//g, '%2F');
}

