//Javascript Code File for 
//EPSBuildings.com Dealer Administration Site

//CODE BEGINS

var eps_preLoadConstructionPage;
var eps_preLoadPhotoGalleryCategory;

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function init_object() {
        var x;
        try {
                x=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        x=new ActiveXObject("Microsoft.XMLHTTP");
                } catch (oc) {
                        x=null;
                }
        }      
        if(!x && typeof XMLHttpRequest != "undefined")
                x = new XMLHttpRequest();
        if (x)
                return x;
}
function fix_url(url){
	var day = new Date(); 
	var id = day.getTime(); 
	var randomnumber = String(day+id);
	var rightthree=Right(url,3);
	return url;	
}
function open_url(url, target, iteration) {
	url=fix_url(url)
	this.x=init_object();
	this.x.open("GET", url, true);
	this.x.onreadystatechange = function() { 
	 	if (self.x.readyState == 4) {
			data = self.x.responseText.split("||JavaScriptCode||");
			if (iteration != null){
				if (self.x.status!=200){
					reportError("Unable to reach " + url + ": ERROR " + self.x.status);
				}else{
					if (data[0]!=null){document.getElementsByName(target)[iteration].innerHTML = data[0];}
				}
			}else{
				document.getElementById(target).innerHTML = data[0];
			}
			eval(data[1]);
		} 
	}
 	this.x.send('');
}


function append_text(target, vtext) {
 	if ( ! document.getElementById) {
  		return false;
 	}
	document.getElementById(target).innerHTML += vtext;
}	
function replace_text(target, vtext) {
 	if ( ! document.getElementById) {
  		return false;
 	}
	if (document.getElementById(target)!=null){
		document.getElementById(target).innerHTML = vtext;
	}
}	
function chkObject(theVal){
	var objectCheck=document.getElementById(theVal);
	if (objectCheck==null){
		return false;
    }else{
        return true;
    }
}
function reportError(ec){
	if (chkObject("eps_errorcode")){
		append_text("eps_errorcode", "<div>" + ec + "</div>");
	}
}
function loadXMLDoc(dname) 
{
var xmlDoc;
if (window.XMLHttpRequest)
  {
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET",dname,false);
  xmlDoc.send("");
  if(xmlDoc.status!=200){
	reportError("File Missing: " + dname);
  	return null;
  }else{
  	return xmlDoc.responseXML;
  }
 }
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(dname);
  if(xmlDoc.status!=200){
	  reportError("File Missing: " + dname);
	  return null;
	}else{
  	return xmlDoc;
  }
 }
return null;
}


function getArgs() { 
	var args = new Object(); 
	var query = location.search.substring(1); 
	var pairs = query.split("&"); 
	for(var i = 0; i < pairs.length; i++) { 
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue; 
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1); 
		args[argname] = unescape(value); 
	} 
	return args; 
} 






//End Standard Control Functions
function initializeDealerContentSystem(){
	//add small logo
	if (chkObject("eps_smalllogo")){
		elementCount = document.getElementsByName("eps_smalllogo").length;
		if(elementCount==null){elementCount=1;}
		for (i=0; i<elementCount; i++) {
			document.getElementsByName("eps_smalllogo")[i].innerHTML="<img src='http://epsbuildings.com/images/logo.jpg'>";
		}
	}
	
	//photo gallery code
	if (chkObject("eps_photogallery_menu")){
		x1_elementCount = document.getElementsByName("eps_photogallery_menu").length;
		xmlDoc=loadXMLDoc("config.xml");
		if (xmlDoc !=null){
			additionalInfo="?a=1";
			if (xmlDoc.getElementsByTagName("image_width")!=null){additionalInfo+="&width=" + xmlDoc.getElementsByTagName("image_width")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("image_height")!=null){additionalInfo+="&height=" + xmlDoc.getElementsByTagName("image_height")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("thumbimage_width")!=null){additionalInfo+="&thumbwidth=" + xmlDoc.getElementsByTagName("thumbimage_width")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("thumbimage_height")!=null){additionalInfo+="&thumbheight=" + xmlDoc.getElementsByTagName("thumbimage_height")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("max_columns")!=null){additionalInfo+="&columns=" + xmlDoc.getElementsByTagName("max_columns")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("max_rows")!=null){additionalInfo+="&rows=" + xmlDoc.getElementsByTagName("max_rows")[0].childNodes[0].nodeValue;}
			if (eps_preLoadPhotoGalleryCategory!=null){additionalInfo+="&category=" + eps_preLoadPhotoGalleryCategory;}
		}else{
			additionalInfo="";
			if (eps_preLoadPhotoGalleryCategory!=null){additionalInfo+="?category=" + eps_preLoadPhotoGalleryCategory;}
		}
		var x1;
		try {
			x1=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
        	try {
            	x1=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (oc) {
            	x1=null;
            }
		}      
        if(!x1 && typeof XMLHttpRequest != "undefined")
		x1 = new XMLHttpRequest();
		x1_url=fix_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/gallery.asp" + additionalInfo));
		x1.open("GET", x1_url, true);
		x1.onreadystatechange = function() { 
			if (x1.readyState == 4) {
				data = x1.responseText.split("||JavaScriptCode||");
				if (x1.status!=200){
					reportError("Unable to reach " + x1_url + ": ERROR " + x1.status);
				}else{
					if (x1_elementCount != null){	
						for (x1_i=0; x1_i<x1_elementCount; x1_i++) {
							if (data[0]!=null){document.getElementsByName("eps_photogallery_menu")[x1_i].innerHTML = data[0];}
						}
					}else{
						document.getElementById("eps_photogallery_menu").innerHTML = data[0];
					}
					eval(data[1]);
				}
			} 
		}
		x1.send('');
	}

	//construction page menu code
	if (chkObject("eps_constructionpage_menu")){
		x2_elementCount = document.getElementsByName("eps_constructionpage_menu").length;
		x2_url=fix_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/constructionpagelist.asp"));
		var x2;
		try {
           	x2=new ActiveXObject("Msxml2.XMLHTTP");
	       } catch (e) {
       	       try {
                 x2=new ActiveXObject("Microsoft.XMLHTTP");
           } catch (oc) {
             x2=null;
          	}
	     }      
       	if(!x2 && typeof XMLHttpRequest != "undefined")
        x2 = new XMLHttpRequest();
		x2.open("GET", x2_url, true);
		x2.onreadystatechange = function() { 
			if (x2.readyState == 4) {
				data = x2.responseText.split("||JavaScriptCode||");
				if (x2.status!=200){
					reportError("Unable to reach " + x2_url + ": ERROR " + x2.status);
				}else{
					if (x2_elementCount != null && x2_elementCount!=0){	
						for (x2_i=0; x2_i<x2_elementCount; x2_i++) {
							if (data[0]!=null){document.getElementsByName("eps_constructionpage_menu")[x2_i].innerHTML = data[0];}
						}
					}else{
						document.getElementById("eps_constructionpage_menu").innerHTML = data[0];
					}
					eval(data[1]);
				}
			} 
		}
		x2.send('');
	}
	
	
	
	var args = getArgs(); 
	if (args.preload_construction_page) eps_preLoadConstructionPage = parseInt(args.preload_construction_page); 
	//check for preloaded construction page
	if (eps_preLoadConstructionPage!=null){
		if (chkObject("eps_constructionpage")){
			xmlDoc2=loadXMLDoc("config.xml");
			if (xmlDoc2 !=null){
				additionalInfo="&a=1";
				if (xmlDoc2.getElementsByTagName("original_css")[0]!=null){
					additionalInfo+="&retaincss=" + xmlDoc2.getElementsByTagName("original_css")[0].childNodes[0].nodeValue;
				}
			}else{
				additionalInfo="";
			}
			x3_elementCount = document.getElementsByName("eps_constructionpage").length;
			x3_url=fix_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/constructionpages.asp?ext=php&id=" + eps_preLoadConstructionPage + additionalInfo));
			var x3;
			try {
				x3=new ActiveXObject("Msxml2.XMLHTTP");
			   } catch (e) {
				   try {
					 x3=new ActiveXObject("Microsoft.XMLHTTP");
			   } catch (oc) {
				 x3=null;
				}
			 }      
			if(!x3 && typeof XMLHttpRequest != "undefined")
			x3 = new XMLHttpRequest();
			x3.open("GET", x3_url, true);
			x3.onreadystatechange = function() { 
				if (x3.readyState == 4) {
					data = x3.responseText.split("||JavaScriptCode||");
					if (x3.status!=200){
						reportError("Unable to reach " + x3_url + ": ERROR " + x3.status);
					}else{
						if (x3_elementCount != null && x3_elementCount!=0){	
							for (x3_i=0; x3_i<x3_elementCount; x3_i++) {
								if (data[0]!=null){document.getElementsByName("eps_constructionpage")[x3_i].innerHTML = data[0];}
							}
						}else{
							document.getElementById("eps_constructionpage").innerHTML = data[0];
						}
						eval(data[1]);
					}
				} 
			}
			x3.send('');
		}
	}
	
	//construction page menu code (FORWARDER)
	if (chkObject("eps_constructionpage_menu_forward")){
		x4_elementCount = document.getElementsByName("eps_constructionpage_menu_forward").length;
		x4_url=fix_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/constructionpagelist_forward.asp"));
		var x4;
		try {
           	x4=new ActiveXObject("Msxml2.XMLHTTP");
	       } catch (e) {
       	       try {
                 x4=new ActiveXObject("Microsoft.XMLHTTP");
           } catch (oc) {
             x4=null;
          	}
	     }      
       	if(!x4 && typeof XMLHttpRequest != "undefined")
        x4 = new XMLHttpRequest();
		x4.open("GET", x4_url, true);
		x4.onreadystatechange = function() { 
			if (x4.readyState == 4) {
				data = x4.responseText.split("||JavaScriptCode||");
				if (x4.status!=200){
					reportError("Unable to reach " + x4_url + ": ERROR " + x4.status);
				}else{
					if (x4_elementCount != null && x4_elementCount!=0){	
						for (x4_i=0; x4_i<x4_elementCount; x4_i++) {
							if (data[0]!=null){document.getElementsByName("eps_constructionpage_menu_forward")[x4_i].innerHTML = data[0];}
						}
					}else{
						document.getElementById("eps_constructionpage_menu_forward").innerHTML = data[0];
					}
					eval(data[1]);
				}
			} 
		}
		x4.send('');
	}


	//URL EXAMPLE CODE - DO NOT USE
	if (chkObject("google")){
		elementCount = document.getElementsByName("google").length;
		xmlDoc=loadXMLDoc("config.xml");
		if (xmlDoc !=null){
			additionalInfo="?id=" + xmlDoc.getElementsByTagName("image_width")[0].childNodes[0].nodeValue;
		}else{
			additionalInfo="";
		}
		if(elementCount==null){elementCount=1;}
		for (i=0; i<elementCount; i++) {
			open_url("control.php?url=" + escape("http://www.epsbuildings.com/" + additionalInfo), "google", i);
		}
	}
}

function eps_photoMenu_reload(){
	if (chkObject("eps_photogallery_menu")){
		elementCount = document.getElementsByName("eps_photogallery_menu").length;
		xmlDoc=loadXMLDoc("config.xml");
		if (currentPage==null){currentPage=1}
		if (currentCategory==null){currentCategory=0}
		if (xmlDoc !=null){
			additionalInfo="?a=1";
			if (xmlDoc.getElementsByTagName("image_width")!=null){additionalInfo+="&width=" + xmlDoc.getElementsByTagName("image_width")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("image_height")!=null){additionalInfo+="&height=" + xmlDoc.getElementsByTagName("image_height")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("thumbimage_width")!=null){additionalInfo+="&thumbwidth=" + xmlDoc.getElementsByTagName("thumbimage_width")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("thumbimage_height")!=null){additionalInfo+="&thumbheight=" + xmlDoc.getElementsByTagName("thumbimage_height")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("max_columns")!=null){additionalInfo+="&columns=" + xmlDoc.getElementsByTagName("max_columns")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("max_rows")!=null){additionalInfo+="&rows=" + xmlDoc.getElementsByTagName("max_rows")[0].childNodes[0].nodeValue;}
			additionalInfo+="&page=" + currentPage;
			additionalInfo+="&category=" + currentCategory;
		}else{
			additionalInfo="?page=" + currentPage;
			additionalInfo+="&category=" + currentCategory;
		}
		if(elementCount==null){elementCount=1;}
		for (i=0; i<elementCount; i++) {
			open_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/gallery.asp" + additionalInfo), "eps_photogallery_menu", i);
		}
	}

}

function eps_photodisplay(photoID){
	if (chkObject("eps_photogallery_display")){
		elementCount = document.getElementsByName("eps_photogallery_display").length;
		xmlDoc=loadXMLDoc("config.xml");
		additionalInfo="?id=" + photoID;
		if (xmlDoc !=null){
			if (xmlDoc.getElementsByTagName("image_width")!=null){additionalInfo+="&width=" + xmlDoc.getElementsByTagName("image_width")[0].childNodes[0].nodeValue;}
			if (xmlDoc.getElementsByTagName("image_height")!=null){additionalInfo+="&height=" + xmlDoc.getElementsByTagName("image_height")[0].childNodes[0].nodeValue;}
		}
		if(elementCount==null){elementCount=1;}
		for (i=0; i<elementCount; i++) {
			open_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/galleryimage.asp" + additionalInfo), "eps_photogallery_display", i);
		}
	}
}

function eps_loadConstructionPage(pageID){
	if (chkObject("eps_constructionpage")){
		xmlDoc=loadXMLDoc("config.xml");
		if (xmlDoc !=null){
			additionalInfo="&a=1";
			if (xmlDoc.getElementsByTagName("original_css")[0]!=null){additionalInfo+="&retaincss=" + xmlDoc.getElementsByTagName("original_css")[0].childNodes[0].nodeValue;}
		}else{
			additionalInfo="";
		}

		elementCount = document.getElementsByName("eps_constructionpage").length;
		if(elementCount==null){elementCount=1;}
		for (i=0; i<elementCount; i++) {
			open_url("control.php?url=" + escape("http://www.epsbuildings.com/xml/constructionpages.asp?ext=php&id=" + pageID + additionalInfo), "eps_constructionpage", i);
		}
	}
}

//Initialize Content System
window.onload = initializeDealerContentSystem;
