﻿function CheckManufacturers(id, msg, cid)
{
	if($get(cid).value == "1")
		return true;
		
	var hid = $get(id).getElementsByTagName('INPUT');
	var nri = 0;
	for(var i = 0; i < hid.length; i++)
		if(hid[i].value == "1")
			nri++;
			
	if(nri > 2)
	{
		alert(msg);
		return false;
	}
	
	return true;
}

var _delaySearchText = '';
var _delaySearchTimeoutID = 0;
function HandleDelaySearch(txtid, lnkid, now)
{
	var txt = $get(txtid);
	
	if(now)
	{
	    if (txt.value != _delaySearchText) 
		{
			$get(lnkid).focus();
			txt.focus();
		}
		
		_delaySearchText = txt.value;
	}
	else
	{
		if(_delaySearchTimeoutID > 0)
			clearTimeout(_delaySearchTimeoutID);
			
		_delaySearchTimeoutID = setTimeout("HandleDelaySearch('" + txtid + "','" + lnkid + "', true)", 300);
	}
}

function ViewOnGoogleMaps(url)
{
	window.open(url, '', 'width=640,height=480');
}

var _divMyProfileMenu;
var _subMenuEntered;
function SlideSubMenuDown(elem, ind)
{
	if(elem)
	{
		_divMyProfileMenu = $get('divMyProfileMenu');
		_divMyProfileMenu.style.display = 'block';
		//var pbounds = Sys.UI.DomElement.getBounds(_divMyProfileMenu);
		var bounds = Sys.UI.DomElement.getBounds($get(elem));
		_divMyProfileMenu.style.top = (bounds.y + bounds.height - 25) + 'px';
		//_divMyProfileMenu.style.left = (bounds.x + bounds.width - pbounds.width) + 'px';

		setTimeout('SlideSubMenuDown(null, 0)', 40);
	}
	else
	{
		if (ind < 13)
		{
			_divMyProfileMenu.style.top = (parseInt(_divMyProfileMenu.style.top) + 2) + 'px';
			setTimeout('SlideSubMenuDown(null, ' + (ind + 1) + ')', 40);
		}
	}
}

function SlideSubMenuUp(elem, ind)
{
	if (_subMenuEntered)
		return;
		
	if (elem)
	{
		if (!_divMyProfileMenu)
			_divMyProfileMenu = $get('divMyProfileMenu');
		
		setTimeout('SlideSubMenuUp(null, 0)', 40);
	}
	else
	{
		if (ind < 13)
		{
			_divMyProfileMenu.style.top = (parseInt(_divMyProfileMenu.style.top) - 2) + 'px';
			setTimeout('SlideSubMenuUp(null, ' + (ind + 1) + ')', 40);
		}
		else
			_divMyProfileMenu.style.display = 'none';
	}
}

function ResetSelected()
{
	if (typeof ($get) == 'function')
	{
		var hdnSelectedValues = $get("hdnSelectedValues");
		if (hdnSelectedValues)
		{
			hdnSelectedValues.value = "";
		}
	}
	else
	{
		setTimeout("ResetSelected()", 500);
	}
}

function GetCodeFromEvent(eventCode)
{
	eventCode = eventCode.toString();
	
	var o = eventCode.indexOf("{");
	if (o > -1)
		eventCode = eventCode.substring(o + 1, eventCode.length);

	var c = eventCode.indexOf("}");
	if (c > -1)
		eventCode = eventCode.substring(0, c);

	return eventCode;
}

function showColorPicker(storageID, iframeID, previewID)
{
	closeColorPicker();
	
	var ifr = $get(iframeID);
	ifr.style.display = 'inline';
	if (ifr.src == "")
		ifr.src = ifr.getAttribute("url") + "&storageID=" + storageID + "&previewID=" + previewID;
}

function closeColorPicker()
{
	var ifrList = document.getElementsByTagName("iframe");
	for (var i = 0; i < ifrList.length; i++)
	{
		if (ifrList[i].id.indexOf("ifrColorPicker") != -1)
		{
			ifrList[i].style.display = 'none';
		}
	}
}

function setColorPickerColor(storageID, previewID, color)
{
	$get(storageID).value = color;
	$get(previewID).style.backgroundColor = "#" + color;
}

/************* AJAX Loaders ***************/

function onEndRequest(sender, args)
{
	if (timeoutPointer != null)
		clearTimeout(timeoutPointer);

	if (updatePanelID != null)
		updatePanelID = null;

	$get('pnlLoadingForAjax').style.display = 'none';
}

var updatePanelID = null;
var timeoutPointer = null;

function ShowLoadingPanel()
{	
	if (updatePanelID == null)
		return;

	$get('pnlLoadingForAjax').style.display = 'block';
}

function onBeginRequest(sender, args)
{
	updatePanelID = Sys.WebForms.PageRequestManager.getInstance()._postBackSettings.panelID.split("|")[0];
	updatePanelID = updatePanelID.replace(new RegExp('\\$', 'g'), '_');
	
	if (updatePanelID.lastIndexOf("_NoDisplayForAjax") > 0)
		return;

	var timeout = 3000;

	if (updatePanelID.lastIndexOf("_AlwaysDisplayForAjax") > 0)
		timeout = 500;

	var upd = $get(updatePanelID);
	timeoutPointer = setTimeout("ShowLoadingPanel()", timeout);
}

function onInitializeRequest(sender, args)
{
	if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
	{
		args.set_cancel(true);
	}
}

function RegisterAJAXFct()
{
	var obj = Sys.WebForms.PageRequestManager.getInstance();
	if (obj != null && $get('pnlLoadingForAjax'))
	{
		obj.add_beginRequest(onBeginRequest);
		obj.add_endRequest(onEndRequest);
		obj.add_initializeRequest(onInitializeRequest);
	}
};

/*********** End AJAX Loaders *************/

function keepSessionAlive()
{
	$get('ifrEmptyPage').src = '/EmptyPage.aspx';
	
	setTimeout('keepSessionAlive()', 200000);
}

function openWindow(url)
{
	window.open(url, '', 'width=1000,height=750,toolbar=no,scrollbars=yes');
}

function OpenDelete(id)
{
	var lnk = $get(id);
	if (lnk.disabled)
	{
		return;
	}
	
	var linkBounds = Sys.UI.DomElement.getBounds(lnk);
	var optionsBox = $get('divDeleteContainer');

	optionsBox.style.display = 'block';
	optionsBox.style.top = (linkBounds.y + lnk.offsetHeight + 2) + 'px';
    //align box to the left of the delete button
	//optionsBox.style.left = linkBounds.x - 3 + 'px';
	//align box to the right of the delete button
	optionsBox.style.left = (linkBounds.x + lnk.offsetWidth + 2 - optionsBox.offsetWidth) + 'px';	

	setTimeout("registerDeleteDocumentClick()", 1000);

	return false;
}

function registerDeleteDocumentClick()
{
	document.onclick = function(ev)
	{
		var source = document.all ? event.srcElement : ev.target;
		if (source.id.indexOf("txtDeleteOlder") < 0)
		{
			if ($get('divDeleteContainer'))
			{
				$get('divDeleteContainer').style.display = 'none';

				document.onclick = null;
			}
		}
	}
}

/*********** Start contact selected *************/

var timeoutContactSelectedID;
var contactSelectedContainerID = "divContactSelectedContainer";
function OpenContactSelected(id)
{
	var lnk = $get(id);
	if (lnk.disabled)
	{
		return;
	}

	var lbounds = Sys.UI.DomElement.getBounds(lnk);
	var container = $get(contactSelectedContainerID);

	container.style.top = (lbounds.y + 18) + 'px';
	container.style.left = lbounds.x - 20 + 'px';
	container.style.display = 'block';

	timeoutContactSelectedID = window.setTimeout('CloseContactSelected()', 3000);
	return false;
}

function CloseContactSelected()
{
	if (timeoutContactSelectedID)
	{
		window.clearTimeout(timeoutContactSelectedID);
	}

	$get(contactSelectedContainerID).style.display = 'none';
}

function ShowContactSelected()
{
	if (timeoutContactSelectedID)
	{
		window.clearTimeout(timeoutContactSelectedID);
	}

	var container = $get(contactSelectedContainerID);	
	if (container)
	{
		container.style.display = 'block';
	}
}

function HideContactSelected()
{	
	if (timeoutContactSelectedID)
	{
		timeoutContactSelectedID = window.setTimeout('CloseContactSelected()', 500);
	}
	else
	{
		var container = $get(contactSelectedContainerID);
		if (container)
		{
			container.style.display = 'none';
		}
	}
}

var lnkContactSelectedId = "";
var lnkSendAsBroadcastId = "";
var sendAsBroadcastAllowed = false;
function RefreshContactSelected()
{
	var hdnSelectedValues = $get('hdnSelectedValues');
	var lnkContactSelected = $get(lnkContactSelectedId);
	if (hdnSelectedValues && lnkContactSelected)
	{
		if (hdnSelectedValues.value == "")
		{
		    lnkContactSelected.className = "button disabled";
		    DisableLink(lnkContactSelectedId);

		    //also update send as broadcast (if exists and allowed)
		    var lnkSendAsBroadcast = $get(lnkSendAsBroadcastId);
		    if (sendAsBroadcastAllowed == true) 
            {
                if (lnkSendAsBroadcast) 
                {
		            DisableLink(lnkSendAsBroadcastId);
		        }
		    }
		}
		else
		{
		    //lnkContactSelected.className = "button-drop-down";
		    lnkContactSelected.className = "button";
		    EnableLink(lnkContactSelectedId);

            //also update send as broadcast (if exists and allowed)
		    var lnkSendAsBroadcast = $get(lnkSendAsBroadcastId);
		    if (sendAsBroadcastAllowed == true) 
            {
		        if (lnkSendAsBroadcast) 
                {
                    EnableLink(lnkSendAsBroadcastId);
		        }
            }
		}
	}
}
/*********** End contact selected *************/

function DisableLink(id)
{
	var lnk = $get(id);
	if (lnk)
	{
		lnk.disabled = true;
		if (lnk.className.indexOf("disabled") == -1)
		{
			lnk.className = lnk.className + " disabled";
		}
	}
}

function EnableLink(id)
{
	var lnk = $get(id);
	if (lnk)
	{
		if (lnk.disabled)
		{
			lnk.disabled = false;
			lnk.className = lnk.className.replace(" disabled", "");
		}
	}
}

function confirmDelete()
{
	if (confirm('Are you sure you want to delete these items?') == false)
	{
		return false;
	}
}

function Check(chk, id)
{	
	var val = $get('hdnSelectedValues').value;
	if (chk.checked)
	{
		val += val == "" ? id : "," + id;
	}
	else
	{
		var values = val.split(",");
		var newVal = "";
		for (var i = 0; i < values.length; i++)
		{
			if (values[i] != id)
			{
				newVal += newVal == "" ? values[i] : values[i] + ",";
			}
		}
		val = newVal;
	}

	$get('hdnSelectedValues').value = val;
}

function ItemCheck(checkbox, id, lnkContactSelectedClientId)
{	
	Check(checkbox, id);	
	lnkContactSelectedId = lnkContactSelectedClientId;
	RefreshContactSelected();
}

function ToggleCheck(chk, cid)
{
	var chkList = $get(cid).getElementsByTagName('INPUT');
	for (var i = 0; i < chkList.length; i++)
	{
		if (chkList[i].type.toLowerCase() == 'checkbox')
		{
			chkList[i].click();
		}
	}
}

/***************** Contact Detail **********************/

var _selectedObjectID;
var _selectedFieldID;
var _selectedMessageType;
function ShowContactDetails(objID, fieldID, userID, type)
{
	_selectedObjectID = objID;
	_selectedFieldID = fieldID;
	_selectedMessageType = type;	
	ITTO.ScriptService.GetUserDetails(userID, OnGetUserDetailsComplete);
}

function OnGetUserDetailsComplete(userDetails)
{
	if (/*_selectedObjectID && */userDetails && _selectedFieldID)
	{
		var user = eval("(" + userDetails + ")");

		$get('spContactName').innerHTML = user.Name;
		$get('lnkContactCompanyName').innerHTML = user.Company;
		$get('lnkContactCompanyName').href = "javascript:ShowCompanyDetails('" + user.CompanyID + "')";
		$get('spContactPhone').innerHTML = user.Telephone;
		$get('lnkContactSendMessage').href = "javascript:ReplyToUser('" + _selectedMessageType + "'," + _selectedFieldID + "," + user.UserID + ")";
		/*$get('lnkContactSendEmail').href = "javascript:ReplyToUser('em'," + _selectedFieldID + "," + user.UserID + ",'" + _selectedMessageType + "')";*/
		if (user.Image == '')
		{
			$get('imgContactPhoto').src = _rootImageFolder + "Users/DefaultUserImage.jpg";
		}
		else
		{
			$get('imgContactPhoto').src = _rootImageFolder + "Users/" + user.UserID + "/" + user.Image;
		}

		var spContactAOL = $get('spContactAOL');
		if (spContactAOL)
		{
			if (user.ContactAOL == '')
			{
				spContactAOL.style.display = 'none';
			}
			else
			{
				spContactAOL.style.display = 'block';
				$get('spAolValue').innerHTML = user.ContactAOL;
				$get('spAolValue').href = 'aim:goim?screenname=' + user.ContactAOL;
			}
		}
		var spContactMSN = $get('spContactMSN');
		if (spContactMSN)
		{
			if (user.ContactMSN == '')
			{
				spContactMSN.style.display = 'none';
			}
			else
			{
				spContactMSN.style.display = 'block';
				$get('spMsnValue').innerHTML = user.ContactMSN;
				$get('spMsnValue').href = 'msnim:chat?contact=' + user.ContactMSN;
			}
		}
		var spContactSkype = $get('spContactSkype');
		if (spContactSkype)
		{
			if (user.ContactSkype == '')
			{
				spContactSkype.style.display = 'none';
			}
			else
			{
				spContactSkype.style.display = 'block';
				$get('spSkypeValue').innerHTML = user.ContactSkype;
				$get('spSkypeValue').href = 'skype:' + user.ContactSkype + '?chat';
			}
		}
		var spContactYahoo = $get('spContactYahoo');
		if (spContactYahoo)
		{
			if (user.ContactYahoo == '')
			{
				spContactYahoo.style.display = 'none';
			}
			else
			{
				spContactYahoo.style.display = 'block';
				$get('spYahooValue').innerHTML = user.ContactYahoo;
				$get('spYahooValue').href = 'ymsgr:sendim?' + user.ContactYahoo;
			}
		}

		var bounds = Sys.UI.DomElement.getBounds($get(_selectedObjectID));
		var container = $get('divContactDetails');
		container.style.display = 'block';
		container.style.top = bounds.y + 20 + 'px';
		container.style.left = bounds.x - 90 + 'px';
	}
}

function CloseContactDetails() {
    var divContactDetails = document.getElementById('divContactDetails');
    if (divContactDetails) {
        divContactDetails.style.display = 'none';
    }
}

/*******************************************************/

/***************** Feedback **********************/

function startToggleFeedbackTop(txtID)
{
	var openFeedback;

	if (!_feedbackDiv)
	{
		_feedbackDiv = $get('divFeedbackTop');
	}
	
	if (parseInt(_feedbackDiv.style.height) == 0)
	{
		openFeedback = true;
		_feedbackDiv.style.display = 'block';
	}
	else
	{
		openFeedback = false;
	}

	toggleFeedbackTop(openFeedback, txtID);
}

function toggleFeedbackTop(openFeedback, txtID)
{
	var cont = false;
	var feedbackHeight = parseInt(_feedbackDiv.style.height);

	if (openFeedback)
	{
		if (feedbackHeight < 200)
		{
			_feedbackDiv.style.height = (feedbackHeight + 20) + 'px';
			cont = true;
		}
		else
		{
			$get(txtID).focus();
		}
	}
	else
	{
		if (feedbackHeight > 0)
		{
			if (feedbackHeight == 20)
			{
				_feedbackDiv.style.display = 'none';
			}
			
			_feedbackDiv.style.height = (feedbackHeight - 20) + 'px';
			cont = true;
		}
	}

	if (cont)
	{
		setTimeout('toggleFeedbackTop(' + openFeedback + ', "' + txtID + '")', 20);
	}
}

var _feedbackDiv;

function startToggleFeedback()
{
	var openFeedback;

	if (!_feedbackDiv)
	{
		_feedbackDiv = $get('divFeedback');
	}
	
	if (parseInt(_feedbackDiv.style.width) == 0)
	{
		openFeedback = true;
		_feedbackDiv.style.display = 'block';
	}
	else
	{
		openFeedback = false;
	}

	toggleFeedback(openFeedback);
}

function toggleFeedback(openFeedback)
{
	var cont = false;
	var feedbackWidth = parseInt(_feedbackDiv.style.width);

	if (openFeedback)
	{
		if (feedbackWidth < 500)
		{
			_feedbackDiv.style.width = (feedbackWidth + 20) + 'px';
			cont = true;
		}
	}
	else
	{
		if (feedbackWidth > 0)
		{
			if (feedbackWidth == 20)
			{
				_feedbackDiv.style.display = 'none';
			}
			
			_feedbackDiv.style.width = (feedbackWidth - 20) + 'px';
			cont = true;
		}
	}

	if (cont)
	{
		setTimeout('toggleFeedback(' + openFeedback + ')', 20);
	}
}

/*************************************************/

/***************** Help **********************/
var _helpDiv;

function startToggleHelp()
{
	var openHelp;

	if (!_helpDiv)
	{
		_helpDiv = $get('divHelp');
	}

	if (parseInt(_helpDiv.style.width) == 0)
	{
		openHelp = true;
		_helpDiv.style.display = 'block';
	}
	else
	{
		openHelp = false;
	}

	toggleHelp(openHelp);
}

function toggleHelp(openHelp)
{
	var cont = false;
	var helpWidth = parseInt(_helpDiv.style.width);

	if (openHelp)
	{
		if (helpWidth < 500)
		{
			_helpDiv.style.width = (helpWidth + 20) + 'px';
			cont = true;
		}
	}
	else
	{
		if (helpWidth > 0)
		{
			if (helpWidth == 20)
			{
				_helpDiv.style.display = 'none';
			}

			_helpDiv.style.width = (helpWidth - 20) + 'px';
			cont = true;
		}
	}

	if (cont)
	{
		setTimeout('toggleHelp(' + openHelp + ')', 20);
	}
}

/*************************************************/

/*
var _descriptionContainer;
var _lastDescriptionKey;

function ShowDescription(p1, p2)
{
	var key = p2._dataKeyValues.InventoryNotificationID;
	if (!key)
	{
		key = p2._dataKeyValues.InventoryID;
	}
	if (!key)
	{
		key = p2._dataKeyValues.ShowroomID;
	}
	if (!key)
	{
		key = p2._dataKeyValues.ClassifiedID;
	}	
	
	if (key != _lastDescriptionKey)
	{
		_lastDescriptionKey = key;
		
		var spanList = $get(p2.get_id()).getElementsByTagName('span');
		for (var i = 0; i < spanList.length; i++)
		{
			if (spanList[i].id.match("lblDescription$"))
			{
				FillDescriptionContainer(spanList[i].innerHTML, p2.get_id());
				break;
			}
		}
	}

	ShowDescriptionContainer();
}

function FillDescriptionContainer(text, elementID)
{
	if (_descriptionContainer == null)
	{
		_descriptionContainer = document.createElement('div');
		_descriptionContainer.className = 'AbsPopup';
		_descriptionContainer.style.padding = '10px';
		document.body.appendChild(_descriptionContainer);
	}

	var bounds = Sys.UI.DomElement.getBounds($get(elementID));

	_descriptionContainer.style.top = (bounds.y + bounds.height - 10) + 'px';
	_descriptionContainer.style.left = (bounds.x + 200) + 'px';	
	
	_descriptionContainer.innerHTML = text;
}

function ShowDescriptionContainer()
{
	if (_descriptionContainer.innerHTML != '')
	{
		_descriptionContainer.style.display = 'block';
	}
}

function HideDescription(p1, p2)
{
	_descriptionContainer.style.display = 'none';
}
*/

function MarkReadRecord(id)
{
	var tr = $get(id);
	if (tr)
	{
		var spans = tr.getElementsByTagName("span");
		for (var i = 0; i < spans.length; i++)
		{
			spans[i].className = "";
		}

		var as = tr.getElementsByTagName("a");
		for (var i = 0; i < as.length; i++)
		{
			as[i].className = "";
		}
	}
}

function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

/********************* Search Showroom Async **************************/
function CheckShowroomSearchHasItems()
{
	ITTO.ScriptService.ShowroomSearchHasItems(_showroomSearchParameter, OnCheckShowroomSearchHasItemsCompleted);
}

function OnCheckShowroomSearchHasItemsCompleted(result)
{
	if (result)
	{
		$('#divResultsFoundInShowroom').slideDown('slow');
	}
}

function CloseResultsFoundInShowroom()
{
	$('#divResultsFoundInShowroom').slideUp('slow');
}

function OpenResultsInShowroom(a)
{
	CloseResultsFoundInShowroom();
	a.href = 'Showroom.aspx?search=' + unescape(_showroomSearchParameter);
	a.click();
}
/*********************** End Search Showroom Async ************************/

/********************* Search Broadcast Async **************************/
function CheckBroadcastSearchHasItems()
{
	ITTO.ScriptService.BroadcastSearchHasItems(_broadcastSearchParameter, OnCheckBroadcastSearchHasItemsCompleted);
}

function OnCheckBroadcastSearchHasItemsCompleted(result)
{
	if (result)
	{
		$('#divResultsFoundInBroadcast').slideDown('slow');
	}
}

function CloseResultsFoundInBroadcast()
{
	$('#divResultsFoundInBroadcast').slideUp('slow');
}

function OpenResultsInBroadcast(a)
{
	CloseResultsFoundInBroadcast();
	a.href = 'Broadcast.aspx?search=' + unescape(_broadcastSearchParameter);
	a.click();
}
/*********************** End Broadcast Showroom Async ************************/

/********************* Search Inventory Async **************************/
function CheckInventorySearchHasItems()
{
	ITTO.ScriptService.InventorySearchHasItems(_inventorySearchParameter, OnCheckInventorySearchHasItemsCompleted);
}

function OnCheckInventorySearchHasItemsCompleted(result)
{
	if (result)
	{
		$('#divResultsFoundInInventory').slideDown('slow');
	}
}

function CloseResultsFoundInInventory()
{
	$('#divResultsFoundInInventory').slideUp('slow');
}

function OpenResultsInInventory(a)
{
	CloseResultsFoundInInventory();
	a.href = 'Inventory.aspx?search=' + unescape(_inventorySearchParameter);
	a.click();
}
/*********************** End Inventory Showroom Async ************************/

function CheckPopupWindowLocation(wnd)
{
	var bounds = wnd.getWindowBounds();
	if (bounds.y < 0)
	{
		wnd.moveTo(bounds.x, 10);
	}
}

function StartGetUserNewConversations()
{
	GetUserNewConversations();

	setInterval('GetUserNewConversations()', 600000);
}

function GetUserNewConversations()
{
	ITTO.ScriptService.GetNewUserConversations(OnGetUserNewConversationsComplete);
}

function OnGetUserNewConversationsComplete(result)
{
	var conversations = eval(result);
	for (var i = 0; i < conversations.length; i++)
	{
		var convBubble = document.getElementById('convBubble' + conversations[i].ModuleID);
		
		if (convBubble)
		{
			if (parseInt(conversations[i].Count) > 0)
			{
				convBubble.innerHTML = conversations[i].Count;
				convBubble.style.display = 'block';
			}
			else
			{
				convBubble.style.display = 'none';
			}
		}
	}
}

function ForceCloseRadWindow(id, time)
{
	if (time == 2000)
	{
		return;
	}

	var element = parent.parent.document.getElementById(id);
	
	if (element.style.display != 'none')
	{
		element.style.display = 'none';
	}

	setTimeout("ForceCloseRadWindow('" + id + "'," + (parseInt(time) + 200) + ")", 200);
}
