/**this function create HttpRequests, checks to see what objects the
* browser supports in order to create the right kind of XMLHttpRequest
* tyoe object to return.
*
*@return Returns an XMLHttpRequest type object or false
*@type Object | Boolean
*/
//Global Variables
	var txtPos = null;
	var file = '../library/php/rmsAjaxRequests.php';
	//var file = '../library/php/rmsPath.php';
	
	function createXMLHttpRequest() {
		var request = false;
	
		/*Does this browser support the XMLHttpREquest object? */
	
		if(window.XMLHttpRequest) {
			if(typeof XMLHttpRequest != 'undefined')
				/*Try to creat a new XMLHttpREquest object*/
				try {
					request = new XMLHttpRequest();
				}
				catch (e) {
					request = false;
				}
	
			/*does this browser support ActiveX objects?*/
		}
		else if (window.ActiveXObject) {
			/*try to create a new ACtive XMLHttp object*/
	
			try {
				request = new ActiveXObject("MSXML2.XMLHTTP");
			}
			catch (e) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					request = false;
				}
			}
		}
	
		return request;
	}
	
	
	/** This function requestData, takes the passed /pReq/ object and
	* sends the passed /pData/ to the passed /pURl/. The /pReq
	* object calls the /pFunc/ function on /onreadystatechange/.
	*
	*/
	function requestData(pUrl,pData,pMethod,divID) {
		var mytime = '&amp;ms=' + new Date().getTime();
		var data   =  pData + mytime;
		txtPos     = divID;
		//$(divID).innerHTML = 'test';
		
		var req = new Ajax.Updater(divID,pUrl,{ 
				  method     : pMethod,
				  parameters : data,
				  onComplete : function(response){
					  $(txtPos).innerHTML = response.responseText;	
				  }
		});
	}
	
	function addInnerHTML(pReq){
		$(txtPos).innerHTML = pReq.responseText;	
	}
	
	
	function showInAlert(){
		var div = $(id);
		
	}

	
	/** This function set the class attribute for the tag with the id
		* id =  the id of the element to be given the class
		* attr = the attribute to be set for the element e.g. class 
		* val = the value of the attrubute to set
	*/
	
	function rmsSetAttrib(id,attr,val) {
		var elemt = $(id);
		var attr  = document.createAttribute(attr);
		attr.value = val;
		elemt.setAttributeNode(attr);
	}
	
	/**
	 
	 
	*/
	function rmsAddText(id,data) {
		var elemt = $(id);
		elemt.appendChild(document.createTextNode('data'));
	}
	
	function rmsDisp(id,data) {
		var elemt = $(id);
		elemt.innerHTML = data;
	}
	
	/** This function inserts a row to a table
		* the row spans the number of columns specified
		* a div with id more is created within the row.
	*/
	function rmsInRowSpan(tbl,ind,span,rId) {	
			var curtbl = $(tbl).insertRow(cInd);
			var filter = 'STYLE="filter:blendTrans(duration=12)"';
			browser=navigator.appName
			var curRow  = $(rId);
			curRow.setAttribute('class','expand');
			curRow.innerHTML = 'happy';
		/*	if(browser=="Microsoft Internet Explorer"){
				var cRow   = curtbl.insertCell(0);
				cRow.colSpan = span;
				cRow.setAttribute('class','expand');
				cRow.setAttribute('colspan',span);
				cRow.innerHTML = '<div id="' + rId + ' " class="expand" ' + filter + '>this text shuld not display after th ecick</div>';
			} else{
				curtbl.innerHTML = '<td colspan="' + span + '"><div id="' + rId + '" class="expand" ' + filter + '>test ajax</div></td>';
			}*/
		
	}
	
	function rmsRowDel(id,ind) {
		var row = $(id).deleteRow(ind);
	}
	
	
	function rmsExpand(sign,rowID,data) {
		var disp = '';
		var curRow = sign.parentNode.parentNode;
		cInd       =  curRow.rowIndex;
		rId = 'more'+ rowID;
		divRow = 'row' + rowID;
		
		var trs = $$('tr[id]');
		for(var i=0; i<trs.length; i++){
			var trID = trs[i].id;
			if(trID.startsWith('row')){
				$(trID).hide();				   
			}
			
		}
		/*//var file = '../library/php/rmsAjaxRequests.php';*/
	
		if(sign.innerHTML  == '+') {
		//	$(rId).show();
			$(divRow).show();
			disp  = '-';
			requestData(file,data,'Post',rId);
		}
		else {
			$(rId).hide();
			$(divRow).hide();
			disp = '+';
		}
	
		sign.innerHTML = disp;
	}
	
	function doSlideShow(param,divID){
		//var file = '../library/php/rmsAjaxRequests.php';
		requestData(file,param,'GET',divID);
		$('pbLinks').innerHTML = 'related items';
	}
		
	function rmsEdit(intxt,rowID,data) {
		
		//rmsExpand(intxt,rowID,data)
		var disp = '';
		//var url  = '../library/php/rmsAjaxRequests.php';
		cInd =  intxt.parentNode.parentNode.rowIndex + 1;
		rId  = 'more'+ cInd;
		txtPos = rId;		

		$(rId).show();
		$(divRow).className='show';
		
		var rmsAjax = new Ajax.Request(
		file,
		{ method : 'get',
		  parameters  : data,
		  onComplete  : addInnerHTML
		 });
	}
	
	function showEdit(){
		
	}
	
	function showCalender(){
		var cal = $('rmsDate');
		cal.setAttribute('class','show');
	}
	
	function doDateUpdate(divID,dLink){
		document.frmAdmin.divID.value = dLink;
		alert(dLink);
	}
	
	function submitForm(name){
		$(name).submit();
	}
	
	function showSubmit(sBn){
		//$('bnAction').show();
		$('hiddenBn').value = 0;
		if(sBn.value ==''){
			//$('bnAction').hide();			
		} else{
		//	if($('bnAction')) $('bnAction').show();
			$('frmAdmin').submit();	
		}			
	}
	
	function submitForm(name){
		$('hiddenBn').value = 1;
		$(name).submit();
	}
	
	function loginUser(){
		var login = $('rmsLogin');
		login.innerHTML = '<button>logout</button>';
	}
	

	function openSmiley() {
		w=window.open("rmsSmileys.php", "smileys", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=300,height=300");
	  if(!w.opener)	  {
	  	w.opener=self;
	  }
	}
 
 	
	function showAnswer(num){
		var id     = 'faq' + num;
		var plus   = $('plus' + num);
		var toshow = $('faq' + num);
		var opened = $$('tr');
		

		
		
		if(plus.innerHTML == '+'){
			opened.each(function(n){
				if(n.id.startsWith('faq')){
					n.hide();
				} 
			});
			plus.innerHTML = '-';
			Effect.SlideDown(id);
		}else{
			plus.innerHTML = '+';
			toshow.hide();
		}
	}
	
	function registerNewUser(){
		
	}
	
	function switchDisplay(one,two,three){
		$(one).style.display='inline';
		$(two).style.display='none';
		$(three).style.display='none';
		$('Errors').innerHTML='';
	}

	function submitInquiry(frmID,divID){
		var pUrl     = "rmsFormMail.php";
		var pData    = $(frmID).serialize();
		var pos      = $('rmsErrors');
		/*$(frmID).action = pUrl;
		$(frmID).submit();*/
		//requestData(file,pData,"Post",divID) ;
		//$(divID).innerHTML = pData;
		var req = new Ajax.Request(pUrl,{
				method:'Post',
				parameters:pData,
				onSuccess: function(response){
					var data  = response.responseText.evalJSON(true);
					var type  = parseInt(data.type);
					if(type == 0){
						var pos = $('rmsErrors');
						pos.innerHTML = data.msg;
						pos.className = 'showErrors';
						pos.show();
					}else if(type == 1){
						$(divID).innerHTML = data.msg;	
						pos.hide();
					}
				}
		});	  
	}
	
	
	function playSound(filename,where){
		$(where).innerHTML = "<embed src='" + filename + "' hidden='true' height='0px' width='0px' type='audio/mpeg' loop='true' autostart='true'> ";
	}
	
	
	function resetForm(frmName){
		$(frmName).submit();
	}
	
	
	function sendFeedBack(elm){
		elm.form.submit();
	}
	
	
	function dispIconImage(path,file,use){
		var divid = $('dispIcon');
		if(use == '1') file = file + '/thumb.gif';
		divid.innerHTML= "<img src='" + path + "/" + file + "' title= '" + file + "' alt='Current Icon Selection' height='48px' hspace='30px'>";
		divid.style.display='inline';
	}
	
	function switchCSSClass(id,newClass){
		var divid = $(id);
		//divid.setAttribute('class',newClass);
		divid.className = newClass;
	}
	
	function rmsDeleteRecord(table,id){
		pReq       = new Ajax.Updater(
		'rmsErrors',file,
			{ method     : 'Post',
			  parameters : {'table':table,'ID':id,'action':'deleteRecord'},
			  onComplete : function(){
			  	$('rmsErrors').className = 'showPass';
			  }
			}			
		);	
	}
	
	function toggleBox(chk,table,id,fld){
		var val = 0;
		if($(chk).checked){
			val = 1;
		}
		
		var reg = new Ajax.Request(file,
			{method : 'Post',
			 parameters: {'table':table, 'recID':id,'field':fld,'action':'toggle','value':val},
			 onSuccess: function(response){ 
			 	var data = response.responseText;
			 	$('rmsErrors').innerHTML = '1 . Updating record ' + id + ' in table ' + table + ', setting ' + fld + ' = ' + val + ' (' + data + ')';
				$('rmsErrors').className  = 'showPass';
			}
		});
	}
	
	function showSxn(sxn,toClose){
		var divs = document.getElementsByTagName('div');
		var i    =  0;
		var id   = '';
		for(i=0; i<divs.length; i++){
			id   = divs[i].id;
			if(id.startsWith(toClose)){
					$(id).hide();
			}
		}
		$(sxn).show();
	}
	
	function resetDisplay(){
		var divs = document.getElementsByTagName('div');
		var id   = '';
		for(var i=0; i<divs.length; i++){
			id   = divs[i].id;
			if(id && id.startsWith('sxn')) $(id).hide();
		}
		for(var k=1; k<5; k++){
			var dd = $('sxn' + k);
			dd.show();
		}
	}
	
	function chooseTitle(value){
		if(value == 'others'){
			$('seltitle').name='';
			$('other').enable();
		}else{
			$('seltitle').name='title';
			$('other').disable();
		}
	}
/*************************************************************************************************/	
	resizeEditorBox = function (editor) {
    // Have this function executed via TinyMCE's init_instance_callback option!
    // requires TinyMCE3.x
    var container = editor.contentAreaContainer, /* new in TinyMCE3.x -
        for TinyMCE2.x you need to retrieve the element differently! */
        formObj = document.forms[0], // this might need some adaptation to your site
        dimensions = {
            x: 0,
            y: 0,
            maxX: 0,
            maxY: 0
        }, doc, docFrame;

    dimensions.x = formObj.offsetLeft; // get left space in front of editor
    dimensions.y = formObj.offsetTop; // get top space in front of editor

    dimensions.x += formObj.offsetWidth; // add horizontal space used by editor
    dimensions.y += formObj.offsetHeight; // add vertical space used by editor

    // get available width and height
    if (window.innerHeight) {
        dimensions.maxX = window.innerWidth;
        dimensions.maxY = window.innerHeight;
    } else {
		// check if IE for CSS1 compatible mode
        doc = (document.compatMode && document.compatMode == "CSS1Compat")
            ? document.documentElement
            : document.body || null;
        dimensions.maxX = doc.offsetWidth - 4;
        dimensions.maxY = doc.offsetHeight - 4;
    }

    // extend container by the difference between available width/height and used width/height
    docFrame = container.children [0] // doesn't seem right : was .style.height;
    docFrame.style.width = container.style.width = (container.offsetWidth + dimensions.maxX - dimensions.x - 2) + "px";
    docFrame.style.height = container.style.height = (container.offsetHeight + dimensions.maxY - dimensions.y - 2) + "px";
}

	function adjustMceHeight(editorID) {
	var frame, doc, docHeight, frameHeight;
	
	frame = document.getElementById(editorID+"_ifr");
	if ( frame != null ) {
		//get the document object
		if (frame.contentDocument) { 
			doc = frame.contentDocument; 
		} else if (frame.contentWindow) { 
			doc = frame.contentWindow.document; 
		} else if (frame.document) { 
			doc = frame.document; 
		}
		
		if ( doc == null )
			return;
		
		//prevent the scrollbar from showing
		doc.body.style.overflow = "hidden";
	
		docHeight;
		frameHeight = parseInt(frame.style.height);
		
		//Firefox
		if ( doc.height ) { docHeight = doc.height; }
		//MSIE
		else { docHeight = parseInt(doc.body.scrollHeight); }
		
		//MAKE BIGGER
		if ( docHeight > frameHeight-20 ) { frame.style.height = (docHeight+20) + "px"; }
		//MAKE SMALLER
		else if ( docHeight < frameHeight-20 ) { frame.style.height = Math.max((docHeight+20), 100) + "px"; }
		
		//only repeat while editor is visible
		setTimeout("adjustMceHeight('" + editorID + "')", 1);
	}
}
