function LinkToEmail(LTOEi)
{
	document.location = "../asp/ContactThem.asp?EmailAddress=" + encodeURIComponent(VendorEmails[LTOEi]) + 
                                            "&VendorName=" + encodeURIComponent(VendorNames[LTOEi]) + 
                                            "&VendorType=" + encodeURIComponent(VendorTypeDescription)
}
	
function LinkToWebsite(LTOWi)
{
    if (VendorWebsites[LTOWi].substring(0, 7) == "http://")
        window.open(VendorWebsites[LTOWi]);
    else
        window.open("http://" + VendorWebsites[LTOWi]);
}

function DirectLinkMoreInfo(psMoreInfo)
{
    document.location = psMoreInfo;
}

function DirectLinkToEmail(psEmail, psName, psType)
{
	document.location = "../asp/ContactThem.asp?EmailAddress=" + encodeURIComponent(psEmail) + 
                                                "&VendorName=" + encodeURIComponent(psName) + 
                                                "&VendorType=" + encodeURIComponent(psType);
}
	
function DirectLinkToWebsite(psWebsite)
{
    if (psWebsite.substring(0, 7) == "http://")
        window.open(psWebsite);
    else
        window.open("http://" + psWebsite);
}

function LinkMoreInfo(LMOIi)
{
    document.location = VendorLinkURLs[LMOIi]
}


function ContructVendorAddressAndPhoneLine(CVAAPLVendorAddressLine1s, CVAAPLVendorAddressLine2s, CVAAPLVendorPhones, CVAAPLVendorFaxes, CVAAPLVendorListingTypes)
{
	var sAddressText = "";
    var sPhoneText = "";

    SetColWidth("100%");
    SetTableWidth("100%");
    NewTable();

	    if (CVAAPLVendorAddressLine1s != "")
		    sAddressText += ("Address: " + CVAAPLVendorAddressLine1s);
	    if (CVAAPLVendorAddressLine2s != "")
	    {
		    if (sAddressText != "")
			    sAddressText += (", " + CVAAPLVendorAddressLine2s);
		    else
			    sAddressText += ("Address: " + CVAAPLVendorAddressLine2s);
	    }

	    if (CVAAPLVendorPhones != "")
		    sPhoneText += ("Ph:" + "&nbsp;" + CVAAPLVendorPhones);
	    if (CVAAPLVendorFaxes != "")
	    {
		    if (sPhoneText != "")
			    sPhoneText += "&nbsp; &nbsp;";
		    sPhoneText += ("Fax:" + "&nbsp;" + CVAAPLVendorFaxes);
	    }

        if (sAddressText == "" &&
            sPhoneText == "")
        {
            EndTable();
            return;
        }

	    if (sAddressText == "")
            sAddressText = "&nbsp;";
        else
            sAddressText += "&nbsp;&nbsp;"
        if (sPhoneText == "")
            sPhoneText = "&nbsp;";

        SetHorizontalAlignment("left")
	    NewRow();
            Paragraph(sAddressText, "supplierLevel" + (parseInt(CVAAPLVendorListingTypes) + 1));
        SetHorizontalAlignment("right")
        SetNobreak(true);
        NewColumn();
            Paragraph(sPhoneText, "supplierLevel" + (parseInt(CVAAPLVendorListingTypes) + 1));

    EndTable();

}

function Validate()
{
    return true;
}


function WriteListingHTML(WLHTMLPersonTypeId)
{
    Heading("directory listing - " + VendorTypeDescription, 2);

    var bShortList = false;
    if (VendorTypeDescription == "short list")
        bShortList = true;

	SetHorizontalAlignment("left")
    SetVerticalAlignment("center")

    if (PersonId > 0 &&
        (WLHTMLPersonTypeId == 1 ||
         WLHTMLPersonTypeId == 2) &&
        VendorIds.length > 0)
    {
	    NewTable("", "", "calendarenabled" + (VendorIds.length==1?"bottom":""));
            SetOnChangeEvent("SetShortListToSelected()");
            CheckBox("AddOrRemoveVendor0");
        NewColumn("calendarenabled" + (VendorIds.length==1?"bottom":"") + "right");
    }
    else
	    NewTable("", "", "calendarenabled" + (VendorIds.length<=1?"bottom":"") + "right");

    var i;
    for (i=0; i < VendorIds.length; i++)
    {
        if (! bShortList)
        {
            SetCellpadding(0);
            SetCellspacing(0);
        }
            SetVerticalAlignment("top");
            SetColWidth("100%");
            SetTableWidth("100%");
            NewTable();

                if (VendorLinkURLs[i] != "")
                    Link(VendorNames[i] + (bShortList?" (" + VendorType[i] + ")": ""), VendorLinkURLs[i], "supplierLevel" + VendorListingTypes[i]);
                else
                    Paragraph(VendorNames[i] + (bShortList?" (" + VendorType[i] + ")": ""), "supplierLevel" + VendorListingTypes[i]);
			    SetHorizontalAlignment("right")
                SetColWidth("1%");
                SetNobreak(true);

                NewColumn();
                Space(1);
			    if (VendorLinkURLs[i] != "")
			    {
                    Space(1);
				    Image(gsMoreInfoImageURL, 
					      "directory", 
					      "javascript:LinkMoreInfo(" + i + ")",
					      "17",
					      "17",
					      "More Information");
			    }
			    if (VendorEmails[i] != "")
			    {
                    Space(1);
				    Image(gsEmailImageURL, 
					      "directory", 
					      "javascript:LinkToEmail(" + i + ")",
					      "19",
					      "18",
					      "Email Address");
			    }
			    if (VendorWebsites[i] != "")
			    {
                    Space(1);
				    Image(gsWebsiteImageURL, 
					      "directory", 
					      "javascript:LinkToWebsite(" + i + ")",
					      "18",
					      "18",
					      "Website");
			    }
            EndTable();

            if (! bShortList)
            {
                SetCellpadding(0);
                SetCellspacing(0);
            }
			ContructVendorAddressAndPhoneLine(VendorAddressLine1s[i], 
                                              VendorAddressLine2s[i], 
                                              VendorPhones[i], 
                                              VendorFaxes[i], 
                                              VendorListingTypes[i]);

            var sCellClass = "calendar";

            sCellClass += (i%2?"enabled":"disabled");
            sCellClass += (i==(VendorIds.length-2)?"bottom":"");
        
            if (i != (VendorIds.length-1))
            {
                SetHorizontalAlignment("left");
                SetVerticalAlignment("center")
                if (PersonId > 0 &&
                    (WLHTMLPersonTypeId == 1 ||
                     WLHTMLPersonTypeId == 2))
                {
    		        NewRow("", sCellClass);
                        SetOnChangeEvent("SetShortListToSelected()");
                        CheckBox("AddOrRemoveVendor" + (i+1).toString());
                    NewColumn(sCellClass + "right");
                }
                else
    		        NewRow("", sCellClass + "right");
            }
    }
    if (i==0)
    {
        Text("There are no businesses matching your criteria.  Please refine your search and try again.");
    }

    EndTable();
}

function SetShortListToSelected()
{
    var ii;
    for (ii = 0; ii < gfrmForm.DirectoryListingActions.length; ii++)
    {
        if (gfrmForm.DirectoryListingActions[ii].value == "ShortList")
        {
            gfrmForm.DirectoryListingActions[ii].checked = true;
            break;
        }
    }
}

function SubmitShortListOrDirectory(SLORDAction)
{
    var iii=0;

    for (iii=0; iii < gfrmForm.DirectoryListingActions.length; iii++)
    {
        if (gfrmForm.DirectoryListingActions[iii].checked)
        break;
    }

    if (gfrmForm.DirectoryListingActions[iii].value != "ShortList")
        Submit();
    else
    {
        var ModifiedSupplierIds = "";
        iii = 0;
        while (iii < VendorIds.length)
        {
            if (eval("gfrmForm.AddOrRemoveVendor" + iii.toString() + ".checked"))
            {
                ModifiedSupplierIds = ModifiedSupplierIds + VendorIds[iii].toString();
                ModifiedSupplierIds = ModifiedSupplierIds + ";";
            }
            iii ++;
        }
        document.location = "../asp/ShortList.asp?Action=" + SLORDAction + "&SupplierIds=" + ModifiedSupplierIds;
    }
}
