// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}
var adURL = "";
function popupAds(){
	var dateObj = new Date();
	var callURL = adURL+ "&serviceName=BVCare&d="+dateObj.getTime() ;
	//var callURL = "http://192.168.1.53/cgi-bin/MMOnline.dll/portal/ep/common/popupAd.jsp?tabId=POP_ID_11"+ "&d="+dateObj.getTime() ;
	var jsonObj=new JSONscriptRequest(callURL+'&callback=popAds'  );     
	//alert("Calling Page = "+jsonObj.fullUrl );
	//alert(layerName);
	jsonObj.buildScriptTag(); 
	jsonObj.addScriptTag(); 
}

function popAds(resOb){
if(resOb==null){
	//alert("nothing");
   //do something
}else{
	var type= "" , adSript = "";
	 var totalObj = 0 ;
	 for (x in resOb) totalObj++;
	 //alert(totalObj);
	 for( var g = 0 ; g < totalObj ; g++){
		 type = eval( "resOb.POPUP_"+g+".type");
		 script = eval( "resOb.POPUP_"+g+".adscript") ;
		// alert( g+")"+ eval( "resOb.POPUP_"+g+".type")  );
		 // alert(  g+")"+ eval( "resOb.POPUP_"+g+".adscript")  );
		 if( type != null && script != null && type.indexOf("POPUP") > -1 ){
		 	//	 alert("calling popup "+script);
			  launchPopupAds( script );
		 }else  if(  type != null && script != null &&  type.indexOf("SCRIPTONLY") > -1 ){
		 // alert("calling SCRIPTONLY "+script);
			   launchScripts( script );
		  }
	 }// for 
  }
}

function launchScripts(responseText) {
// RegExp from prototype.sonio.net
var popupScripts = false  ;
var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';

var match = new RegExp(ScriptFragment, 'img');
var scripts = responseText.match(match);
if(scripts) {
	var js = '';
	var heads = document.getElementsByTagName('head').item(0);
	var scriptObj = document.createElement('script');
	for(var s = 0; s < scripts.length; s++) {
		if ( scripts[s].indexOf("src=") != -1  ) {
			var srcIndex=scripts[s].indexOf("src=");
			var tempSrc=scripts[s].substring(srcIndex+5);
			var qouteIndex=tempSrc.indexOf(".js\"");
			var srcStr=tempSrc.substring(0, qouteIndex+3);
			//alert(srcStr);
			scriptObj.setAttribute('language' , 'javascript' );
			scriptObj.setAttribute('type' , 'text/javascript' );
			scriptObj.setAttribute('src' , srcStr );
			heads.appendChild(scriptObj);
		}  // src
	}  // for 
   }  // scripts
} // fucntion

function launchPopupAds(responseText) {
// RegExp from prototype.sonio.net
var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';
var match = new RegExp(ScriptFragment, 'img');
var scripts = responseText.match(match);
if(scripts) {
	var js = '';
	var heads = document.getElementsByTagName('head').item(0);
	var scriptObj = document.createElement('script');
	for(var s = 0; s < scripts.length; s++) {
	if ( scripts[s].indexOf("src=") == -1 ) {
		var match = new RegExp(ScriptFragment, 'im');
			js += scripts[s].match(match)[1];
		}
	}
	//alert(  "evaluating .... "+  js);
	eval(js); 
  }// scripts
}

function getexpirydate( nodays){
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}
function getcookie(cookiename) {
	 var cookiestring=""+document.cookie;
	 var index1=cookiestring.indexOf(cookiename);
	 if (index1==-1 || cookiename=="") return ""; 
	 var index2=cookiestring.indexOf(';',index1);
	 if (index2==-1) index2=cookiestring.length; 
	 return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value,domainName,duration){
		//cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
		cookiestring=name+"="+escape(value)+domainName;  /// Cookie Exists for the Browser session
		document.cookie=cookiestring;
		if(!getcookie(name))return false;
		else	return true;
}

/////
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
function trim( value ) {
	return LTrim(RTrim(value));
}
/////
