
document.onmousemove = getMouseCoordinates;

function getMouseCoordinates(event){
	ev = event || window.event;
	mouseX = ev.pageX;
	mouseY = ev.pageY;
}

//either shows or hides an element based on its current status
function showHideElement(elementId){
	if(document.getElementById(elementId).style.display=='none'){
		document.getElementById(elementId).style.display = '';
	}
	else{
		document.getElementById(elementId).style.display = 'none';
	}
}

function showHideParentElement(elementId){
	if(parent.document.getElementById(elementId).style.display=='none'){
		parent.document.getElementById(elementId).style.display = '';
	}
	else{
		parent.document.getElementById(elementId).style.display = 'none';
	}
}

function hideElement(elementId){
	if(document.getElementById(elementId)){
		document.getElementById(elementId).style.display = 'none';
	}
}

function showElement(elementId){
	if(document.getElementById(elementId)){
		document.getElementById(elementId).style.display = '';
	}
}

function changeHTML(elementId, str){
    document.getElementById(elementId).innerHTML = str;
}

function positionItAtMouse(elementID){
	//define universal dsoc left point
	var dsocleft = document.body.scrollLeft;
	//define universal dsoc top point
	var dsoctop = document.body.scrollTop;

	//if the user is using IE 4+ or Firefox/ NS6+
	if (document.all||document.getElementById){
		if(document.getElementById(elementID)){
			document.getElementById(elementID).style.left=parseInt(mouseX)+5+"px";
			document.getElementById(elementID).style.top=mouseY+5+"px";
		}
	}
}

function xmlhttpPost(strURL, formId, updateElementId, loadingElement, doNotHide){
	if(!doNotHide){
		hideElement(updateElementId);
		showElement(loadingElement);
	}
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatePageHTML(updateElementId, self.xmlHttpReq.responseText);
			if(!doNotHide){
				hideElement(loadingElement);
				showElement(updateElementId);
			}
        }
    }
    self.xmlHttpReq.send(getquerystring(formId));
}

function getquerystring(formId) {
	qstr = '';
	var obj = document.forms[formId]
	for (i=0; obj.elements[i]; i++) {
		currentElement = obj.elements[i].name;
		currentValue = obj.elements[i].value;
    	qstr = qstr + currentElement + '=' + escape(currentValue) + '&';  // NOTE: no '?' before querystring
	}
    return qstr;
}

function updatePageHTML(elementId, str){
    document.getElementById(elementId).innerHTML = str;
}

function validateString(element){
      
    stringType = element.substr(0, 4);
	if(!document.getElementById(element)){
		return false;
	}
    stringToTest = document.getElementById(element).value;
	re = /^$/;
	canBeEmpty = element.substr(4,1);
	if(canBeEmpty=='t'){
		canBeEmpty = true;
	}
	else if(canBeEmpty=='f'){
		canBeEmpty = false;
	}
	if(canBeEmpty==true && stringToTest==''){
		if(document.getElementById(element+'_valid_image')){
			document.getElementById(element+'_valid_image').src = 'http://www.belhurst.com/admin/themes/Default/images/form_valid.gif';
		}
		return true;
	}
if(stringType=='blnk'){re = /.$/;}
if(stringType=='date'){re = /^([0-9]){2,2}\/([0-9]){2,2}\/([0-9]){4,4}$/;}
if(stringType=='emal'){re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;}
if(stringType=='flot'){re = /^\-{0,1}[0-9]{1,6}[\.]{0,1}[0-9]{0,2}$/;}
if(stringType=='gtxt'){re = /^[\nA-Za-z0-9\ \.\-_\(\)\,&!\?\:\;\\'\/\#\$\%\^\*"]+$/;}
if(stringType=='keyp'){re = /^[A-Za-z0-9\ \.\-_\(\)\,&!\?]+$/;}
if(stringType=='link'){re = /^(?:(?:ftp|https?):\/\/){0,1}(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|mobi|tv|us|[a-z][a-z])\b(?:\d+)?(?:\/[^;"\'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"\'<>()\[\]{}\s\x7f-\xff]+)*)?/;}
if(stringType=='ltxt'){re = /^[A-Za-z0-9\ \.\-_\(\)\,&!\?\'\/]+$/;}
if(stringType=='name'){re = /^([a-zA-Z0-9\ \-\,\.\(\)\/\:\\\']){1,64}$/;}
if(stringType=='numb'){re = /^\-{0,1}([0-9]+)$/;}
if(stringType=='pass'){re = /^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/;}
if(stringType=='phon'){re = /^\(([0-9]){3}\) ([0-9]){3}\-([0-9]){4}$/;}
if(stringType=='ptle'){re = /^[A-Za-z0-9\ \.\-_\(\)\,&!\?\']{3,255}$/;}
if(stringType=='user'){re = /^[a-zA-Z0-9]{4,32}$/;}
if(stringType=='zipc'){re = /^[A-Za-z0-9\-]{5,20}$/;}
	goodString = false;

	goodString = re.test(stringToTest);
	if(goodString){
		if(document.getElementById(element+'_valid_image')){
			document.getElementById(element+'_valid_image').src = 'http://www.belhurst.com/admin/themes/Default/images/form_valid.gif';
		}
		return true;
	}
	else{
		if(document.getElementById(element+'_valid_image')){
			document.getElementById(element+'_valid_image').src = 'http://www.belhurst.com/admin/themes/Default/images/form_not_valid.gif';
		}
		return false;
	}
}

function validateDescription(stringType){
if(stringType=='blnk'){return 'Any characters are allowed here.';}
if(stringType=='date'){return 'A date must be in the format mm/dd/yyyy.  For example 03/19/1982,';}
if(stringType=='emal'){return 'An email address must contain the @ symobol and a valid domain name; like joe@example.com.';}
if(stringType=='flot'){return 'ption=A floating point number can be a whole number or a decimal like &quot;6&quot; or &quot;5.34&quot;.';}
if(stringType=='gtxt'){return 'General text that can contain most special characters with a few exceptions.  Like "This is my \'Description\'!?".';}
if(stringType=='keyp'){return 'This key phrase is inserted into your page automatically to ensure proper search engine indexing.  This may be an individual word like "Recipes", or a series of words like "New England Fishing".';}
if(stringType=='link'){return 'A Link to a website.  A link must contain the full protocol.  Like "http://www.example.com".';}
if(stringType=='ltxt'){return 'This is the "clickable" link text in a menu or other website navigation object.  In the case of an image link, this text will appear as the image description. Like "My Page Title". Several special characters like &gt; and &lt; are NOT allowed.';}
if(stringType=='name'){return 'A person\'s name can contain any combination of letters, spaces, dashes, and can be up to 64 characters long. Like Anne, or Smith.';}
if(stringType=='numb'){return 'A whole number containing no decimals, commas, or spaces.  Like 23, -158, or 79932.';}
if(stringType=='pass'){return 'A password must be between 6 and 32 characters long and have at least 1 number, at least one uppercase letter and at least one lowercase letter.';}
if(stringType=='phon'){return 'A phone number must be in the format (555) 555-5555.';}
if(stringType=='ptle'){return 'This is the title that will show up in search engine results, and when users bookmark your page.  You should pick something short and descriptive like "Our Home Page".  It is also the basis for the filename of your page, e.g. "Our Home Page" turns into "Our-Home-Page.html".';}
if(stringType=='user'){return 'A username must be between 4 and 32 characters long and can consist of numbers and letters; like Matt';}
if(stringType=='zipc'){return 'A zip code most be in the format 12345, 12345-54321, or A1B2C3.';}
}

function validateForm(formName){
      
      var obj = document.forms[formName]
      deleting = false;
      delre = /delete/;  //regular expression for delete check box
      
      for (i=0; obj.elements[i]; i++) {
            
            currentElement = obj.elements[i].name;
            currentValue = obj.elements[i].value;
            isValid = true;
            
            if(delre.test(currentElement)){
                  if(obj.elements[i].checked==true){
                        deleting = true;
                  }
            }
            
            if(currentElement.substr(4,2)=='t-' || currentElement.substr(4,2)=='f-'){
                  stringType = currentElement.substr(0,4);
                  canBeEmpty = currentElement.substr(4,1);
                  
                  if(canBeEmpty=='t'){
                        canBeEmpty = true;
						canBeEmptyDescription = 'You can leave this field empty.';
                  }
                  else if(canBeEmpty=='f'){
                        canBeEmpty = false;
						canBeEmptyDescription = 'You can NOT leave this field empty.';
                  }
                  if(stringType=='blnk'){
                  	isValid = true;
                  }
                  else{
                  	isValid = validateString(currentElement);
                  }
            }
            
            if(!isValid){
                  alert(validateDescription(stringType) + '\n' + canBeEmptyDescription + '\nYou will need to fix this before continuing. ');
                  obj.elements[i].focus(); 
                  obj.elements[i].className='formElementError';
                  return false;
                  break;
            }
            else{
                  obj.elements[i].className='';
            }
      }
      
      if(deleting==true){
            return confirm('You have objects selected for deletion.\n This cannot be undone!\n Are you sure you wish to continue?');
      }
      return true;
}

function formatPhoneNumber(elementID){
	if(document.getElementById(elementID)){
		number_final = '';
		number = document.getElementById(elementID).value;
		for(i=0; i<number.length; i++){
			temp_number = number.substr(i, 1);
			if(isNumber(temp_number)){
				number_final = number_final + temp_number;
			}
		}
		if(number_final.length>7){
			document.getElementById(elementID).value = '(' + number_final.substr(0, 3) + ') ' + number_final.substr(3, 3) + '-' + number_final.substr(6, 4);
		}
	}
}

function roundFloat(floatNum,dec){
	floatNum = parseFloat(floatNum);
    return floatNum.toFixed(dec);
}

function isNumber(number){
    re = /^[0-9\.]+$/;
    return re.test(number);
}

function windowWidth(){
	var myWidth = 0;
	if(typeof(window.innerWidth)=='number'){
		//Non-IE
		myWidth = window.innerWidth;
	}
	else if(document.documentElement && document.documentElement.clientWidth){
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	}
	else if(document.body && document.body.clientWidth){
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function windowHeight(){
	var myHeight = 0;
	if(typeof(window.innerHeight)=='number'){
		//Non-IE
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight){
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && document.body.clientHeight){
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}

function setElementSize(elementID, newWidth, newHeight, maximizeWidth, maximizeHeight) {
	if(maximizeWidth){
		newWidth = windowWidth()-25;
	}
	if(maximizeHeight){
		newHeight = windowHeight()-100;
	}
	if(newWidth){
		document.getElementById(elementID).width = newWidth;
	}
	if(newHeight){
		document.getElementById(elementID).height = newHeight;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


