
function DateValidate(source, arguments)
   {
      if (isValidDate(arguments.Value ) )
         arguments.IsValid=true;
      else
         arguments.IsValid=false;
         
   }
  
 function TimeValidate(source, arguments)
   {
  
      if (IsValidTime(arguments.Value ) )
         arguments.IsValid=true;
      else
         arguments.IsValid=false;
         
   }
   
function isValidDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            //alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    

    return true; // date is valid
}

function IsValidTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.
 //alert("Validating Time");
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) {
	//alert("Time is not in a valid format.");
	return false;
	}
	
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) {
		//alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null) {
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
			//alert("You must specify AM or PM.");
			return false;
		}
	}
	if  (hour > 12 && ampm != null) {
		//alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) {
		//alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		//alert ("Second must be between 0 and 59.");
		return false;
	}
return true;
}



function StartDrag()
{
	
	document.getElementById("pErrorPanel").attachEvent('onmousemove',DragWindow);	
}

function EndDrag()
{
	document.getElementById("pErrorPanel").detachEvent('onmousemove',DragWindow);	
}

function DragWindow()
{
	document.getElementById("pErrorPanel").style.left = window.event.clientX - 150;
	document.getElementById("pErrorPanel").style.top = window.event.clientY - 25;
}

function HideErrorPanel(){

	 document.getElementById("pErrorPanel").className = "ErrorPanel";
	 if (document.title == "Company Events"){
		document.getElementById("OfficeList").className = "ShowControl";
		document.getElementById("AgentList").className = "ShowControl";	
	}
	if(document.title == "Send Letters"){
		document.getElementById("ContactList").className = "ShowControl";
		document.getElementById("SelectedList").className = "ShowControl";
	}
}



function ShowErrorPanel()
{
	document.getElementById("pErrorPanel").className = "ShowErrorPanel";
	if (document.title == "Company Events"){
		document.getElementById("OfficeList").className = "HideControl";
		document.getElementById("AgentList").className = "HideControl";
	}
	if(document.title == "Send Letters"){
		document.getElementById("ContactList").className = "HideControl";
		document.getElementById("SelectedList").className = "HideControl";
	}
}

function InitErrorPanel()
{
	document.getElementById("pErrorPanel").onmousedown = StartDrag;
	document.getElementById("pErrorPanel").onmouseup = EndDrag;
	 //document.getElementById("pErrorPanel").className = "ShowErrorPanel";
}

 function SetDateValue(){
 
var d;


d = new Date();


dt = (d.getMonth() + 1) +'/' + d.getDate() + '/' + d.getFullYear()

return dt;

}

 function SetTimeValue(){
 

var t;

var sHours = new String();
var sMinutes = new String();


t = new Date();

if (t.getHours() >= 12){
	sAMPM = ' PM';
	if (t.getHours() > 12)
		sHours = (t.getHours() - 12) + ':';
	else
		sHours = (t.getHours()) + ':';
	
}else{
	sAMPM = ' AM';
	sHours = (t.getHours()) + ':';
}

if (sHours.length < 2)
	sHours = "0" + sHours;


if (t.getMinutes() < 10)
	sMinutes = "0" + t.getMinutes(); 
else
	sMinutes = t.getMinutes(); 

tm = sHours + sMinutes + sAMPM;
	
return tm;

}

function CatchKeyPress(KeyCode, Sender) { 
       var btnToBeClicked = null;
             
            
       if(KeyCode == '13') {
			var ButtonName = Sender.getAttribute("TargetButton");
			if (ButtonName == null) return;
			RemoveEnterAndEscEvents(); 
			btnToBeClicked = document.getElementById(ButtonName); 
				
			if(btnToBeClicked) { 
				btnToBeClicked.click(); 
			} 
		} 
	} 
	
function PressButton(ButtonName) { 
       var btnToBeClicked = null;

			if (ButtonName == null) return;
			btnToBeClicked = document.getElementById(ButtonName); 
				
			if(btnToBeClicked) { 
				btnToBeClicked.click(); 
			} 
	} 
	
	function PressParentButton(ButtonName) { 
       var btnToBeClicked = null;

			if (ButtonName == null) return;
			btnToBeClicked = self.opener.document.getElementById(ButtonName); 
				
			if(btnToBeClicked) { 
				btnToBeClicked.click(); 
			} 
	} 	
		
function RemoveEnterAndEscEvents() { 
			//if (event.keyCode == 13 || event.keyCode == 27)
			if (event.keyCode == 13) { 
				event.cancelBubble = true; event.returnValue = false; 
			} 
		} 
		

function FindListingChild() 
{	
	var WinSettings = "center=yes,resizable=yes,Height=500px,Width=750px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/FindListings.aspx", "dFindListingDialog", WinSettings,false);
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}	

		
function OpenSubscriptionReceipt() 
{	
	var WinSettings = "center=yes,resizable=yes,Height=500px,Width=750px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/SubscriptionReceipt.aspx", "dFindListingDialog", WinSettings,false);
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}		

function OpenNewEventChild() 
{
	
	var WinSettings = "center=yes,resizable=yes,Height=500px,Width=700px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/NewEvents.aspx", "dNewEventDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}	

function OpenEventChild(ReminderID) 
{
	
	var WinSettings = "center=yes,resizable=yes,Height=500px,Width=700px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/NewEvents.aspx?ReminderID=" + ReminderID, "dEventDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}	

function OpenLetterPreview(LetterID) 
{
	
	var WinSettings = "center=yes,resizable=yes,Height=500px,Width=700px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/PreviewLetter.aspx?LetterID=" + LetterID, "dEventDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}	
//this is the first to go
function OpenCompanyEventsChild() 
{
	
	var WinSettings = "center=yes,resizable=yes,Height=500px,Width=750px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/CompanyEvent.aspx", "dCompanyEventsDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}

function OpenPersonalEventChild(ReminderID) 
{

	var WinSettings = "center=yes,resizable=yes,Height=400px,Width=750px,scrollbars=yes,location=yes"
	
	if (ReminderID == 0)
		var MyArgs = window.open("http://" + document.location.host + "/PersonalEvent.aspx", "dPersonalEventsDialog", WinSettings,false);	
	else
		var MyArgs = window.open("http://" + document.location.host + "/PersonalEvent.aspx?ReminderID=" + ReminderID, "dPersonalEventsDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}		

function OpenCompanyEventChild(ReminderID) 
{

	var WinSettings = "center=yes,resizable=yes,Height=400px,Width=750px,scrollbars=yes,location=yes"
	
	if (ReminderID == 0)
		var MyArgs = window.open("http://" + document.location.host + "/EditCompanyEvent.aspx", "dPersonalEventsDialog", WinSettings,false);	
	else
		var MyArgs = window.open("http://" + document.location.host + "/EditCompanyEvent.aspx?ReminderID=" + ReminderID, "dPersonalEventsDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}		


function FindListingChild2() 
{
	var ListingID = "None";
	var ParmB = "";
	var ParmC = "";
	
	var MyArgs = new Array(ListingID, ParmB, ParmC);
	var WinSettings = "center:yes;resizable:no;dialogHeight:500px;dialogWidth:750px"
	// ALTER BELOW LINE - supply correct URL for Child Form
	var MyArgs = window.showModalDialog(
              "http://" + document.location.host + "/FindListings.aspx", 
                      MyArgs, WinSettings);
	if (MyArgs == null)
	{
		window.alert("Nothing returned from child. No changes made to input boxes")
	}
	else
	{
		ActivityListingID.value=MyArgs[0].toString();
		//retvalB.value=MyArgs[1].toString();
		//retvalC.value=MyArgs[2].toString();
		document.location = "http://" + document.location.host + "/AgentShowings2.aspx?ListingID=" + ActivityListingID.value;
	}
}

function OpenHelpDialog(HelpPage,height,width ) 
{	
	if (height == null)
		height=200;
		
	if (width == null)
		width=375;

	var WinSettings = "center=yes,resizable=yes,Height=" + height + "px,Width=" + width + "px,scrollbars=no,toolbar=no,status=yes";
	var MyArgs = window.open("http://" + document.location.host + "/Help Docs/" + HelpPage, "dFindListingDialog", WinSettings,false);
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');
}	



function OpenChildWindow(PageName,height,width ) 
{	
	if (height == null)
		height=200;
		
	if (width == null)
		width=375;

    
	var WinSettings = "center=yes,resizable=yes,Height=" + height + "px,Width=" + width + "px,scrollbars=no,toolbar=no,status=yes";
	var MyArgs = window.open(PageName, "dChildPage", WinSettings,false);

	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');


}	



function storeCaret (textEl) {
	if (textEl.createTextRange)
	 textEl.caretPos = document.selection.createRange().duplicate();
	}
	
function insertAtCaret(textEl, text) {
	
	if (textEl.createTextRange && textEl.caretPos) {
	 var caretPos = textEl.caretPos;
	 caretPos.text = text;
	}
	else
		alert('Please select a position in your letter to input text.');
	//textEl.value = text;
	}	
	
	function ClearTextBox(objtext,str){
	
	if (objtext.value == str)
		objtext.value = '';
	
	}
	
	function SetTextBox(objtext,str){
	
	if (objtext.value == '')
		objtext.value = str;
	
	}
	
function SelectDate(txtDate) 
{	

	height=250;
	width=250;
		
	var WinSettings = "center=yes,resizable=yes,Height=" + height + "px,Width=" + width + "px,scrollbars=no,toolbar=no,status=yes";
	var MyArgs = window.open("http://" + document.location.host + "/SelectDates.aspx?textbox=" + txtDate, "dCal", WinSettings,false);
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');
}	

function OpenSelectPlan(cid,rowidx) 
{
	
	var WinSettings = "center=yes,resizable=yes,Height=250px,Width=475px,scrollbars=yes"
	var MyArgs = window.open("http://" + document.location.host + "/SelectPlan.aspx?ContactID=" + cid + "&RowIndex=" + rowidx, "dSelectPlanDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');

}

function SendSellerListingLetter(lid) 
{
	
	var WinSettings = "center=yes,resizable=yes,Height=250px,Width=475px,scrollbars=yes"
	var MyArgs = window.open("http://www.viewmylisting.com/pgnewlistingmail.asp?ListingID=" + lid + "&isNew=No&isNewEMail=isNew", "dSelectPlanDialog", WinSettings,false);	
	if (!MyArgs)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');
	MyArgs.close();

}


function OpenPreviewWindow(PageName,height,width ) 
{	
	var MyArgs1
	if (height == null)
		height=800;
		
	if (width == null)
		width=750;

    
	var WinSettings = "center=yes,resizable=yes,Height=" + height + "px,Width=" + width + "px,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,addressbar=yes";
	MyArgs1 = window.open(PageName, "dPreviewPage", WinSettings,true);

	if (!MyArgs1)
		alert('You will need to disable your pop-up blocker in order for ViewMyListing.com to function correctly.  ViewMyListing.com does not display pop-up ads any where on this site.  If you receive pop-up ads while utilizing our service you may want to check your computer for adware.');
	else
		MyArgs1.document.location.reload(true);

}	



function clonetime(){
	document.getElementById("AlarmTime").value = document.getElementById("ReminderTime").value;
}


function CleanUpPhones(phone){

        var tmp;
        var tmpPhone = new String("");
        var x;
        var PN = new String(phone);
        
        if (PN.length > 0){                            //if there is a ohone Number
            for (x=0; x<PN.length; x++){               //loop though and pull out any non numbers
                tmp = PN.charCodeAt(x);
                if (tmp >= 48 && tmp <= 57)
                    tmpPhone = tmpPhone + PN.charAt(x);
            }

            if (tmpPhone.charAt(0) == "1")             //if the first number is 1 get rid of it
                tmpPhone = tmpPhone.substr(1);

            if (tmpPhone.length > 10)                  //if the total phone length > 10 make it 10
                tmpPhone = tmpPhone.substr(0,10);

        }

        return tmpPhone;

}

function FormatPhone(phone){
		
		var pn = new String(phone);
        pn = CleanUpPhones(pn)
      
        if (pn.length  == 10)
            pn = "(" + pn.substr(0,3) + ") " + pn.substr(3, 3) + "-" + pn.substr(6, 4);
        return pn;
}	