var arrSellers = new Array();
var arrAccessories = new Array();

var g_objSearch = null;
var g_objMap = null;
var g_objGeocoder = null;
var g_objMapContainer = null;

function fixFormAction()
{
	var formAction = $(document.forms[0]).attr('action');
	if (formAction.indexOf('://') == -1 && formAction.indexOf('/') != 0)
	{
		document.forms[0].action = '/' + formAction;
	}
}

function updateSellerList(f_strUserID, f_strFullName, f_strRemove) 
{
    //alert(typeof(arrSellers[0]));
    
    if (f_strUserID == '')
        return;
 
    if (document.forms[0].hidSellers.value.indexOf(f_strUserID) != -1)
        return;
        
    if (typeof(arrSellers[0]) == 'undefined')
    {
        arrSellers.length = 1;
        arrSellers[0] = new Array(2);
        arrSellers[0][0] = f_strUserID;
        arrSellers[0][1] = f_strFullName;
    }
    else
    {
       // alert(f_strUserID);
        arrSellers.length = arrSellers.length + 1;
        arrSellers[arrSellers.length - 1] = new Array(2);
        arrSellers[arrSellers.length - 1][0] = f_strUserID;
        arrSellers[arrSellers.length - 1][1] = f_strFullName;
    }    
    
    updateSellerHtml(arrSellers, f_strRemove)
}

function removeSeller(f_intNumber, f_strRemove)
{

    for (i = f_intNumber + 1; i < arrSellers.length; ++i)
    {
         arrSellers[i - 1] = arrSellers[i];
    }
    arrSellers.length = arrSellers.length - 1;
    
    updateSellerHtml(arrSellers, f_strRemove)
}

function updateSellerHtml(f_arrSellers, f_strRemove)
{
    document.getElementById('divSellers').innerHTML = '';
    document.forms[0].hidSellers.value = '';
    
    for (i = 0; i < f_arrSellers.length; ++i)
    {
	    document.forms[0].hidSellers.value += f_arrSellers[i][0] + ',';
	    document.getElementById('divSellers').innerHTML += '<strong>' + (i + 1) + '.</strong> &nbsp;' + f_arrSellers[i][1] + ' <a href="javascript:removeSeller(' + i + ', \'' + f_strRemove + '\')" class="Orange11">&#187; ' + f_strRemove + '</a><br>';
    }
}

function updateAccessoryList(f_strRemove, f_strSelectAccessory) 
{
    var strValue = document.getElementById('PADataForm_accessorylist').value;
    var strText = document.getElementById('PADataForm_accessorylist').options[document.getElementById('PADataForm_accessorylist').selectedIndex].text;
    var strComment = document.getElementById('PADataForm_accessorycomment').value;
    
    if (strValue.length < 1)
    {
        alert(f_strSelectAccessory);
        return;
    }     
    if (document.forms[0].hidAccessories.value.indexOf(',' + strValue + '|') != -1 || document.forms[0].hidAccessories.value.indexOf(strValue + '|') == 0)
        return;
        
    if (typeof(arrAccessories[0]) == 'undefined')
    {
        arrAccessories.length = 1;
        arrAccessories[0] = new Array(3);
        arrAccessories[0][0] = strValue;
        arrAccessories[0][1] = strComment;
        arrAccessories[0][2] = strText;
    }
    else
    {
       // alert(f_strUserID);
        arrAccessories.length = arrAccessories.length + 1;
        arrAccessories[arrAccessories.length - 1] = new Array(3);
        arrAccessories[arrAccessories.length - 1][0] = strValue;
        arrAccessories[arrAccessories.length - 1][1] = strComment;
        arrAccessories[arrAccessories.length - 1][2] = strText;
    }    
    
    document.getElementById('PADataForm_accessorycomment').value = '';
    document.getElementById('PADataForm_accessorylist').selectedIndex = null;
    
    updateAccessoryHtml(arrAccessories, f_strRemove);
    
}

function updateAccessoryListEdit(f_strRemove, strValue, strText, strComment) 
{
    if (typeof(arrAccessories[0]) == 'undefined')
    {
        arrAccessories.length = 1;
        arrAccessories[0] = new Array(3);
        arrAccessories[0][0] = strValue;
        arrAccessories[0][1] = strComment;
        arrAccessories[0][2] = strText;
    }
    else
    {
       // alert(f_strUserID);
        arrAccessories.length = arrAccessories.length + 1;
        arrAccessories[arrAccessories.length - 1] = new Array(3);
        arrAccessories[arrAccessories.length - 1][0] = strValue;
        arrAccessories[arrAccessories.length - 1][1] = strComment;
        arrAccessories[arrAccessories.length - 1][2] = strText;
    }    
    updateAccessoryHtml(arrAccessories, f_strRemove);
    
}

function updateAccessoryHtml(f_arrAccessories, f_strRemove)
{
    
    document.getElementById('alSelected').innerHTML = '';
    document.forms[0].hidAccessories.value = '';

    for (i = 0; i < f_arrAccessories.length; ++i)
    {
	    document.forms[0].hidAccessories.value += f_arrAccessories[i][0] + '|' + f_arrAccessories[i][1] + ',';
	    document.getElementById('alSelected').innerHTML += '<div class=alSelectedContent><strong>' + f_arrAccessories[i][2] + '</strong><br> ' + f_arrAccessories[i][1] + '</div><div class="alAccRemove"><a class="Orange12Bold" href="javascript:removeAccessory(' + i + ', \'' + f_strRemove+ '\')">&#187; ' + f_strRemove + '</a></div>';
    }
}

function removeAccessory(f_intNumber, f_strRemove)
{

    for (i = f_intNumber + 1; i < arrAccessories.length; ++i)
    {
         arrAccessories[i - 1] = arrAccessories[i];
    }
    arrAccessories.length = arrAccessories.length - 1;
    
    updateAccessoryHtml(arrAccessories, f_strRemove);
}

function bookmarksite()
{
    var BookmarkURL="http://www.mascus.com"
    var BookmarkTitle="Mascus"
    if (document.all)
    {                         // For IE
    window.external.AddFavorite(BookmarkURL, BookmarkTitle); 
    }
    else if (window.sidebar)
    {                  // For Firefox 
      window.sidebar.addPanel(BookmarkTitle, BookmarkURL, '');
    }
 }
 
 function movepic(img_name,img_src) 
 { 
 document[img_name].src=img_src; 
 }

function paySelectedPrivateAds(f_strErrorMsg)
{
    var objForm = document.forms[0];
    var strProdIds = '';
    for (i = 0; i < objForm.length; i++)
    {
        if (objForm[i].name.toLowerCase() == 'prodid')
        {
            if (objForm[i].checked)
            {
                if (strProdIds.length > 0)
                {
                    strProdIds += ',';
                }
                strProdIds += objForm[i].value;
            }
        }
    }
    
    if (strProdIds.length > 0)
    {
        //var strUrl = '/PayAds.aspx?ProdID=' + strProdIds;
        //window.location.href = strUrl;
        submitForm('', '/PayAds.aspx');
    }
    else if (f_strErrorMsg)
    {
        alert(f_strErrorMsg);
    }
}

function selectBank(f_strBank, f_blnHideVatDetails)
{
    var objForm = document.forms[0];
    objForm['BankID'].value = f_strBank;
    if (f_blnHideVatDetails)
    {
        objForm['HideVATDetails'].value = '1';
    }
    objForm.action = m_strBankRedirectUrl;
    objForm.submit();
}

function setBank(f_strBank)
{
    var objForm = document.forms[0];
    objForm['BankID'].value = f_strBank;
}

function showPaymentInfo(f_intTransID, f_strBlock, f_strBank)
{
    var strUrl = '/PayAds.aspx?TransID=' + f_intTransID;
    if (f_strBank)
    {
		strUrl += '&BankID=' + f_strBank;
    }
    strUrl += '&ShowInfo=' + f_strBlock + '#' + f_strBlock;
    window.location = strUrl;
}

function checkAll(f_strFieldName)
{
    toggleCheckBox(f_strFieldName, true);
}

function uncheckAll(f_strFieldName)
{
    toggleCheckBox(f_strFieldName, false);
}

function toggleCheckBox(f_strFieldName, f_blnChecked)
{
    var objForm = document.forms[0];
    for (i = 0; i < objForm.length; i++)
    {
        if (objForm[i].name.toLowerCase() == f_strFieldName.toLowerCase())
        {
            objForm[i].checked = f_blnChecked;
        }
    }
}

function setFieldValue(f_strName, f_strValue)
{
	if (document.forms[0][f_strName])
	{
		document.forms[0][f_strName].value = f_strValue;
	}
}

function setFieldValues(f_arrNameValue)
{
	if (f_arrNameValue)
	{
		for (i = 0; i < f_arrNameValue.length; i++)
		{
			if (document.forms[0][f_arrNameValue[i].name])
			{
				document.forms[0][f_arrNameValue[i].name].value = f_arrNameValue[i].value;
			}
		}
	}
}

function getCheckedCount(f_strFieldName)
{
    var objForm = document.forms[0];
    var intCount = 0;
    for (i = 0; i < objForm.length; i++)
    {
        if (objForm[i].name.toLowerCase() == f_strFieldName.toLowerCase())
        {
            if (objForm[i].checked)
            {
				intCount++;
            }
        }
    }
    return intCount;
}

function RefreshOrder(f_strFormName) {
	intCount = 1;
	if (typeof(document.forms[f_strFormName]) == 'object') {
		for (var i = 0; i < document.forms[f_strFormName].length ; i ++)  {
			if (document.forms[f_strFormName].elements[i].type=='text') {
				if (document.forms[f_strFormName].elements[i].name == 'ordernr')
				{
					document.forms[f_strFormName].elements[i].value = intCount;
					intCount = intCount + 1;
				}
			}
		}
	}
}

function setPostAction(f_strPostAction)
{
    if (!f_strPostAction)
    {
        f_strPostAction = '';
    }
    if (f_strPostAction != 'dologin')
    {
        if (document.forms[0]['lfTxtPw'])
        {
            document.forms[0]['lfTxtPw'].value = '';
        }
    }
    document.forms[0]['postaction'].value = f_strPostAction;
}

function setFormAction(f_strFormAction)
{
    if (f_strFormAction)
    {
        document.forms[0]['__VIEWSTATE'].value = '';
        document.forms[0].action = f_strFormAction;
    }
}

function handleEnter(f_objEvent, f_strPostAction, f_strFormAction)
{
    if (f_objEvent.keyCode == 13)
    {
		if (typeof f_strPostAction == 'function')
		{
			f_strPostAction();
		}
		else
		{
			submitForm(f_strPostAction, f_strFormAction);
        }
        return false;
    }
    else
    {
        return true;
    }
}

function submitForm(f_strPostAction, f_strFormAction)
{
    setPostAction(f_strPostAction);
    setFormAction(f_strFormAction);
    document.forms[0].submit();
}

function submitIfChecked(f_strFieldName, f_intMinItems, f_intMaxItems, f_strErrorMessage, f_strConfirmMessage, f_strPostAction, f_strFormAction)
{
	var intCheckedCount = getCheckedCount(f_strFieldName);
	//alert(intCheckedCount)
	if (f_intMaxItems > 0 && (intCheckedCount < f_intMinItems || intCheckedCount > f_intMaxItems))
	{
		if (f_strErrorMessage)
		{
			alert(f_strErrorMessage.replace('#minimum#', f_intMinItems).replace('#maximum#', f_intMaxItems).replace('#number#', f_intMinItems + ' - ' + f_intMaxItems));
		}
		return false;
	}
	else if (intCheckedCount < f_intMinItems)
	{
		if (f_strErrorMessage)
		{
			alert(f_strErrorMessage.replace('#minimum#', f_intMinItems).replace('#number#', f_intMinItems));
		}
		return false;
	}
	else if (f_intMaxItems > 0 && intCheckedCount > f_intMaxItems)
	{
		if (f_strErrorMessage)
		{
			alert(f_strErrorMessage.replace('#maximum#', f_intMaxItems).replace('#number#', f_intMaxItems));
		}
		return false;
	}
	else
	{
		if (f_strConfirmMessage)
		{
			if (confirm(f_strConfirmMessage.replace('#number#', intCheckedCount)))
			{
				submitForm(f_strPostAction, f_strFormAction);
			}
			else
			{
				return false;
			}
		}
		else
		{
			submitForm(f_strPostAction, f_strFormAction);
		}
	}
}

function disableEnter(f_objEvent)
{
    if (f_objEvent.keyCode == 13)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function openInfoPopup(f_strContentKey, f_intWidth, f_intHeight)
{
    if (!f_intWidth)
    {
        f_intWidth = 350;
    }
    if (!f_intHeight)
    {
        f_intHeight = 400;
    }
    var objPopup = window.open('/popup.aspx?content=' + f_strContentKey, 'infoPopup', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=' + f_intHeight + ',width=' + f_intWidth);
    objPopup.focus();
}

function openCurrencyInfoPopup()
{
    openInfoPopup('pcCurrencyInfo');
}

function initGoogleMap(f_strContainerDivId, f_strMapDivId)
{
	if (GBrowserIsCompatible())
	{
		g_objSearch = new GlocalSearch();
		g_objGeocoder = new GClientGeocoder();
		
		g_objMapContainer = document.getElementById(f_strContainerDivId);
		
		g_objMap = new GMap2(document.getElementById(f_strMapDivId));
		g_objMap.addControl(new GSmallMapControl());
		//g_objMap.setCenter(new GLatLng(37.4419, -122.1419), 13);
	}
}

function findMapPoint(f_strStreet, f_strCity, f_strCountry)
{
	if (g_objGeocoder)
	{
		var strAddress = '';
		var strNextTry = null;

		strAddress = f_strCity + ', ' + f_strCountry;
		if (f_strStreet)
		{
			strNextTry = strAddress;
			strAddress = f_strStreet + ', ' + strAddress;
		}
		//alert(strAddress);

		g_objGeocoder.getLatLng(
			strAddress,
			function(point)
			{
				showGoogleMap(point, strNextTry);
			}
		);
	}
}

function findMapPointUK(f_strPostCode)
{
	if (g_objSearch)
	{
		//alert(f_strPostCode);
		g_objSearch.setSearchCompleteCallback(null,
			function()
			{
				if (g_objSearch.results[0])
				{    
					var resultLat = g_objSearch.results[0].lat;
					var resultLng = g_objSearch.results[0].lng;
					var point = new GLatLng(resultLat, resultLng);
					showGoogleMap(point, null);
				}
				else
				{
					//alert("Postcode not found!");
				}
			}
		);
		g_objSearch.execute(f_strPostCode + ", UK");
	}
}

function showGoogleMap(f_objPoint, f_strNextTry)
{
	if (!f_objPoint)
	{
		//alert("Address not found");
		//g_objMapContainer.style.display = 'none';
		if (f_strNextTry)
		{
			//alert(f_strNextTry);
			g_objGeocoder.getLatLng(
				f_strNextTry,
				function(point)
				{
					showGoogleMap(point, null);
				}
			);
		}
	}
	else
	{
		//g_objMapContainer.style.display = 'block';
		g_objMap.setCenter(f_objPoint, 13);
		var objMarker = new GMarker(f_objPoint, {clickable: false});
		g_objMap.addOverlay(objMarker);
		//objMarker.openInfoWindowHtml(strAddress);
	}
}

function openClose(f_strElementId, f_strArrowId, f_strButtonId, f_strOpenText, f_strCloseText)
{
	var intState = toggleDisplay(f_strElementId);
	var objArrow = document.getElementById(f_strArrowId);
	var objButton = document.getElementById(f_strButtonId);
	if (objArrow && objButton && intState != -1)
	{
		if (intState == 0)
		{
			objArrow.innerHTML = '&raquo;';
			objButton.innerHTML = '&raquo; ' + f_strOpenText;
		}
		else if (intState == 1)
		{
			objArrow.innerHTML = '';
			objButton.innerHTML = '&laquo; ' + f_strCloseText;
		}
		objButton.blur();
	}
}

function expandCollapse(f_strToggleElementId, f_strHeaderId, f_strButtonId, f_strExpandText, f_strCollapseText)
{
	var intState = toggleDisplay(f_strToggleElementId, true);
	if (f_strHeaderId)
	{
		var objHeader = document.getElementById(f_strHeaderId);
		if (objHeader)
		{
			if (intState == 0)
			{
				objHeader.title = f_strExpandText;
			}
			else if (intState == 1)
			{
				objHeader.title = f_strCollapseText;
			}
		}
	}
	if (f_strButtonId)
	{
		var objButton = document.getElementById(f_strButtonId);
		if (objButton)
		{
			if (intState == 0)
			{
				objButton.className = 'ocExpandButton';
				objButton.title = f_strExpandText;
			}
			else if (intState == 1)
			{
				objButton.className = 'ocCollapseButton';
				objButton.title = f_strCollapseText;
			}
		}
	}
}

function toggleDisplay(f_strElementId, f_blnAnimate)
{
	if (f_strElementId)
	{
		var objElement = document.getElementById(f_strElementId);
		if (objElement)
		{
			if (isHiddenElement(objElement))
			{
				showElement(f_strElementId, f_blnAnimate);
				return 1;
			}
			else
			{
				hideElement(f_strElementId, f_blnAnimate);
				return 0;
			}
		}
	}
	return -1;
}

function toggleCssClass(f_strElementId, f_strClass1, f_strClass2)
{
	if (f_strElementId)
	{
		var objElement = document.getElementById(f_strElementId);
		if (objElement)
		{
			var strClassName = objElement.className;
			if (!strClassName || strClassName == f_strClass1)
			{
				objElement.className = f_strClass2;
				return 2;
			}
			else
			{
				objElement.className = f_strClass1;
				return 1;
			}
		}
	}
	return -1;
}

function isHiddenElement(f_objElement)
{
	var blnHidden = false;
	if (f_objElement)
	{
		if (f_objElement.style.display == 'none')
		{
			blnHidden = true;
		}
	}
	return blnHidden;
}

function showElement(f_strElementId, f_blnAnimate)
{
	if (f_strElementId)
	{
		var objElement = document.getElementById(f_strElementId);
		if (objElement)
		{
			if (typeof jQuery != 'undefined')
			{
				$(objElement).slideDown('fast');
			}
			else
			{
				objElement.style.display = 'block';
			}
		}
	}
}

function hideElement(f_strElementId, f_blnAnimate)
{
	if (f_strElementId)
	{
		var objElement = document.getElementById(f_strElementId);
		if (objElement)
		{
			if (typeof jQuery != 'undefined')
			{
				$(objElement).slideUp('fast');
			}
			else
			{
				objElement.style.display = 'none';
			}
		}
	}
}

function trackFormChanges(f_objField)
{
	var objForm = document.forms[0];
	var objHiddenField = objForm['changedfields'];
	var strFieldName = '';
	
	if (typeof f_objField == 'string')
	{
		strFieldName = f_objField;
	}
	else
	{
		strFieldName = f_objField.name;
	}
	
	if (!objHiddenField)
	{
		objHiddenField = document.createElement('INPUT');
		objHiddenField.type = 'hidden';
		objHiddenField.name = 'changedfields';
		objHiddenField.id = 'changedfields';
		objHiddenField.value = '';
		objForm.appendChild(objHiddenField);
	}
	
	if (objHiddenField.value)
	{
		if (objHiddenField.value.indexOf(strFieldName) == -1)
		{
			objHiddenField.value += ',' + strFieldName;
		}
	}
	else
	{
		objHiddenField.value = strFieldName;
	}
}

function changeListPage(f_intPage, f_strFieldName)
{
	if (!f_strFieldName)
	{
		f_strFieldName = 'Page';
	}
	setHiddenValue(f_strFieldName, f_intPage);
}

function changeListSort(f_strSort, f_strFieldName)
{
	if (!f_strFieldName)
	{
		f_strFieldName = 'Sort';
	}
	setHiddenValue(f_strFieldName, f_strSort);
}

function changeListSize(f_intSize, f_strFieldName)
{
	if (!f_strFieldName)
	{
		f_strFieldName = 'PageSize';
	}
	setHiddenValue(f_strFieldName, f_intSize);
}

function setHiddenValue(f_strFieldName, f_strValue)
{
	var objForm = document.forms[0];
	var objHiddenField = objForm[f_strFieldName];
	
	if (!objHiddenField)
	{
		objHiddenField = document.createElement('INPUT');
		objHiddenField.type = 'hidden';
		objHiddenField.name = f_strFieldName;
		objHiddenField.id = f_strFieldName;
		objHiddenField.value = '';
		objForm.appendChild(objHiddenField);
	}
	
	objHiddenField.value = f_strValue;
}

function findPosition(f_objElement) 
{ 
	var intLeft = 0; 
	var intTop = 0; 
	var blnFound = false;
	 
	if (f_objElement.offsetParent) 
	{ 
		blnFound = true;
		do 
		{ 
			intLeft += f_objElement.offsetLeft; 
			intTop += f_objElement.offsetTop; 
		}
		while (f_objElement = f_objElement.offsetParent); 
	} 

	return {
		found: blnFound,
		left: intLeft,
		top: intTop
	};
}

function positionAfter(f_strElemId, f_strAfterElemId)
{
	var objElem = document.getElementById(f_strElemId);
	var objAfterElem = document.getElementById(f_strAfterElemId);
	if (objElem && objAfterElem)
	{
		objElem.parentNode.removeChild(objElem);
		objAfterElem.parentNode.insertBefore(objElem, objAfterElem.nextSibling);
	}
}

function positionOver(f_strElemId, f_strOverElemId)
{
	var objElem = document.getElementById(f_strElemId);
	var objOverElem = document.getElementById(f_strOverElemId);
	if (objElem && objOverElem)
	{
		var objPos = findPosition(objOverElem);
		if (objPos.found)
		{
			objElem.style.top = objPos.top + 'px';
			objElem.style.left = objPos.left + 'px';
		}
	}
}

function cancelEvent(f_objEvent)
{
	//f_objEvent.preventDefault();
	//f_objEvent.returnValue = false;
	//f_objEvent.cancelBubble = true;
	return false;
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}

