/**
* Function to generate a random password 6-9 characters long
*
*/
function generatePass(passObj1,passObj2) {
	//alert("Generating Pass");
	var tmpArr = new Array("a","b","c","d","e","f","g","h","i","j","k","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","9","8","7","6","5","4","3","2");
	var retStr = "";
	var randNum = 6 + Math.round(Math.random()*3);
	//alert(randNum);
	for (var i = 0; i < randNum; i++) {
		retStr += tmpArr[Math.floor(Math.random()*(tmpArr.length-1))];		
	} // end for
	//alert(randNum+"\n"+retStr);
	passObj1.value = retStr;
	passObj2.value = retStr;
}


/**
* Function to select all checkbox elements in a form.
* This funciton is used by the Table class in class.dbExtensions.php
*/
function selectAllCheckboxes(formObj) {

	if (formObj.mainCheckBox.checked == true) selectAllCheck(formObj,true);
	else selectAllCheck(formObj,false);

}

/**
* A supplemental function to the above function
*/
function selectAllCheck(formObj,val) {
	
	var elem = formObj.elements.length;
	for(i = 0; i < elem; i++) {
	
		if (formObj.elements[i].type == 'checkbox') formObj.elements[i].checked = val;
		
	} 
	
} 

/**
* Function to check to see if a checkbox has been selected (leads)
*/
function checkForCheckboxes(formObj) {
	
	var ok = false;
	var elem = formObj.elements.length;
	for (i = 0; i < elem; i++) {
			if (formObj.elements[i].type == 'checkbox' && formObj.elements[i].checked == true) {
				ok = true;
				break;
			} 
	} 
	if (ok == true && formObj.selected_action) {
		if (formObj.selected_action.value == "") ok = false;	
	}
	return ok;
} 


/**
* Function to attach one obj onto another.  They second object SHOULD be a div tag, but it really can be anything.
*
* Also, DO NOT FORGET TO SET THE FOLLOWING IN THE STYLE OF THE ATTACH OBJECT: position:absolute;
*
* @param string the ID of the parent object to attach the other object to
* @param string the ID of the object to attach
* @param integer the width to offset the attached object.  Optional.  Will default to the parent object's width
* @param integer the height to offset the attached object.  Optinal.  Will default to the parent object's height
* @param boolean debug mode
*/
function attach(parentID,thisID,offL,offT,debug) {
	
	if (debug == null) debug = false;
	if (debug == true) myw('messages','attaching');
	// Get the objects
	var pObj = document.getElementById(parentID);
	var tObj = document.getElementById(thisID);
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	
	// Check for the offsets
	if (debug == true) myw('messages','L: '+offL+'<br>T: '+offT);
	if (offL == null || isNaN(offL)) offL = (gIsIE == true ? -3 : -4); // pObj.offsetWidth;
	if (offT == null || isNaN(offT)) offT = pObj.offsetHeight+(gIsIE == true ? 12 : 7);
	if (debug == true) myw('messages','L: '+offL+'<br>T: '+offT);
	
	// Get the new positions, adjusting for the scroll top
	var leftPos = findPosX(pObj) + offL;
	var topPos = findPosY(pObj) + offT + st;
	if (debug == true) myw('messages','FindX: '+findPosX(pObj)+'<br>FindY: '+findPosY(pObj));
	if (debug == true) myw('messages','LeftPos: '+leftPos+'<br>TopPos: '+topPos);

	// Now attach the object
	tObj.style.left = leftPos + 'px';
	tObj.style.top =  topPos + 'px';
	if (debug == true) myw('messages','Left Style: '+tObj.style.left+'<br>Top Style'+tObj.style.top);
	
} // end function

// Function to change a value of a submit button.  Used with the searchbox
function change(obj,str) {
	obj.value = str;
} // end function
/**
* Function to find the x position of an object
*/
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

/**
* Function to find the y position of an object
*/
  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

/**
* Function to write to a div, instead of the fucking alert box
*/
function myw(objid,str) {
	document.getElementById(objid).innerHTML += str+"<br>";
} // end function

/**
* Image Swap/Load/Restore Functions
*/
function MM_preloadImages() { 
  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_swapImgRestore() { 
  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_findObj(n, d) { 
  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() { 
  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];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/**
* Function to see if this browser is IE
*/
function isIE() {
	if (navigator.appName.indexOf('Microsoft') != -1) return true;
	else return false;
} // end function

var gIsIE = isIE();

/**
* Function to toggle display properties of a given div area
*/
function toggleDiv( divId ) {
	var toggleThis = document.getElementById(divId);
	if (toggleThis.style.display == 'block') toggleThis.style.display = 'none';
	else toggleThis.style.display = 'block';
} // end function

function selectRelatedRadios( relGroup, selOption ) {
	var radioArray = document.getElementsByTagName('input');
	for(var i=0;i<radioArray.length;i++) {
		var currentInput = radioArray[i];
		if(currentInput.type=="radio") {
			if( relGroup=='ALL' && currentInput.value==selOption ) { currentInput.checked = true; }
			else if( currentInput.getAttribute('rel')==relGroup && currentInput.value==selOption ) { currentInput.checked = true; }
		}//end if radio
	}//end for
}//end function

function selectRelatedTextFields( relGroup, selValue ) {
	var textArray = document.getElementsByTagName('input');
	for(var i=0;i<textArray.length;i++) {
		var currentInput = textArray[i];
		if(currentInput.type=="text") {
			if( relGroup=='ALL' && currentInput.getAttribute('rel')) { currentInput.value = selValue; }
			else if( currentInput.getAttribute('rel')==relGroup ) { currentInput.value = selValue; }
		}//end if radio
	}//end for
}//end function


function popitup( bodytext ) {
	newwindow=window.open('','name','height=600,width=625,scrollbars=yes');
	var tmp = newwindow.document;
	tmp.write('<html><head><title>' + document.title + '</title>');
	tmp.write('<link href="css/styles.css" rel="stylesheet" type="text/css">');
	tmp.write('<link href="css/home.css" rel="stylesheet" type="text/css">');
	tmp.write('</head><body>');
	for(j=0;j<bodytext.length;j++) {
		tmp.write('<table width="600" height="64" border="0" align="center" cellpadding="3" cellspacing="0" id="pagecontent">');
		tmp.write( bodytext[j] + '</table>');
	}//end for
	tmp.write('<p><a href="javascript:window.print()">Print this page</a>.</p>');
	tmp.write('<p><a href="javascript:self.close()">Close</a> the popup.</p>');
	tmp.write('</body></html>');
	tmp.close();
}//end function 

function addOption( selectObj, newName, newValue ) {
	if(typeof(selectObj)!="object") selectObj = document.getElementById(selectObj);
	var newOptionObj = new Option( newName, newValue );
	selectObj.options[selectObj.options.length] = newOptionObj;
}//emd function

function isOptionInList( targetSelectObj, targetOptionName ) {
	for(var i=0;i<targetSelectObj.options.length;i++) {
		if(	targetSelectObj.options[i].text == targetOptionName ) { return true; }
	}
	return false;
}//end function

function selectOption( targetSelectObj, targetOptionName ) {
	if(typeof(targetSelectObj)!="object") targetSelectObj = document.getElementById(targetSelectObj);
	for(var i=0;i<targetSelectObj.options.length;i++) {
		if(	targetSelectObj.options[i].text == targetOptionName ) { targetSelectObj.options[i].selected = true; }
	}
}

function printMe() { 
	var allOfThem = document.getElementsByTagName('table');
	
	var bodytext = new Array();
	for(var i=0; i<allOfThem.length; i++ ) {
		if( allOfThem[i].id.substr(0,11) == "pagecontent" ) {
			//alert(allOfThem[i].innerHTML);
			bodytext.push( allOfThem[i].innerHTML );
		}
	}
	popitup( bodytext );
}//end function

/* Function to append HTML to the DOM */
/* obj is the DOM object to append to (a div, td, body) */ 
function appendHTML(obj,html) {
	if(document.all) {
		/* If IE */
		obj.insertAdjacentHTML('beforeEnd', html);
	} else 	if (document.createRange) {
		/* If Netscape/Mozilla */
		var range = document.createRange();
		range.selectNode(obj)
		var documentFragment = range.createContextualFragment(html);
		obj.appendChild(documentFragment);
	}
}

// Set the vars for the bubble tooltip

function showToolTip(e,text) {

	if(document.all) e = event;

	// Set all of the vars
	var bt_obj = document.getElementById('bubble_tooltip');
	var bt_obj2 = document.getElementById('bubble_tooltip_content');
	var bt_objTop = document.getElementById('bubble_tooltip_top');
	var bt_objMiddle = document.getElementById('bubble_tooltip_middle');
	var bt_objBottom = document.getElementById('bubble_tooltip_bottom');

	bt_obj2.innerHTML = text;
	bt_obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	
	// Get the initial top and lefts
	var leftPos = e.clientX - 100;
	var topPos = e.clientY - bt_obj.offsetHeight - 5;
	
	// Check to see if the left pos is too small (need to flip it)
	if(leftPos < 0) {
		
		// reset the left to 0
		leftPos = 0;
		
		// Check if its off the page top wise (need to flip top to bottom)
		if (topPos <= 0) {
			
			// Reset the top pos
			topPos = e.clientY + 10 + st;
			bt_objTop.className = 'ul_top';
			bt_objMiddle.className = 'middle_flip';
			bt_objBottom.className = 'ul_bottom';
			
		} else {
			
			// Reset the top pos again
			topPos = topPos + st - 5;
			bt_objTop.className = 'll_top';
			bt_objMiddle.className = 'middle_flip';
			bt_objBottom.className = 'll_bottom';
			
		} // end else
	
	// Otherwise, its normal
	} else {
		
		// Check if its off the page top wise (need to flip top to bottom)
		if (topPos <= 0) {
			
			// Reset the top pos
			topPos = e.clientY + 10 + st;
			bt_objTop.className = 'ur_top';
			bt_objMiddle.className = 'middle';
			bt_objBottom.className = 'ur_bottom';
			
		} else {
			
			// Reset the top pos again
			topPos = topPos + st - 5;
			bt_objTop.className = 'lr_top';
			bt_objMiddle.className = 'middle';
			bt_objBottom.className = 'lr_bottom';
			
		} // end else
		
	} // end else
		
	// Reposition the div
	bt_obj.style.left = leftPos + 'px';
	bt_obj.style.top =  topPos + 'px';
	
}	

function hideToolTip()
{
	hide('bubble_tooltip');
	//document.getElementById('bubble_tooltip').style.display = 'none';
	//document.getElementById('bubble_tooltip2').style.display = 'none';
}

function hide(c) { 
	if (document.getElementById && document.getElementById(c)!= null) 
		node = document.getElementById(c).style.display='none'; 
	else if (document.layers && document.layers[c]!= null) 
		document.layers[c].display = 'none'; 
	else if (document.all) 
		document.all[c].style.display = 'none'; 
} 

function buttonDown( cellObj ) {
	cellObj.style.borderTop = 'solid thin black';
	cellObj.style.borderLeft = 'solid thin black';
	cellObj.style.borderRight = 'solid thin #999999';
	cellObj.style.borderBottom = 'solid thin #999999';
}//end function

function buttonUp( cellObj ) {
	cellObj.style.borderTop = 'solid thin #999999';
	cellObj.style.borderLeft = 'solid thin #999999';
	cellObj.style.borderRight = 'solid thin black';
	cellObj.style.borderBottom = 'solid thin black';
}//end function

function hide(c) { 
	if (document.getElementById && document.getElementById(c)!= null) 
		node = document.getElementById(c).style.display='none'; 
	else if (document.layers && document.layers[c]!= null) 
		document.layers[c].display = 'none'; 
	else if (document.all) 
		document.all[c].style.display = 'none'; 
} 

function show(c) { 
	if (document.getElementById && document.getElementById(c)!= null) 
		node = document.getElementById(c).style.display=''; 
	else if (document.layers && document.layers[c]!= null) 
		document.layers[c].display = ''; 
	else if (document.all) 
		document.all[c].style.display = ''; 
}

function hideShow(c) {
	if (document.getElementById && document.getElementById(c)!= null) {
		if (document.getElementById(c).style.display == 'none') show(c);
		else hide(c);
	} else if (document.layers && document.layers[c]!= null) {
		if (document.layers[c].display == 'none') show(c);
		else hide(c);
	} else if (document.all) {
		if (document.all[c].style.display == 'none') show(c);
		else hide(c);
	}
}
