//*******************************************************
//*		ajaxLib.js contains all required JS libs required	*
//*		for the proper AJAX work with the clients sites		*
//*		outside the PRISM.																*
//*******************************************************
//Preload main AJAX Engine
var libURL;
if(!libURL) libURL = "http://" + location.host;
document.write("<script type=\"text\/javascript\" src=\"" + libURL + "/prism_resource\/javascript\/ajaxengine.js\"><\/script>");
var enginePath;
var ajaxActionFile = libURL + "/client_ajax/index.php";
var imgDir = libURL + "/prism_resource/image/";

function WriteOutput(where,what){if(getElement(where)) getElement(where).innerHTML = what;}
function getElement(id){if(document.getElementById(id)) return document.getElementById(id);else return false;}
function get_random(){var ranNum= Math.round(Math.random()*999999);return ranNum;} 
function cutLine(str,len){if(str && str.length > len) str = str.substring(0,len) + '..'; return str;}

//Window mehtods
var openWindowID;
function CreatePopupWindow(id, ww, hh, message, bgcolor, toBeAttached, xx, yy){  
  if(document.getElementById(id)) return false;
  if(!bgcolor) bgcolor = 'FFFFFF';
  var e = document.createElement("div");
  e.id = id;
  e.style.visibility = "hidden";
  e.style.position = "absolute";
  e.style.overflowY = "hidden";
  divww = ww + 10; divhh = hh + 10;
  e.style.width = divww + "px";
  e.style.height = divhh + "px";
  e.style.zIndex = "-1";
  document.getElementsByTagName("body")[0].appendChild(e);	
  changeWindowIndex(id);
if(xx) var x = xx; else var x = Math.ceil(pageWidth()/4); 
if(yy) var y = yy; else var y = 50; 		
var partbody = "";	  
    partbody += "<table width='98%'><tr><td height='98%' align='center' valign='middle'><div id='boxContainer'><div id='boxContent'><table cellpadding=0 cellspacing=10 width=\"100%\" border=0 align=center bgcolor=\"#" + bgcolor + "\"><tr><td id=\"movebar\" onmousedown=\"changeWindowIndex('" + id + "')\"><table cellpadding=0 cellspacing=0 width=\"100%\" border=0><tr><td style=\"cursor:move;background-image:url('prism_resource/image/ajax_drag.gif');\"><ilayer width=\"100%\" onSelectStart=\"return false\"><layer width=\"100%\" onMouseover=\"isHot=true;if (isN4) ddN4("+id+")\" onMouseout=\"isHot=false\" height=\"25\">&nbsp;<\/layer><\/ilayer><\/td><td width=\"25\" align=\"right\"><a href=\"javascript:\" onclick=\"deletePopupWindow('" + id + "')\"><img src=\"prism_resource/image/ajax_close.gif\" width=20 height=20 border=0><\/a><\/td><\/tr><\/table><\/td><\/tr>";					
			if(toBeAttached) partbody += "<tr><td id=\"popcontent_" + id + "\"><\/td><\/tr>"; 
      else partbody += message;          
			partbody += "<\/table><\/div><\/div></td></tr></table>";
      WriteOutput(id,partbody);          
      if(toBeAttached) getElement('popcontent_' + id).appendChild(toBeAttached);          	
		  document.getElementById(id).style.visibility = 'visible';
		  document.getElementById(id).style.top = y + "px";	
		  document.getElementById(id).style.left = x + "px";
  return true;
}
function deletePopupWindow(what){
  openWindowID = null;
	var d = document.getElementsByTagName("body")[0];
  var olddiv = document.getElementById(what);
  if(document.getElementById(what)) d.removeChild(olddiv);		
}
function changeWindowIndex(id){
	if(openWindowID) document.getElementById(openWindowID).style.zIndex = "99";
	document.getElementById(id).style.zIndex = "100";
	openWindowID = id;
}

//Screen resolution
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}

function loadDataToMainArrays(xml){
    mainMassives = new Array(); 
    var x = xml.responseXML.getElementsByTagName('myRoot');  					
        if(x[0].childNodes.length == 0) {
  		    alert('No data has arrived, please contact us for support');return;
  	    }	  					
      	for (j = 0; j < x[0].childNodes.length; j++) {
      		massivename = x[0].childNodes[j].nodeName;     
          eval(massivename + "Array = new Array();");
      		eval(massivename + "ArrayStatic = new Array();");
          eval(massivename + "Massives = new Array();");      
          mainMassives.push(massivename);
      		for (k = 0; k < x[0].childNodes[j].childNodes.length; k++) {
      			thename = x[0].childNodes[j].childNodes[k].nodeName;
      			if(x[0].childNodes[j].childNodes[k].firstChild) {								
      					thevalue = x[0].childNodes[j].childNodes[k].firstChild.nodeValue;					
      				}
      			else {thevalue = ""};  
           //alert(massivename + '/' + thename + '/' + thevalue);            
           eval(massivename + "Array['" + thename + "'] = thevalue");
					 eval(massivename + "ArrayStatic['" + thename + "'] = thevalue");
           eval(massivename + "Massives.push(thename)");							
      	}
      } 
  }	
	function defineDataFromXML(xml,level){
   var xmlDataArray = new Array();
   if(!level) var level = 2;
   var x = xml.responseXML.getElementsByTagName('myRoot');
   if(x[0].attributes.getNamedItem('PAGESNUMBER'))
      xmlDataArray['PAGESNUMBER'] = x[0].attributes.getNamedItem('PAGESNUMBER').value;
   else xmlDataArray['PAGESNUMBER'] = 1; 
   if(x[0].attributes.getNamedItem('TOTALRECORDS'))
      xmlDataArray['TOTALRECORDS'] = x[0].attributes.getNamedItem('TOTALRECORDS').value;
   else xmlDataArray['TOTALRECORDS'] = 0;
   if(x[0].attributes.getNamedItem('CURRENTPAGE'))
       xmlDataArray['CURRENTPAGE'] = x[0].attributes.getNamedItem('CURRENTPAGE').value;
   else xmlDataArray['CURRENTPAGE'] = 1;
   if(x[0].attributes.getNamedItem('CURRENTDOM'))
       xmlDataArray['CURRENTDOM'] = x[0].attributes.getNamedItem('CURRENTDOM').value;
   else xmlDataArray['CURRENTDOM'] = null;
             
   xmlDataArray['XMLLOOP'] = x[0].childNodes.length;
   for (var j = 0; j < x[0].childNodes.length; j++) {
   if (x[0].childNodes[j].nodeType != 1) continue;
   var cNodeName = x[0].childNodes[j].nodeName;
   xmlDataArray.push(cNodeName);
   if(x[0].childNodes[j].firstChild) {      
     xmlDataArray[cNodeName] = x[0].childNodes[j].firstChild.nodeValue;
       if(level == 2){//Extract second XML LEVEL
       for (var a = 0; a < x[0].childNodes[j].childNodes.length; a++) {            
           if(x[0].childNodes[j].childNodes[a].firstChild) {         
             eval("xmlDataArray['" + cNodeName + "_" + x[0].childNodes[j].childNodes[a].nodeName + "'] = x[0].childNodes["+j+"].childNodes["+a+"].firstChild.nodeValue");
           } else {
             eval("xmlDataArray['" + cNodeName + "_" + x[0].childNodes[j].childNodes[a].nodeName + "'] = ''");
           }
         }
       }
      } else { //first level node value is empty
        xmlDataArray[cNodeName] = '';       
      }
    }     
    return xmlDataArray;
  }    
	
	function searchPrismArray(ar,vl){
    for(var a = 0; a < ar.length; a++) {
      if(ar[a] == vl) return a; 
    }
    return -1;    
  }
	function createListFromArray(array,delimiter){
     var newlist = "";
     if(!delimiter) var delimiter = ",";
     for(a = 0; a < array.length; a++){
       if(array[a]) {        
        if(newlist != '') newlist += delimiter; 
        newlist += array[a];        
       }
     }    
     return newlist;    
  }
	function cEscape(str){  
		str = unicodeToEntities(str);//convert to entities  
	  try{
		str = str.replace(/;amp;/g,";");
		str = str.replace(/&amp;#/g,"&#");	
		str = str.replace(/"/g,"&quot;");
		str = str.replace(/</g,'&lt;');
		str = str.replace(/>/g,'&gt;');  
		str = escape(str);  
	  } catch(err){}
		return str;
	}
	function unicodeToEntities(str){  	 
	try{
		str = str.toString();   
    var q='';
    for(i=0; i<str.length; i++) {
      j=str.charCodeAt(i);
      q+=(j==38)?'&amp;':(j<128)?str.charAt(i):'&#'+j+';';      
    }    
		return q;
    }catch(err){return str;}
  }
	
	function changeOpac(opacity, id) { 
			if(getElement(id)) var object = document.getElementById(id).style;		
			else var object = document.body.style; 
      object.opacity = (opacity / 100); 
      object.MozOpacity = (opacity / 100); 
      object.KhtmlOpacity = (opacity / 100); 
      object.filter = "alpha(opacity=" + opacity + ")"; 
  }
	
	function CreateMessage(message){
  	return "<table width='100%'><tr><td height='100%' align='center' valign='middle'><div class='MessageBox' id='boxContainer'><div class='MessageBox' id='boxContent'><br>" + message + "<br><img src='/prism_resource/image/wait.gif' width=32 height=32 vspace=30></div></div></td></tr></table>";
	}
	function padZero(num) {
	  return (num < 10)? '0' + num : num ;
	}
	
	function getXMLNodeFromText(text,node){
	  var text = text.replace(/&amp;nbsp;/g,"&nbsp;").replace(/&lt;/g,"<").replace(/&gt;/g,">");
	  var startpos = text.indexOf('>', text.indexOf('<' + node + ' ', 1)) + 1;
	  return text.substring(startpos,text.indexOf('</' + node + '>', startpos));
	}  
	
	
	
	
	//Load revision properties library does add a popup window feature
	var _timer, script;	
	try{
	if(window.loadRevisionProperties) {
		var revisionProperties = {
			loadData: function(){
				try{
					if (arguments.callee.done) return;
			    arguments.callee.done = true;
			    if (_timer) clearInterval(_timer);	
					
					var _temp = ajaxActionFile;
					ajaxActionFile = libURL + "/client_storage/revisionProperties.xml";
					new makeAjaxRequest('', 'get', {parameters: ''}, revisionProperties.parseData); 
					ajaxActionFile = _temp;
				} catch(err){}
			},
			parseData: function(xml){
				try{
					var xmlDataArray = defineDataFromXML(xml);
					revisionProperties.parseLinks(xmlDataArray);
				} catch(err){}
			},
			openPopupWindow: function(config){
				 var newWindow = window.open(config.chref,null,"top=" + config.popup_top + ", left=" + config.popup_left + ", width=" + config.popup_width + ", height=" + config.popup_height + ", toolbar=" + config.popup_toolbar +", menubar=" + config.popup_menubar + ", location=" + config.popup_location_bar + ", scrollbars=" + config.popup_scrollbars + ", status=" + config.popup_status_bar + ", resizable=" + config.popup_resizable);
				 try{
				 	newWindow.focus();
				} catch(e) {
					//alert("Please disable Pop Up blocker, or hold shift button!");
				}
			},
			parseLinks: function(xmldata){
				var anchors = document.getElementsByTagName('a');
				var result; 
				var rexp = /(ci_id[=][0123456789]*|ci_id[\/][0123456789]*)/g;	
				var rexpdig = /^\d+$/;				
				for(var a = 0; a < anchors.length; a++){
					var ll = anchors[a].href;										
					if(result = new RegExp(rexp).exec(ll))
						if(parseInt(result[0].split('=')[1]))
							result = parseInt(result[0].split('=')[1]);
						else if(parseInt(result[0].split('/')[1]))
							result = parseInt(result[0].split('/')[1]);
						else 
							result = null;
					
					if(result) {
						var arrayV = searchPrismArray(xmldata, 'contentItem_' + result);						
						if(arrayV!=-1) {
							var ci_id = xmldata[xmldata[arrayV] + '_ci_id'];
							var popup_left = xmldata[xmldata[arrayV] + '_popup_left'];
							var popup_top = xmldata[xmldata[arrayV] + '_popup_top'];
							var popup_width = xmldata[xmldata[arrayV] + '_popup_width'];
							var popup_height = xmldata[xmldata[arrayV] + '_popup_height'];
							var popup_menubar = xmldata[xmldata[arrayV] + '_popup_menubar'];
							var popup_toolbar = xmldata[xmldata[arrayV] + '_popup_toolbar'];
							var popup_location_bar = xmldata[xmldata[arrayV] + '_popup_location_bar'];
							var popup_directories = xmldata[xmldata[arrayV] + '_popup_directories'];
							var popup_status_bar = xmldata[xmldata[arrayV] + '_popup_status_bar'];
							var popup_scrollbars = xmldata[xmldata[arrayV] + '_popup_scrollbars'];
							var popup_resizable = xmldata[xmldata[arrayV] + '_popup_resizable'];
							var popup_fullscreen = xmldata[xmldata[arrayV] + '_popup_fullscreen'];
							var popup_channelmode = xmldata[xmldata[arrayV] + '_popup_channelmode'];
							var popup_dependent = xmldata[xmldata[arrayV] + '_popup_dependent'];							
							var chref = ll;													
							var	config = {chref: chref, ci_id: ci_id, popup_left: popup_left, popup_top: popup_top, popup_width: popup_width, popup_height: popup_height, popup_menubar: popup_menubar, popup_toolbar: popup_toolbar, popup_location_bar: popup_location_bar, popup_directories: popup_directories, popup_status_bar: popup_status_bar, popup_scrollbars: popup_scrollbars, popup_resizable: popup_resizable, popup_fullscreen: popup_fullscreen, popup_channelmode: popup_channelmode, popup_dependent: popup_dependent};
							anchors[a].href = 'javascript:;';	
							anchors[a].config = config;
							anchors[a].onclick = function(){revisionProperties.openPopupWindow(this.config)};	
						}						
					}
					
				}				
			}		
		}	
		
		/* for Mozilla/Opera9 */
		if (document.addEventListener) {
		    document.addEventListener("DOMContentLoaded", revisionProperties.loadData, false);		
		}	else if (navigator.userAgent.toLowerCase().indexOf('msie 7')>-1) {	/* for Internet Explorer 7 */
			document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
			script = document.getElementById("__ie_onload");
			script.onreadystatechange = function() {
			    if (this.readyState == "complete") {
			        revisionProperties.loadData(); 
			    }
			};
		} else if (navigator.userAgent.toLowerCase().indexOf('webkit')>-1) {	/* for Safari */
		    _timer = setInterval(function() {
		        if (/loaded|complete/.test(document.readyState)) {
		            revisionProperties.loadData();
		        }
		    }, 10);
		} else {		/* for other browsers */
			window.onload = revisionProperties.loadData;
		}
		
	}
	}catch(err){}