/* Thickbox 2.1 - jQuery plugin for displaying content in a box above the page
 * Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
$(document).ready(TB_init);
function TB_init(){
	$("a.thickbox").click(function(event){
		event.preventDefault();
		this.blur();
	    var caption = this.title || this.name || "";
        var group = this.rel || false;
		TB_show(caption, this.href, group);
	});
}
function TB_show(caption, url, rel) {

	if ( !$("#TB_HideSelect").length ) {
		//$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
		$("#TB_overlay").click(TB_remove);
	}
	$(window).scroll(TB_position);
	TB_overlaySize();
	$("body").append("<div id='TB_load'><img src='http://secure.eodessa.com/estock/themes/theme_4/images/progressbar.gif' /></div>");
	TB_load_position();
	var baseURL = url.match(/(.+)?/)[1] || url;
    var imageURL = /\.(jpe?g|png|gif|bmp)/gi;
    if ( baseURL.match(imageURL) ) {
		var dummy = { caption: "", url: "", html: "" };
		var prev = dummy,
			next = dummy,
			imageCount = "";
		if ( rel ) {
			function getInfo(image, id, label) {
				return {
					caption: image.title,
					url: image.href,
					html: "<span id='TB_" + id + "'>&nbsp;&nbsp;<a href='#'>" + label + "</a></span>"
				}
			}
		var imageGroup = $("a[@rel="+rel+"]").get();
			var foundSelf = false;
			for (var i = 0; i < imageGroup.length; i++) {
				var image = imageGroup[i];
				var urlTypeTemp = image.href.match(imageURL);
				if ( image.href == url ) {
					foundSelf = true;
					imageCount = "Image " + (i + 1) + " of "+ (imageGroup.length);
				} else {
					if ( foundSelf ) {
						next = getInfo(image, "next", "Next &gt;");
						break;
					} else {
						prev = getInfo(image, "prev", "&lt; Prev");
					}
				}
			}
		}		
		imgPreloader = new Image();
		imgPreloader.onload = function() {
		imgPreloader.onload = null;
        var pagesize = TB_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			if (imageWidth > x) {
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x; 
				if (imageHeight > y) { 
					imageWidth = imageWidth * (y / imageHeight); 
					imageHeight = y; 
				}
			} else if (imageHeight > y) { 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
				if (imageWidth > x) { 
					imageHeight = imageHeight * (x / imageWidth); 
					imageWidth = x;
				}
			}
			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;
			$("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + imageCount + prev.html + next.html + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div>");
			
			$("#TB_closeWindowButton").click(TB_remove);
			
			function buildClickHandler(image) {
				return function() {
					$("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					TB_show(image.caption, image.url, rel);
					return false;
				};
			}
			var goPrev = buildClickHandler(prev);
			var goNext = buildClickHandler(next);
			if ( prev.html ) {
				$("#TB_prev").click(goPrev);
			}			
			if ( next.html ) {		
				$("#TB_next").click(goNext);
			}
			document.onkeydown = function(e) {
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				switch(keycode) {
				case 27:
					TB_remove();
					break;
				case 190:
					if( next.html ) {
						document.onkeydown = null;
						goNext();
					}
					break;
				case 188:
					if( prev.html ) {
						document.onkeydown = null;
						goPrev();
					}
					break;
				}
			}
			TB_position();
			$("#TB_load").remove();
			$("#TB_ImageOff").click(TB_remove);
			$("#TB_window").css({display:"block"});
		}
		imgPreloader.src = url;		
	} else { //code to show html pages
		var queryString = url.match(/\?(.+)/)[1];
		var params = TB_parseQuery( queryString );
		TB_WIDTH = (params['width']*1) + 30;
		TB_HEIGHT = (params['height']*1) + 40;
		var ajaxContentW = TB_WIDTH - 30,
			ajaxContentH = TB_HEIGHT - 45;
		if(url.indexOf('TB_iframe') != -1){	
			urlNoQuery = url.split('&TB_');		
			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' onload='TB_showIframe()'> </iframe>");
		} else {
			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
		}				
		$("#TB_closeWindowButton").click(TB_remove);		
			if(url.indexOf('TB_inline') != -1){	
				$("#TB_ajaxContent").html($('#' + params['inlineId']).html());
				TB_position();
				$("#TB_load").remove();
				$("#TB_window").css({display:"block"}); 
			}else if(url.indexOf('TB_iframe') != -1){
				TB_position();
				if(frames['TB_iframeContent'] == undefined){//be nice to safari
					$("#TB_load").remove();
					$("#TB_window").css({display:"block"});
					$(document).keyup( function(e){ var key = e.keyCode; if(key == 27){TB_remove()} });					
				}
			}else{				
				$("#TB_ajaxContent").load(url, function(){
					TB_position();
					$("#TB_load").remove();
					$("#TB_window").css({display:"block"}); 					
				});
			}		
	}	
	$(window).resize(TB_position);
	document.onkeyup = function(e){ 	
		if (e == null) { // ie
			keycode = event.keyCode;
			keycode = e.which;		} else { // mozilla
		}
		if(keycode == 27){ // close
			TB_remove();
		}	
	}		
}
function TB_iframeWithCloseReload(caption, url, width, height) {
	try {	
	  $("#TB_overlay").css({display:"block"});
	    $("#TB_HideSelect").css({display:"block"});
	    if (document.getElementById("TB_HideSelect") == null) {
		    //$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
		    $("#TB_overlay").click(TB_remove);
		}			    
		if(caption==null){caption=""};
		try{ $(window).scroll(TB_position); } catch(err){ } 		
		TB_overlaySize();
		$("body").append("<div id='TB_load'><img src='http://secure.eodessa.com/estock/themes/theme_4/images/progressbar.gif' /></div>");
		TB_load_position();	    	
			TB_WIDTH = width;
			TB_HEIGHT = height;
			ajaxContentW = TB_WIDTH;
			ajaxContentH = TB_HEIGHT - 45;
			
			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWithReload' title='Close'>close</a></div></div>"+
			    "<iframe frameborder='0' hspace='0' src='"+url+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+TB_WIDTH+"px;height:"+TB_HEIGHT+"px;' onload='TB_showIframe()'> </iframe>");
		    $("#TB_closeWithReload").click(TB_removeAndreload);			
			TB_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 
			
		try{ $(window).resize(TB_position); } catch(err){}
		
		document.onkeyup = function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				TB_remove();
			}	
		}		    
	} catch(e) {}
}
function TB_iframe(caption, url, width, height) {
	try {	
	    $("#TB_overlay").css({display:"block"});
	    $("#TB_HideSelect").css({display:"block"});
	    if (document.getElementById("TB_HideSelect") == null) {
		    //$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
		    $("#TB_overlay").click(TB_remove);
		}			    
		if(caption==null){caption=""};
		try{ $(window).scroll(TB_position); } catch(err){ } 		
		TB_overlaySize();
		
		$("body").append("<div id='TB_load'><img src='http://secure.eodessa.com/estock/themes/theme_4/images/progressbar.gif' /></div>");
		TB_load_position();	    	
			TB_WIDTH = width;
			TB_HEIGHT = height;
			ajaxContentW = TB_WIDTH;
			ajaxContentH = TB_HEIGHT - 45;
			
			$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div></div>"+
			    "<iframe frameborder='0' hspace='0' src='"+url+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+TB_WIDTH+"px;height:"+TB_HEIGHT+"px;' onload='TB_showIframe()'> </iframe>");
				
			$("#TB_closeWindowButton").click(TB_remove);			
			TB_position();
			$("#TB_load").remove();
			$("#TB_window").css({display:"block"}); 
			
		try{ $(window).resize(TB_position); } catch(err){}
		
		document.onkeyup = function(e){ 	
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if(keycode == 27){ // close
				TB_remove();
			}	
		}		    
	} catch(e) {}
}
function TB_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function TB_remove() {
 	try{  
 	//$("#TB_imageOff").unclick();
	//$("#TB_overlay").unclick();
    //$("#TB_closeWindowButton").unclick();
	$("#TB_window").css({display:"none"});
	$("#TB_overlay").css({display:"none"});
	$("#TB_HideSelect").css({display:"none"});
	$("#TB_window").html(""); 
	$("#TB_overlay").html(""); 
	$("#TB_HideSelect").html(""); 
	$("#TB_load").remove();
	} catch(err){}
	return false;
}
function TB_removeAndreload() {
 	try{  
 	$("#TB_window").css({display:"none"});
	$("#TB_overlay").css({display:"none"});
	$("#TB_HideSelect").css({display:"none"});
	$("#TB_window").html(""); 
	$("#TB_overlay").html(""); 
	$("#TB_HideSelect").html(""); 
	$("#TB_load").remove();
	window.location = window.location;
	} catch(err){}
	return false;
}
function TB_position() {
	var pagesize = TB_getPageSize();	
	var arrayPageScroll = TB_getPageScrollTop();
	var left = (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2) + "px";
	var top = (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+ "px";
	var w = TB_WIDTH+"px";
	$("#TB_window").css("width",w);
	$("#TB_window").css("top",top);
	$("#TB_window").css("left",left);
}
function TB_overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$("#TB_overlay").css({"height": yScroll, "width": xScroll});
	$("#TB_HideSelect").css({"height": yScroll,"width": xScroll});
}

function TB_load_position() {
	var pagesize = TB_getPageSize();
	var arrayPageScroll = TB_getPageScrollTop();
	$("#TB_load")
		.css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2), top: (arrayPageScroll[1] + ((pagesize[1]-100)/2)) })
		.css({display:"block"});
}

function TB_parseQuery ( query ) {
	if( !query )
		return {};
	var params = {};
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('=');
		if ( !pair || pair.length != 2 )
			continue;
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}

function TB_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}

function TB_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}

function ShowLogin()
{
    document.getElementById('BGDiv').style.display = 'inline';
    document.getElementById('LoginDiv').style.display = 'inline';
    SetPosition()            
    window.onscroll = SetPosition;
    window.resize = SetPosition;
}
function SetPosition()
{
    try
    {
        document.getElementById('BGDiv').style.width = getSize()[0] + 'px';
        document.getElementById('BGDiv').style.height = getSize()[1] + 'px';
        document.getElementById('BGDiv').style.left = getScrollXY()[0] + 'px';
        document.getElementById('BGDiv').style.top = getScrollXY()[1] + 'px';
    }
    catch(event)
            {
            }            
    try
    {
        var top 
        top = (getSize()[1]/2) + getScrollXY()[1] - 225
        document.getElementById('LoginDiv').style.top =  top + 'px';
		var left
        left = (getSize()[0]/2) + getScrollXY()[0] - 125
        document.getElementById('LoginDiv').style.left = left + 'px';
    }catch(event){           }
}
function getSize() 
{
     var myWidth = 0, myHeight = 0;
     if( typeof( window.innerWidth ) == 'number' ) 
     {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
     } 
     else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
     {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
     } 
     else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
     {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
     }
     return [ myWidth, myHeight ];
}
function getScrollXY() 
{
     var scrOfX = 0, scrOfY = 0;
     if( typeof( window.pageYOffset ) == 'number' ) 
     {
                //Netscape compliant
                scrOfY = window.pageYOffset;
                scrOfX = window.pageXOffset;
     } 
     else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
     {
                //DOM compliant
                scrOfY = document.body.scrollTop;
                scrOfX = document.body.scrollLeft;
     } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
     {
                //IE6 standards compliant mode
                scrOfY = document.documentElement.scrollTop;
                scrOfX = document.documentElement.scrollLeft;
     }
            return [ scrOfX, scrOfY ];
}
function HideDialog()
{
     document.getElementById('BGDiv').style.display = 'none'; 
     document.getElementById('LoginDiv').style.display = 'none';
}  
function AddAdditionalFile()
{
	document.getElementById("AdditionalFileCount").value = afCount;	
	var tbl = document.getElementById("AdditionalFiles");
	var tbody = tbl.getElementsByTagName("tbody")[0];
	var row = document.createElement("tr");
	var col = document.createElement("td");
	var inf = document.createElement("input");	
	inf.type= "file";
	inf.id  = "afile_" + afCount;
	inf.name= inf.id;
	inf.size= 40;	
	col.appendChild(inf);
	row.appendChild(col);
	tbody.appendChild(row);	
	afCount++;
}
function RemoveAdditionalFile()
{
	var tbl = document.getElementById("AdditionalFiles");	
	if (tbl.rows.length > 1)
	{
		tbl.deleteRow(tbl.rows.length - 1);
	}
}  
function getHttpObject() {
    var objType = false;
    try {
        objType = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            objType = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            objType = new XMLHttpRequest();
        }
    }
    return objType;
}
function getAXAH(url){
	var theHttpRequest = getHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH();};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);
		function processAXAH(){
		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {			   
					return theHttpRequest;			    
			   } else {
					alert("Error: HTTP request return the following status message: " + theHttpRequest.statusText);;
			   }
		   }
		}
}
function LoginMe(url)
{
	var geturl = url+"service1.asmx/Login?email="+$("#UserEmail").attr("value")+"&password="+$("#UserPassword").attr("value");
	var theHttpRequest = getHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH();};
	theHttpRequest.open("GET", geturl);
	theHttpRequest.send(false);
		function processAXAH(){
		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {			   
					var myXML = ExtractXmlFromTheWebService(theHttpRequest); 			                                                           
					for (var i = 0; i<myXML.length; i++) 
					{				 
						var t = myXML[i];
						var customerid= ExtractValueFromNode(t, "customerid");
						if(customerid=="0")
							$("#LoginError").html("Invalid login information");
						else
							window.location = url+"index.aspx?guid="+customerid;
					}			    
			   } else {
					alert("Error: HTTP request return the following status message: " + theHttpRequest.statusText);;
			   }
		   }
		}
}
function SignupMe(url)
{
	if($("#UserEmail1").attr("value")=="")
	{
		$("#LoginError1").html("Email can't be blank.");
		$("#UserEmail1").focus();
		return;
	}
	if($("#UserPassword1").attr("value")=="")
	{
		$("#LoginError1").html("Password can't be blank.");
		$("#UserPassword1").focus();
		return;
	}
	if($("#FirstName").attr("value")=="")
	{
		$("#LoginError1").html("First Name can't be blank.");
		$("#FirstName").focus();
		return;
	}
	if($("#LastName").attr("value")=="")
	{
		$("#LoginError1").html("Last Name can't be blank.");
		$("#LastName").focus();
		return;
	}
	var geturl = url+"service1.asmx/CreateAccount?email="+$("#UserEmail1").attr("value")+"&password="+$("#UserPassword1").attr("value")+"&firstname="+$("#FirstName").attr("value")+"&lastname="+$("#LastName").attr("value");
	var theHttpRequest = getHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH();};
	theHttpRequest.open("GET", geturl);
	theHttpRequest.send(false);
		function processAXAH(){
		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {			   
					var myXML = ExtractXmlFromTheWebService(theHttpRequest); 			                                                           
					for (var i = 0; i<myXML.length; i++) 
					{				 
						var t = myXML[i];
						var customerid= ExtractValueFromNode(t, "customerid");
						if(customerid=="0")
							$("#LoginError1").html("Can't create account, please contact site administrator");
						else
							window.location = url+"index.aspx?guid="+customerid;
					}			    
			   } else {
					alert("Error: HTTP request return the following status message: " + theHttpRequest.statusText);;
			   }
		   }
		}
}
function ExtractXmlFromTheWebService(httpRequest)
{
    var xmlDoc = httpRequest.responseXML;//responseText;    
    if(xmlDoc != null)
        return xmlDoc.getElementsByTagName("Table");
    else
        return "";
}
function ExtractXmlFromWebService(httpRequest, startNodeName)
{
    var xmlDoc = httpRequest.responseXML;               
    if(xmlDoc != null)
      return xmlDoc.getElementsByTagName(startNodeName);
    else
      return "";
}
function ExtractValueFromNode(xmlString, node){
  return (xmlString.getElementsByTagName(node)[0].firstChild != null ? xmlString.getElementsByTagName(node)[0].firstChild.nodeValue : "");
}
function PagerMOver(el)
{
el.style.color='white';el.style.borderColor='black';el.style.backgroundColor='#FF6600';
}
function PagerMOut(el)
{
el.style.color='black';el.style.borderColor='white';el.style.backgroundColor='white';
}
 
