﻿// JScript File
function ShowListingLevel(sMarket,sTrListingLevel,sValListingLevel,bListing,sMarketCode)
{	
	var oMyMarketDropDown = document.getElementById(sMarket);
	var oMyListingLevel = document.getElementById(sTrListingLevel);
	var oMyListingLevelVal = document.getElementById(sValListingLevel);
	var bisListing = bListing;
	var oMarketCode
	//Only display listing level dropdown box and listing level validation control 
	//for listing orders in telemarketing market. 
	if (oMyMarketDropDown == null)
		oMarketCode = sMarketCode;
	else
		oMarketCode = oMyMarketDropDown.value;

	//Only display listing level dropdown box and listing level validation control 
	//for listing orders in telemarketing market.
	if ((bisListing == "True")  && ( oMarketCode == "W02000"))
	{
		oMyListingLevel.style.display = ''; 
		oMyListingLevelVal.initialvalue = "0"; 
	}
	else
	{
		oMyListingLevel.style.display = 'none'; 
		oMyListingLevelVal.initialvalue = '';
	}
}

function ToggleOnlineAppAttributes(sOnlineApp,sTrOnlineApp2,sValSerialNo)
{
    var oOnlineAppDropdown = document.getElementById(sOnlineApp); 
    var oOnlineApp2 = document.getElementById(sTrOnlineApp2); 
    var oMyValidator = document.getElementById(sValSerialNo);
    if (oOnlineAppDropdown != null)
    {
        //if ((oOnlineAppDropdown.value == 'Blue Moon') && (oOnlineApp2 != null))
        if (oOnlineAppDropdown.value == 'Blue Moon')
        {
            oOnlineApp2.style.display = '';   
            oMyValidator.enabled = true;
        }   
        else
        {
            oOnlineApp2.style.display = 'none'; 
            oMyValidator.enabled = false; 
        }    
    } 
    else
    {
        oOnlineApp2.style.display = 'none';
    }       
}

function AdjustAppointmentOnlineAppValues(schkPurchaseWebsite,sddlAppointment,sddlOnlineApp,sAction)
{
    var oWebsiteURL = document.getElementById(schkPurchaseWebsite);
    var oAppointment = document.getElementById(sddlAppointment); 
    var oOnlineApp = document.getElementById(sddlOnlineApp); 
    
    if (oWebsiteURL != null)
    {
        if (oWebsiteURL.checked == false)  //WebsiteURL is not checked
        { 
            for (var i=0; i<oAppointment.options.length; i++)
            { 
                //Set "Not Active" as the selected value if creating an new order
                if (sAction == "Add")
                {
                    if (oAppointment.options[i].value == "Not Active")
                        oAppointment.options[i].selected = true;
                }
                //Remove "Resident Impact" from the select value if creating an new order   
                if (oAppointment.options[i].value == "Resident Impact")
                    oAppointment.remove(i);
            }
            
            for (var i=0; i<oOnlineApp.options.length; i++)
            {
                //Set "Not Active" as the selected value
                if (sAction == "Add")
                {
                    if (oOnlineApp.options[i].value == "Not Active")
                        oOnlineApp.options[i].selected = true;
                }
                //Remove "Resident Impact" from the select value     
                if (oOnlineApp.options[i].value == "Resident Impact")
                    oOnlineApp.remove(i);
            }
        }
        else //WebsiteURL is checked
        {
            var bResidentImpact = false; 
            for (var i=0; i<oAppointment.options.length; i++)
            { 
                if (oAppointment.options[i].value == "Resident Impact")
                {
                    bResidentImpact = true;
                    break;
                }
            }

            if (bResidentImpact == false)
            {
                //Add "Resident Impact" to Appointment dropdown
                var oAppointmentOptionNew = document.createElement('option');
                oAppointmentOptionNew.text = "Resident Impact";
                oAppointmentOptionNew.value = "Resident Impact";
                oAppointmentOptionNew.selected = true;
                oAppointment.add(oAppointmentOptionNew); 
            }
            
            var bOnlineApp = false; 
            for (var i=0; i<oOnlineApp.options.length; i++)
            { 
                if (oOnlineApp.options[i].value == "Resident Impact")
                {
                    bOnlineApp = true;
                    break;
                }
            }
           
            if (bOnlineApp == false)
            {
                //Add "Resident Impact" to Online application dropdown
                var oOnlineAppOptionNew = document.createElement('option');
                oOnlineAppOptionNew.text = "Resident Impact";
                oOnlineAppOptionNew.value = "Resident Impact";
                oOnlineAppOptionNew.selected = true;
                oOnlineApp.add(oOnlineAppOptionNew); 
            }     
        }
               
    }
}

// Counts number of characters in the specified TextBox or TextArea.
// Displays message in the specified message field showing 
// maximum number of characters and number of characters remaining.
function countRemainingChars( textBoxID, countMsgFieldID, maxlimit ) 
{
	var dTxt = document.getElementById( textBoxID );
	var msg  = document.getElementById( countMsgFieldID );
	
	if ((dTxt != null) && (msg != null))//TD3168
	{
	    if ( dTxt.value.length > maxlimit )
		    dTxt.value = dTxt.value.substring( 0, maxlimit );
    		
	    var newlength = maxlimit - dTxt.value.length;
	    msg.innerHTML = "max " + maxlimit + " characters (" + newlength + " remain)";
	}    
}

//For Copy contact
function copyContact(txtName, txtEmail, txtPhone, txtExt, txtFax )
{
    document.getElementById(txtName).value = valname;
    document.getElementById(txtEmail).value = valemail;
    document.getElementById(txtPhone).value = valphone;
    document.getElementById(txtExt).value = valext;
    document.getElementById(txtFax).value = valfax; 
    return false;
}

function convertASDate( sRetDate )
{
   var DateArray = new Array();
   DateArray = sRetDate.split("/");	   
   var mm = DateArray[0];       
   var dd = DateArray[1];
   var yyyy = DateArray[2];
   if(dd<10){dd='0'+dd;}
   if(mm<10){mm='0'+mm;}
   RetDate = mm+"/"+dd+"/"+yyyy;
   return RetDate;
}
var formClicked = false;
function updateIsActive(chkbox, action, contractid)
{
	if ( formSubmitted() ) return false;

	if (confirm("Change Active?"))
	{
		contractid.value = chkbox.getAttribute("OrderContractID");
		
		if (chkbox.checked)
		    action.value = "UPDATEISACTIVE";
		else
		    action.value = "UPDATEISNOTACTIVE";
		chkbox.disabled = true;   
		document.aspnetForm.submit();
		return;
	} else {
	    if (chkbox.checked)
		    chkbox.checked = false;
	    else
		    chkbox.checked = true;
		formClicked = false;
		return false;
	}	
}
function formSubmitted()
{
	if (!formClicked) {
		formClicked = true } 
	else {
		return true; }
	return false;
}

function changeQueue(queue, action, contractid, newqueue, sLiveDate, sEndDate, sOptionType)
{	
	if ( formSubmitted() ) return false;

	//allow change of queue From New to Artist or CSR, not later
	if ((queue.getAttribute("EntityID") < 0) && (queue.getAttribute("IsListing") == "1") && (queue.value > 3)) 
	{
		alert("This property needs to have a valid PropertyID before any of its listing orders can be moved to a different queue.");
		action.value = "RESETQUEUE";
		contractid.value = queue.getAttribute("ContractID");
		document.aspnetForm.submit();
		return;
	}
	if (confirm("Change Queue?"))
	{	
		var status = queue.options[queue.options.selectedIndex].text;
		//TD3014: There should be a validation that requires a Live Date to be entered when the contract is published.
		if(sLiveDate == "" && status == "Published")
		{
		    newqueue.value = "No Live Date. Please correct this by editing the contract.";  // Contains the error message.
		    action.value = "INVALIDQUEUE";            
	    }
	    else if ((sEndDate == "") && (status == "Published") && (sOptionType=="True"))  //TD3056
	    {
	        newqueue.value = "No End Date. Please correct this by editing the contract.";  // Contains the error message.
		    action.value = "INVALIDQUEUE"; 
	    }
	    else
	    {
		    contractid.value = queue.getAttribute("ContractID");
		    newqueue.value = queue.options[queue.options.selectedIndex].value;
		    action.value = "UPDATEQUEUE";
		}	    
		document.aspnetForm.submit();
		return;
	}
	formClicked = false;
}

//Function to display the order control based on selection of combo.
function DisplayOrderControl(ctrl, objDiv)
{
    objDiv = document.getElementById(objDiv);
    var sSelectedOrder = "";
    for(i=0; i<ctrl.length; i++)
    {
        if(ctrl[i].value!=0)
        {
            if(document.getElementById(ctrl[i].value).style.display != 'none')
            {
                sSelectedOrder = ctrl.options[ctrl[i].index].text;
             }
         }
    }    

    if(sSelectedOrder !="")
    {
	    if ( !confirm('Your changes to ' + sSelectedOrder + ' will be lost. Save changes and reload the form to perform other actions. Choose cancel to stay on the page and ok to loose changes and continue perform different action. Do you wish to loose changes?'))
	    {
		    return false;
	    }
    
    }

    for(i=0; i<ctrl.length; i++)
    {
        if(ctrl[i].value!=0)
            document.getElementById(ctrl[i].value).style.display = 'none';
    }
    if(ctrl.value !="0")
    {
        document.getElementById(ctrl.value).style.display = 'block';
        objDiv.style.display = 'none';
        ctrl.disabled=true;
        
        if(SelectedDropDownID.indexOf(ctrl.id)<0)  //If already added 
        {
            SelectedDropDownID = SelectedDropDownID + ctrl.id + ",";
        }            
    }
    return false;   //To avoid post back.
}

//For close button in all div
function CloseDiv(divName, divSection)
{
    if(divName !=null)
    {
	    if ( !confirm('You will abandon all changes if you continue. Are you sure you want to do this?'))
	    {
		    return false;
	    }
        divName.style.display = "none";
        divSection.style.display = "block"; //Display the corresponding section div to choose any other options.
    }
   //Set the none option in apporpriate dropdown.
   var ctrlArray = SelectedDropDownID.split(","); 
   for(i=0; i<ctrlArray.length - 1; i=i+1)
   {      
        var ctrl = document.getElementById(ctrlArray[i]);
        if(ctrl!=null)
        {  
            for(j=0; j<ctrl.length; j++)
            {
                if(ctrl.options[j].value == divName.id)
                {
                    ctrl.value = "0";
                    ctrl.disabled=false;
                    break;
                }
            }                
         }
   }   
   return false;
}

function checkURL(value) {
var myregex = new RegExp("^(http:\/\/|https:\/\/|ftp:\/\/){1}([0-9A-Za-z]+\.)");
if(myregex.test(value)) {
  return(true);
}
else {
  return(false);
}
}


function ValidateDateRange(sAction, sStartDate, sEndDate, sLiveDate, sShootDate1, sShootDate2, sShootDate3, sTitle, sOldEndDate, sOptionTypeID, sListingStartDate, sToday, sQueue)
{
        var Today = sToday;
        StartDate = convertASDate(sStartDate);
        if(sEndDate != "")	 
            EndDate = convertASDate(sEndDate);	 
        else 
            EndDate = "";
        //TD 2984    
        if(sLiveDate != "")    
            LiveDate = convertASDate(sLiveDate);
        else
            LiveDate = "";
        //TD 2984
        var ShootDate1="";
        var ShootDate2="";
        var ShootDate3="";        
        if(sShootDate1!="")
            ShootDate1 = convertASDate(sShootDate1);
        if(sShootDate2!="")
            ShootDate2 = convertASDate(sShootDate2);
        if(sShootDate3!="")
            ShootDate3 = convertASDate(sShootDate3);  	    
	    var objdiv = document.getElementById("dvErrorMessage");
	    var dtOldEndDate = document.getElementById(sOldEndDate).value;	
        var OptionTypeID = document.getElementById(sOptionTypeID).value;
        var ListingStartDate = document.getElementById(sListingStartDate).value;
        dtOldEndDate = convertASDate(dtOldEndDate);  
	    ListingStartDate = convertASDate(ListingStartDate);  
	    
        //When dropping an order, enddate must be later than or equal to 
        //the system date           
        //When dropping an order, enddate must be later than or equal to the system date
        if ((sAction == "Drop")&& ( EndDate!= "") && (compareDates(Today, "MM/dd/yyyy", EndDate,"MM/dd/yyyy") !=0))
        {
            sMsg = sMsg + "<li>&nbsp;Please enter an end date that ends today or later. </li>";
        }   
        
        //When dropping an order, enddate must be earlier than the current enddate
        if ((sAction == "Drop") && ( EndDate!= "") && (compareDates(EndDate, "MM/dd/yyyy", dtOldEndDate,"MM/dd/yyyy") !=0)  && ( dtOldEndDate!= "01/01/1900"))
        {
            sMsg = sMsg +  "<li>&nbsp;Please enter an end date prior to the existing end date. </li>";
        }
        
        //Validate if the startdate is earlier than the enddate
        if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && ((compareDates(StartDate, "MM/dd/yyyy", EndDate,"MM/dd/yyyy") !=0) && ( EndDate!= "")))
        {
            sMsg = sMsg + "<li>&nbsp;Start date cannot be later than end date. </li>";
        }         
        
        //Validate if the Site Live Date is later than today  --S483
        /*if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && ((compareDates( Today, "MM/dd/yyyy", LiveDate,"MM/dd/yyyy") !=0))&& (LiveDate != ""))//TD 2984
        {
            if (sAction == "Edit")
            {
                if (sQueue == "Published")
                {
                    sMsg = sMsg + "";
                }
                else
                {
                    sMsg = sMsg + "<li>&nbsp;Site Live Date must be today or later. </li>"; 
                }

            }
            else
            {
                sMsg = sMsg + "<li>&nbsp;Site Live Date must be today or later. </li>";            
            }
        }*/     
        
        //Validate if the Site Live Date is earlier than the startdate --S483
        /*if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && ((compareDates(StartDate, "MM/dd/yyyy", LiveDate,"MM/dd/yyyy") !=0))&& (LiveDate != ""))//TD 2984
        {
            sMsg = sMsg + "<li>&nbsp;Site Live Date must be later than start date. </li>";                       
        }*/
          
        //Validate if the Site Live Date is earlier thatn the startdate --S483
        if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && ((compareDates(LiveDate, "MM/dd/yyyy", EndDate,"MM/dd/yyyy") !=0)) && (LiveDate != "") && (EndDate != ""))//TD 2984
        {   
            sMsg = sMsg + "<li>&nbsp;Site Live date cannot be later than end date. </li>";                       
        }
             
        //Validate if the enddate is later than today
        if (((sAction == "Add") || (sAction == "Renew")) &&   ((compareDates(EndDate, "MM/dd/yyyy", Today, "MM/dd/yyyy") ==0)) && (EndDate != ""))  
        {
            sMsg = sMsg + "<li>&nbsp;Please enter an end date that ends later than today.</li>";                       
        }       
        
        //Validate if a new order's start date of bold, front conver, property premium, emercial, online tour order is 
        //later than the corresponding listing. When editing an non-listing order, the order's startdate can be earlier 
        //than listing's start date and the order is still valid. 
        if ( ( (sAction == "Add") || (sAction == "Renew") )&& ( (OptionTypeID == 1) || (OptionTypeID == 14) || (OptionTypeID == 3) || (OptionTypeID == 4) || (OptionTypeID == 5) ) )     
        {             
            if ( ( ListingStartDate != "01/01/1900") && (compareDates(ListingStartDate, "MM/dd/yyyy", StartDate,"MM/dd/yyyy") !=0))
            {
                sMsg = sMsg + "<li>&nbsp;Start date cannot be earlier than listing start date. </li>";                                       
            }
        }
        
        //When creating a new order, start date cannot be earlier than system date
        /*if (sAction == "Add")
		{
			if ( (compareDates(Today, "MM/dd/yyyy", StartDate,"MM/dd/yyyy") !=0)) 
			{
			  sMsg = sMsg + "<li>&nbsp;Start date must be today or later.</li>";                       
			}
		}*/
		 
		if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && (ShootDate1 != "01/01/1900")  && (ShootDate1 != "") )
        {
            if (compareDates(Today, "MM/dd/yyyy", ShootDate1,"MM/dd/yyyy") !=0)
            {
                sMsg = sMsg + "<li>&nbsp;ShootDate1 must be today or later.</li>";                       
            }
        }
        if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && (ShootDate2 != "01/01/1900")  && (ShootDate2 != "") )
        {
            if (compareDates(Today, "MM/dd/yyyy", ShootDate2,"MM/dd/yyyy") !=0)
            {
                sMsg = sMsg + "<li>&nbsp;ShootDate2 must be today or later.</li>";                       
            }
        }
        if (((sAction == "Add") || (sAction == "Edit") || (sAction == "Renew")) && (ShootDate3 != "01/01/1900")  && (ShootDate3 != "") )
        {
            if (compareDates(Today, "MM/dd/yyyy", ShootDate3,"MM/dd/yyyy") !=0)
            {
                sMsg = sMsg + "<li>&nbsp;ShootDate3 must be today or later.</li>";                       
            }
        }                
        return true;
}
    var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//Function to create XMLHttp Handler
function CreateXmlHttp()
{          
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");	    
    }
    catch(e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");		   
        } 
        catch(oc)
        {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari 
    if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
    {
        XmlHttp = new XMLHttpRequest();
    }	
}

function GetInnerText (node)
{
    var strNode ;
    if(BrowserDetect.browser == "Safari")
    {
        var serializer = new XMLSerializer();
	    var tempNode = serializer.serializeToString(node);
	    if(tempNode.indexOf("<value>") > -1 && tempNode.indexOf("</value>") > -1)
	    {
		    strNode = tempNode.replace("<value>", "");
		    strNode = strNode.replace("</value>", "");
	    }
	    if(tempNode.indexOf("<key>") >-1 && tempNode.indexOf("</key>") > -1)
	    {
		    strNode = tempNode.replace("<key>", "");
		    strNode = strNode.replace("</key>", "");
	    }
	}
    return (node.textContent || node.innerText || node.text || strNode) ;
}
       
function getParentDivID(ref)
{
    ok=0; // it's just to start the loop, we don't use it to get out.
    while (!ok)
	    {
	    ref = ref.parentNode;
	    if (ref.nodeType==1) //check that the node is a tag, not text (type=3)
		    {
		    if (String(ref.nodeName)=="DIV" && ref.id !="")
			    {
			    return ref.id;
			    }
		    }
	    }
} 
function fnMakeEditable(lnkobj,divid)
{
    var divobject = getParentDiv(lnkobj,divid);
    if( divobject )
    {
        var inputs = divobject.getElementsByTagName('input');
        for( var i=0; i<= inputs.length -1; i++ )
        {
            if( inputs[i].type == "text" )
                inputs[i].readOnly=false;
            if( inputs[i].type == "button" && inputs[i].id.indexOf('dtStartDate') != -1 )
                inputs[i].style.display = "inline";
            if( inputs[i].type == "button" && inputs[i].id.indexOf('dtEndDate') != -1 )
                inputs[i].style.display = "inline";
            if(  inputs[i].type == "hidden" && inputs[i].id.indexOf('hidOverride') != -1 )
                inputs[i].value = "True";
               
        }
        
    }
    
}

function getParentDiv(ref,divid)
{
    ok=0; // it's just to start the loop, we don't use it to get out.
    while (!ok)
	    {
	    ref = ref.parentNode;
	    if (ref.nodeType==1) //check that the node is a tag, not text (type=3)
		    {
		    if (String(ref.nodeName)=="DIV" && ref.id == divid)
			    {
			    return ref;
			    }
		    }
	    }
} 
