// JavaScript Document

var displaying = false;


function removeCommentPopup(srcObj)
{
	if (displaying) 
	{
		var bodyObj = document.getElementsByTagName("body")[0];
		
		var infoObj = document.getElementById("infoDiv");

		bodyObj.removeChild(infoObj);
		displaying = false;
		
	}
}


function showCommentPopup(srcObj, argType, argId)
{
	if (displaying)
	{
		removeCommentPopup(srcObj);	
	}
	if (!displaying) 
	{
		
		displaying = true;
		var bodyObj = document.getElementsByTagName("body")[0];//.appendChild(documentForm);
		
		var posX, posY, w, h;
		
		// bookshopcomment contactcomment
		
		
		if (argType == "contactcomment")
		{
			w = 450;
			h = 100;
		}

		if (argType == "bookshopcomment")
		{
			w = 80;
			h = 20;
		}
 
		
//		posX = (findPosX(srcObj) + (srcObj.width / 2)) - (w / 2) ;
//		posY = (findPosY(srcObj) + (srcObj.height / 2)) - (h / 2) ;
		
		posX = findPosX(srcObj);
		posY = findPosY(srcObj);


		if ( posX < 0 )
			posX = 0;

		if ( posY < 0 )
			posY = 0;
		
		if ( parseInt(posX + w) > parseInt(getViewportSize("w")) )
			posX = parseInt(getViewportSize("w")) - w - 21;
			
			
		var strContents1 = "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" height=\"100%\" style=\"border-color: #FFA953; border-width: 1px; border-style: solid;\"><tr><td class=\"formDataText\" valign=\"middle\" align=\"center\" bgcolor=white>";
		var strContents2 = "</td></tr></table>";
		


		var infoDiv = document.createElement("DIV");
		infoDiv.style.position = "absolute";
		infoDiv.style.left = posX + "px";
		infoDiv.style.top = posY + "px";
		infoDiv.style.width = w + "px";
		infoDiv.style.height = h + "px";
//		infoDiv.style.borderStyle = "solid";
//		infoDiv.style.borderWidth = "1px";
//		infoDiv.style.borderColor = "#999999";
		infoDiv.style.backgroundColor = "#FFFFFF";
		infoDiv.style.backgroundImage = "url(../images/utility/loading.gif)";
		infoDiv.style.backgroundRepeat = "no-repeat";
		infoDiv.style.backgroundPosition = "center";
		
		if (argType == "contactcomment")
		{
			infoDiv.style.overflowX = "hidden";
			infoDiv.style.overflowY = "auto";
		}

		infoDiv.id = "infoDiv";
		infoDiv.onclick = function() { removeCommentPopup(srcObj); };
		infoDiv.innerHTML = strContents1 + "Loading..." + strContents2;
		infoDiv.style.zIndex = 9999;

		bodyObj.appendChild(infoDiv);	
		
						
					
					
		var httpRequest;
		
		if (window.XMLHttpRequest)     // Object of the current windows
			httpRequest = new XMLHttpRequest();     // Firefox, Safari, ...
		else 
		 if (window.ActiveXObject)   // ActiveX version
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
		
		
		httpRequest.onreadystatechange = function() { 
	
	
			if(httpRequest.readyState == 4) 
			{ 
				if(httpRequest.status == 200) 
				{ 
					infoDiv.innerHTML = strContents1 + httpRequest.responseText + strContents2; 
				}
				else
					alert("Error: " + httpRequest.responseText);
			} 
		};
		
		
		if (argType == "contactcomment")
		{
			httpRequest.open("GET", "rep_contacts.asp?get=contactcomment&CONTACT_ID=" + argId,  true); 
			httpRequest.send(null); 
		}
		
		if (argType == "bookshopcomment")
		{
			httpRequest.open("GET", "bookshops.asp?get=bookshopcomment&SHOP_ID=" + argId,  true); 
			httpRequest.send(null); 
		}
		
		
		
	}

	
	
}

function ajaxUpdate(argObjId, argUrl)
{
	var ajaxUpdateObj = document.getElementById(argObjId);
	
	if(ajaxUpdateObj == null)
	{
		alert("Object with ID " + argObjId + " does not exist.");
		return false;
	}
	
	var httpRequest;
	
	if (window.XMLHttpRequest)     // Object of the current windows
	{
		httpRequest = new XMLHttpRequest();     // Firefox, Safari, ...
	}
	else 
	{
		if (window.ActiveXObject)   // ActiveX version
		{
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
		}
	}
	
	
	httpRequest.onreadystatechange = function() {
		
		if(httpRequest.readyState == 4)
		{
			if(httpRequest.status == 200)
			{
				ajaxUpdateObj.innerHTML = httpRequest.responseText;
			}
			else
			{
				if(confirm("Error " + httpRequest.status + ". Do you want to see the response?"))
				{
					document.write(httpRequest.responseText);
				}
				return false;
			}
		}
	};
	
	httpRequest.open("GET", argUrl,  true); 
	httpRequest.send(null);
	
	if (document.getElementById("infoDiv"))
		document.getElementById("infoDiv").style.display = "none";
	
	return true;


}

function clearContactsFilters(argFormName)
{
	// tbFilter
	var ajaxsForm = document.getElementById(argFormName);
	
	if (ajaxsForm == null)
	{
		alert("Form with name " + argFormName + " does not exist.");	
		return false;
	}
	
	for(i=0; i < ajaxsForm.elements.length; i++)
	{
		var elmObj = document.searchForm.elements[i];
		
		if (elmObj.name.substring(0, 8) == "tbFilter")
		{
			elmObj.value = "";
		}
	}
	
	document.getElementById("filterSource").value = "";
	document.getElementById("sortColumn").value = "CONTACT_ID";
	document.getElementById("sortDirection").value = "ASC";
	document.getElementById("sortColumnActive").value = "";
	document.getElementById("pagingRecordStart").value = "1";
	sumbitForm("sortColumn");
	
}

function ajaxSubmitForm(argFormName, argObjId, argUrl, argImgId)
{
	var ajaxsForm = document.getElementById(argFormName);
	var ajaxUpdateObj = document.getElementById(argObjId);
	var ajaxsfLoadingImg = document.getElementById(argImgId);


	if (ajaxsForm == null)
	{
		alert("Form with name " + argFormName + " does not exist.");	
		return false;
	}
	
	if(ajaxUpdateObj == null)
	{
		alert("Object with ID " + argObjId + " does not exist.");
		return false;
	}
	
	var ajaxsdata = "";
	
	for(i=0; i < ajaxsForm.elements.length; i++)
	{
		var elmObj = document.searchForm.elements[i];
		
		ajaxsdata = ajaxsdata + elmObj.name + "=" + elmObj.value.replace(" ", "+") + "&";

	}
	
	var httpRequest;
	
	if (window.XMLHttpRequest)     // Object of the current windows
	{
		httpRequest = new XMLHttpRequest();     // Firefox, Safari, ...
	}
	else 
	{
		if (window.ActiveXObject)   // ActiveX version
		{
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
		}
	}
	
	httpRequest.onreadystatechange = function() {
		
		if(httpRequest.readyState == 4)
		{
			if(httpRequest.status == 200)
			{
				ajaxUpdateObj.innerHTML = httpRequest.responseText;
				
				window.setTimeout("document.getElementById(document.getElementById(\"filterSource\").value).select();", 100);
				
				//if (ajaxsfLoadingImg != null)
					//ajaxsfLoadingImg.style.display = "none";	
			}
			else
			{
				if(confirm("Error " + httpRequest.status + ". Do you want to see the response?"))
				{
					//alert(httpRequest.responseText.substring(httpRequest.responseText.length-600, httpRequest.responseText.length));
					document.write(httpRequest.responseText);
				}
				return false;
			}
		}
	};
	
	if (ajaxsfLoadingImg != null)
		ajaxsfLoadingImg.style.display = "";	
		
	var ajaxsUrl = "";
	
	if (argUrl == "")
		ajaxsUrl = ajaxsForm.action;
	else
		ajaxsUrl = argUrl;
	

	httpRequest.open("POST", ajaxsUrl, true); 
	httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	httpRequest.send(ajaxsdata);
	
	if (document.getElementById("infoDiv"))
		document.getElementById("infoDiv").style.display = "none";
	
	
	
}


function contactsInit()
{

	//sumbitForm("sortColumn");
	//var obj = document.getElementById("filterSource").value;
	
	//if (obj != null)
	//{
	//	obj.select();
	//	obj.focus();
	//}
}




function sumbitForm(argSourceId)
{
	document.getElementById("filterSource").value = argSourceId;
	ajaxSubmitForm("searchForm", "rep_contacts_div", "", "contacts_loading_img");
}


function doKey(boxId, e)
{
	var key;

	 if(window.event)
		  key = window.event.keyCode;     //IE
	 else
		  key = e.which;     //firefox
		 

	if (key == 13)
	{
		document.getElementById("pagingRecordStart").value = "1";
		sumbitForm(boxId);					
	}
	
	return false;
}

function sortBy(argColumnName)
{
	var objCol = document.getElementById("sortColumn");
	var objDir = document.getElementById("sortDirection");
	var objColAct = document.getElementById("sortColumnActive");

	objCol.value = argColumnName;

	if(objColAct.value == argColumnName)
	{
		if (objDir.value == "ASC")
			objDir.value = "DESC";
		else
			objDir.value = "ASC";
	}
	else
	{
		objDir.value = "ASC"
	}

	
	//alert(objDir.value);
	sumbitForm("sortColumn");
}

function openWindow(argName, argWidth, argHeight, argUrl)
{
    var owtop = ((window.screen.availHeight - argHeight) / 2);
    var owleft = ((window.screen.availWidth - argWidth) / 2);
	
    var win = window.open(argUrl, argName, "width=" + argWidth + ",height=" + argHeight + ",left=" + owleft + ",top=" + owtop + ",scrollbars=yes,resizable=yes");
}

function openContactWindow(argContactId)
{

	var url = "rep_contact.asp?CONTACT_ID=" + argContactId + "&window=true";
	
	openWindow("ibrocontactview", 640, 480, url);
	
	
	
}

function pageTo(argStart)
{
	document.getElementById("pagingRecordStart").value = argStart;
	sumbitForm("sortColumn");
}















function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}

function getViewportSize(argUnit)
{
 var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
 if ( argUnit == "h" )
 {
	 return viewportheight;
 }
 else
 {
	 return viewportwidth;
 }
 
}

hs.graphicsDir = 'highslide/graphics/';
hs.outlineType = 'rounded-white';
hs.wrapperClassName = 'draggable-header';
hs.align = 'center';
hs.showCredits = false;
hs.preserveContent = false;
hs.allowMultipleInstances = true;


function openHighslideWindow(argWidth, argHeight, argUrl, argTitle) {
	
	var hiddenHighslideLink = document.createElement("A");
	hiddenHighslideLink.style.display = "none";
	hiddenHighslideLink.href = argUrl;
	
	if (argTitle)
		hiddenHighslideLink.title = argTitle;

	hiddenHighslideLink.onclick = function() {
		return hs.htmlExpand(hiddenHighslideLink, { dimmingOpacity: 0.75, objectType: 'iframe', height: argHeight, width: argWidth, headingEval: 'this.a.title' } );
	}
	
	var bodyObj = document.getElementsByTagName("BODY")[0];
	bodyObj.appendChild(hiddenHighslideLink);
	hiddenHighslideLink.onclick();

}
function expandHighslideImage(argUrl) {

	var hiddenHighslideLink = document.createElement("A");
	hiddenHighslideLink.style.display = "none";
	hiddenHighslideLink.href = argUrl;
	
	hiddenHighslideLink.onclick = function() {
		return hs.expand(hiddenHighslideLink, { dimmingOpacity: 0.75 } );
	}
	
	var bodyObj = document.getElementsByTagName("BODY")[0];
	bodyObj.appendChild(hiddenHighslideLink);
	hiddenHighslideLink.onclick();

}


var oldLoadingContent = "";

function toggleContacts()
{
	var contactsContentsObj = document.getElementById("rep_contacts_div");
	var contactsRowObj = document.getElementById("contacts_row");


	if (location.href.indexOf("bookshop_contacts.asp") == -1) {
		var contactsLinkObj = document.getElementById("contacts_link");
		var contactsImgObj = document.getElementById("contacts_img");
		
		if (contactsLinkObj.innerHTML == "Show contacts") {
			contactsLinkObj.innerHTML = "Hide contacts";
			contactsImgObj.title = "Hide contacts";
			contactsRowObj.style.display = "";
			oldLoadingContent = contactsContentsObj.innerHTML;
	
			ajaxUpdate(contactsContentsObj.id, "rep_contacts.asp?embed=true&SHOP_ID=" + document.getElementById("SHOP_ID").value + "&shopview=true");
		}
		else {
			contactsLinkObj.innerHTML = "Show contacts";
			contactsImgObj.title = "Show contacts";
			contactsRowObj.style.display = "none";
			contactsContentsObj.innerHTML = oldLoadingContent;		
		}
	}
	else {
		contactsRowObj.style.display = "";
		ajaxUpdate(contactsContentsObj.id, "rep_contacts.asp?embed=true&SHOP_ID=" + document.getElementById("SHOP_ID").value + "&shopview=true");
	}
}





















