sfHover = function() {
    var sfEls = document.getElementById("TopNavigation").getElementsByTagName("li");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function() {
            this.className += " sfhover";
        }

        sfEls[i].onmouseout = function() {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function EmailAFriend(PropertyId) {
    windowDoPop = window.open('EmailAFriend.aspx?PropertyId=' + PropertyId, 'doPop', 'width=550,height=430,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no');
	windowDoPop.focus(); 
} 

function SmsEmailAgent(AgentId, WebRef, PropertyId) {
    windowDoPop = window.open('SmsEmailAgent.aspx?AgentId=' + AgentId + '&WebRef=' + WebRef + '&PropertyId=' + PropertyId, 'doPop', 'width=560,height=430,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no');
	windowDoPop.focus(); 
}

function Calculator(Calculators) {
    var CalculatorWindow

    CalculatorWindow = window.open(Calculators, "Standard_Bank_Calculators", "width=720,height=530,scrollbars=yes");
    CalculatorWindow.focus();
    return false;
}

function PunctuatedOnStart(objId) {
	Punctuated(document.getElementById(objId));
}

function Punctuated(obj) {
	var n = obj;
	var s = (''+n.value).split('.');
	s[0] = s[0].split('').reverse().join('').match(/\d{1,3}/gi).join(',').split('').reverse().join('');
	obj.value = (s.join('.'));
}

function UpdateRentalIndicator(objList, UpdateClientId) {
	var Val = "";
	if (objList.options.selectedIndex != 0)
	{
		Val = objList.options[objList.options.selectedIndex].text;
	}
	
	if (Val == "Weekly")
	    Val = "Per Week";
	if (Val == "Monthly")
	    Val = "Per Month";
	if (Val == "Daily")
	    Val = "Per Day";
	
	document.getElementById(UpdateClientId).value = Val;
}

function trimAll(sString) {
	while (sString.substring(0,1) == ' ')
	{
	    sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	    sString = sString.substring(0, sString.length-1);
	}
	return sString;
}

function ListMoveNextOnStart(objId) {
	ListMoveNext(document.getElementById(objId));
}

function ListMoveNext(obj) {
	for (var i = 0;i<obj.options.length;i++) 
	{
        _option = obj.options[i];
        if (_option.selected && _option.text.indexOf('-') == -1)
        {
			if (obj.options[i+1] != null && obj.options[i+1].text.indexOf('-') != -1)
			    obj.options[i+1].selected = true
        }
    }
}

function DisableSelectOnStart(objId) {
	DisableSelect(document.getElementById(objId));
}

function DisableSelect(obj) {
	for (var i = 0;i<obj.options.length;i++) 
	{
        _option = obj.options[i];
        if (_option.selected)
        {
			if (_option.style.backgroundColor != "")
			    _option.selected = false;
        }
    }
}
		
function SetAsHomepage() {
    if (document.all)
	{
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage('http://www.allsaproperty.co.za');
	}
	else
	{
		alert("This function is unavailable in your browser.");
	}
}

function ValidateAgencySearchByKeyword(ctrlInput, Message) {
	if (trimAll(document.getElementById(ctrlInput).value) == "")
	{
		alert(Message);
		document.getElementById(ctrlInput).focus();
		return false;
	}
}

function ValidatePropertySearch(ctrlProvince, ProvinceMessage, IsSaleType, ctrlPropertyType, PropertyTypeMessage, ctrlRentalPeriod, RentalPeriodMessage, ctrlPriceFrom, PriceFromMessage, ctrlPriceTo, PriceToMessage, ValidPriceRangeMessage) {	
	var Message = "";
	var IsValid = true;
	
	if (document.getElementById(ctrlProvince).options.selectedIndex == 0)
	{
		Message += ProvinceMessage;
		IsValid = false;
	}
	
	if (document.getElementById(ctrlPropertyType).options.selectedIndex == 0)
	{
		if (Message != "")Message += "\n";
		Message += PropertyTypeMessage;
		IsValid = false;
	}
	
	if (IsSaleType == "False")
	{
		if (document.getElementById(ctrlRentalPeriod).options.selectedIndex == 0)
		{
			if (Message != "")Message += "\n";
			Message += RentalPeriodMessage;
			IsValid = false;
		}
	}
	
	if (document.getElementById(ctrlPriceFrom).options.selectedIndex == 0 || document.getElementById(ctrlPriceFrom).disabled == true )
	{
		if (Message != "")Message += "\n";
			Message += PriceFromMessage;
			IsValid = false;
	}
	
	if (document.getElementById(ctrlPriceTo).options.selectedIndex == 0  || document.getElementById(ctrlPriceTo).disabled == true )
	{
		if (Message != "")Message += "\n";
			Message += PriceToMessage;
			IsValid = false;
	}
	
	var ctrlFrom = document.getElementById(ctrlPriceFrom).value;
	var ctrlTo = document.getElementById(ctrlPriceTo).value;
		
	if (ctrlFrom != "All" && ctrlTo != "All")
	{
		if (parseInt(ctrlFrom) > parseInt(ctrlTo))
		{
			if (Message != "")Message += "\n";
			Message += ValidPriceRangeMessage;
			IsValid = false;
		}
	}
	
	if (!IsValid)
	{
		alert(Message);
		return false;
	}
}

function ValidateAgencyRegionSearch(ctrlProvince, ctrlCityTown, Message1, Message2) {
	var Message = "";
	var IsValid = true;
	
	if (document.getElementById(ctrlProvince).options.selectedIndex == 0)
	{
		Message += Message1;
		IsValid = false;
	}
	
	if (document.getElementById(ctrlCityTown).options.selectedIndex == 0)
	{
		if (Message != "")Message += "\n";
		    Message += Message2;
		    IsValid = false;
	}
	
	if (!IsValid)
	{
		alert(Message);
		return false;
	}
}	

function ValidateTypeAll(SenderCtrl, ListCtrl) {
	if (SenderCtrl.value == 'All')
	    document.getElementById(ListCtrl).value = 'All'; 
}

function ValidateLoginCredentials(ctrlUsername, ctrlPassword, MessageUsername, MessagePassword) {
	var Message = "";
	var IsValid = true;
	if (trimAll(document.getElementById(ctrlUsername).value) == "")
	{
		Message += MessageUsername;
		IsValid = false;
	}
	
	if (trimAll(document.getElementById(ctrlPassword).value) == "")
	{
		if (Message != "") Message += "\n";
		    Message += MessagePassword;
		    IsValid = false;
	}
	
	if (!IsValid)
	{
		alert(Message);
		return false;
	}
}

function ValidatePropertySearchByWebRef(ctrlInput, Message) {
	if (trimAll(document.getElementById(ctrlInput).value) == "")
	{
		alert(Message);
		document.getElementById(ctrlInput).focus();
		return false;
	}	
}

function AgencyListNavigation(ctrlList, AgencyId, PageId) {
	if (ctrlList.options.selectedIndex==0)
		return false;
	else
		window.location = "?PageId=" + PageId + "&SuburbId=" + ctrlList.value + "&AgencyId=" + AgencyId;	
}

function ValidateImageFileTypes(source, args) {
	args.IsValid = true;
	
	if (args)
	{
		var arrFileExtensions = ".gif,.jpeg,.jpg,.bmp";
		var varFileArry = arrFileExtensions.split(',');
		var FileExtension = args.Value.substring(args.Value.lastIndexOf('.'));
		var ExtensionMatch = false;
		
		if (FileExtension != "")
		{
			for (var i = 0; i < varFileArry.length; i++) 
			{
				if (FileExtension.toString().toLowerCase() == varFileArry[i].toString().toLowerCase())
				{
					ExtensionMatch = true;
					break;
				}
			}		
		}
		else
		    ExtensionMatch = true;
		
		if (!ExtensionMatch)
		    args.IsValid = false;		
	}
}

function ValidateSelectedGridItems(ConfirmationMessage,ErrorMessage) {
	var Marked = 0;
	for (var i = 0; i < document.forms[0].elements.length; i++) 
	{
		if (document.forms[0].elements[i].checked && document.forms[0].elements[i].name.indexOf('checked_') > -1)
		    Marked = Marked +1;		
	}
	if (Marked == 0) 
	{
		alert(ErrorMessage);
		return false;
	}
	else
	{
		if(!confirm(ConfirmationMessage))
		return false;
	}
}

