// Shell Properties
var Menu = window.parent.document.getElementById("Menu");
var tittler2 = window.parent.document.getElementById("tittler2");
var Loading = window.parent.document.getElementById("Loading");
var Loading2 = window.parent.document.getElementById("Loading2");
var TextContents = window.parent.document.getElementById("TextContents");
// Global Variables
var preloadFlag = true
var http_request = false;
var ActiveTab = '';
var VerificationTabMessage=''
var QuickLookupActive=false;
var secs
var timerID = null
var timerRunning = false
var delay = 1000
var DefaultStatusText = String.fromCharCode(169) + " 2007 Western Security Appraisal Services, PS";
self.status = DefaultStatusText;

// Remove Loading Screen
Loading.style.display='none';


// Launch WSAS Tracking App
function newImage(arg) {if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}
function ShowHideBlock(Fid) {
 	var obj = document.getElementById(Fid);
	if (obj.style.display == 'none') {
		obj.style.display="block";
	} else {
		obj.style.display="none";
	}
}
function ShowHideBloackWithNameChange(BlockToChange,NameBlock) {
 	var obj = document.getElementById(BlockToChange);
	var obj2 = document.getElementById(NameBlock);
	if (obj.style.display == 'none') {
		obj.style.display="block";
		obj2.innerHTML='Hide';
	} else {
		obj.style.display="none";
		obj2.innerHTML='Show';
	}
}

function TabCheck(TabName) {
	TabRelease();
	var Tab = document.getElementById('TabName'+TabName);
	var TabContents = document.getElementById('TabContents'+TabName);
	TabContents.style.display="block";
	Tab.style.backgroundColor="#CCCCEE";
}

function GetDynamicContent(url,DivID) {

	http_request = false;
	var obj = document.getElementById(DivID);
	obj.innerHTML = 'Loading...';
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { LoadContents(DivID); };
	http_request.open('GET', url, true);
	http_request.send(null);

}

function LoadContents(DivID) {

	if (http_request.readyState == 4) {
		var obj = document.getElementById(DivID);
		if (http_request.status == 200) {
			var obj = document.getElementById(DivID);
			obj.innerHTML = http_request.responseText;
		} else {
			obj.innerHTML ='There was a problem with your request.'+http_request.status;
		}
	}

}
function SetDynamicContent(Tab) {
	if (VerifyTab(ActiveTab)) {
		ProcessTab(ActiveTab);
		ActiveTab=Tab;
	} else {
		if (ActiveTab=='') {
			ActiveTab=Tab;
		} else {
			TabCheck(ActiveTab);
			alert ("Please make sure you have filled this form out correctly\n"+VerificationTabMessage);
		}
	}
}
function VerifyTab(Tab) {
	var AllowReturn = true;
	VerificationTabMessage ='';
	switch(Tab){
		case 'Client Info':
			var Info = document.ClientInfo;
			if (Info.RcvdDate.value=='') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Recieved Date\n";
				Info.RcvdDate.style.backgroundColor="#FFFF00";
				AllowReturn = false;
			} else {
				Info.RcvdDate.style.backgroundColor="#FFFFFF";
			}
			if (Info.ClientList.value=='NA' || Info.ClientList.value=='New') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Client\n";
				Info.ClientList.style.backgroundColor="#FFFF00";
				AllowReturn =  false;
			} else {
				Info.ClientList.style.backgroundColor="#FFFFFF";
			}
			if (Info.BranchList.value=='NA' | Info.BranchList.value=='New') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Client Branch\n";
				Info.BranchList.style.backgroundColor="#FFFF00";
				AllowReturn =  false;
			} else {
				Info.BranchList.style.backgroundColor="#FFFFFF";
			}
			if (Info.ContactList.value=='NA' | Info.ContactList.value=='New') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Client Contact\n";
				Info.ContactList.style.backgroundColor="#FFFF00";
				AllowReturn =  false;
			} else {
				Info.ContactList.style.backgroundColor="#FFFFFF";
			}
			if (Info.PropertyTypeList.value=='NA') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Property Type\n";
				Info.PropertyTypeList.style.backgroundColor="#FFFF00";
				AllowReturn =  false;
			} else {
				Info.PropertyTypeList.style.backgroundColor="#FFFFFF";
			}
			if (Info.PurposeList.value=='NA') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Appraisal Purpose\n";
				Info.PurposeList.style.backgroundColor="#FFFF00";
				AllowReturn =  false;
			} else {
				Info.ContactList.style.backgroundColor="#FFFFFF";
			}
			if (Info.ServiceList.value=='NA') {
				VerificationTabMessage = VerificationTabMessage+"»Missing Appraisal Service\n";
				Info.ServiceList.style.backgroundColor="#FFFF00";
				AllowReturn =  false;
			} else {
				Info.ContactList.style.backgroundColor="#FFFFFF";
			}
			
		break
	}
	return AllowReturn;
}
function VerifyFieldType(Type,Value,field){
	switch(Type){
		case 'StrictDateTime':
			if (Value.length==16) {
				var month = Value.substring(0,2);
				var day= Value.substring(3,5);
				var year = Value.substring(6,10);
				var hour = Value.substring(11,13);
				var minute = Value.substring(14,16);
				var dateerror = "The date you entered did not meet the following requirement(s) :\n\n";
				var datetest = true;
				var AMPM ='';
				var today = new Date();
				if ((today.getYear()-year)>1) {
						datetest = false;
						dateerror = dateerror + "Must have the current year\n";
				}else if ((year-today.getYear())>0) {
						datetest = false;
						dateerror = dateerror + "Can not enter year in the future\n";
				}else if((today.getYear()-year)>0){
					if (!(month==12 && (today.getMonth()==1) && ((today.getYear()-1)==year))) {
						datetest = false;
						dateerror = dateerror + "Can not use last year\n";
					}
				}
				if (month>12) {
					datetest = false;
					dateerror = dateerror + "Must be an two digit month\n";
				} else {
					if (month > (today.getMonth()+1)) {
						datetest = false;
						dateerror = dateerror + "Can not enter month in the future\n";
					} else if (((today.getMonth()+1) - month) >1){
						datetest = false;
						dateerror = dateerror + "Can not enter prior to last month\n";
					} else {
						if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 ) {
							if (day>31){
								dateerror = dateerror + "Must be an two digit day between 01 and 31\n";
								datetest = false;
							}
						} else if(month==2) {
							if (Leap(year)) {
								if (day>29){
									dateerror = dateerror + "Must be an two digit day between 01 and 29\n";
									datetest = false;
								}
							} else {
								if (day>28){
									dateerror = dateerror + "Must be an two digit day between 01 and 28\n";
									datetest = false;
								}
							}					
						} else  {
							if (day>30){
								dateerror = dateerror + "Must be an two digit day between 01 and 30\n";
								datetest = false;
							}
						}
					}
				}
				if (day<1) {
					dateerror = dateerror + "Must be an two digit day greater then 0\n";
					datetest = false;
				}
				if ((day>(today.getDate+1)) && (month==(today.getMonth+1))) {
					dateerror = dateerror + "Can not enter day in the future\n";
					datetest = false;
				}
				if ((day<(today.getDate+1)) && (month==(today.getMonth))) {
					dateerror = dateerror + "Can not enter day over 30 days in the past\n";
					datetest = false;
				}
				if (hour>12 && hour<25) {
						hour = (hour-12);
						AMPM='PM';
				}
				if (hour ==0) {
						hour=(hour+12);
						AMPM='AM';
				}
				if (hour >24) {
					dateerror = dateerror + "Hour must be in 12 or 24 hour format only\n";
					datetest = false;
				}
				if (minute >59) {
					dateerror = dateerror + "Minute must be less then 60\n";
					datetest = false;
				}
				if (datetest) {
					while (!(AMPM=='AM' || AMPM=='PM')) {
						AMPM=prompt('is this AM or PM?',AMPM);
					}
					NewValue=month+"/"+day+"/"+year+" "+hour+":"+minute+" "+AMPM;
					field.value=NewValue;
					field.style.backgroundColor='#FFFFFF';
					
				} else {
					alert(dateerror);
					field.style.backgroundColor='#FFFF00';
					field.focus();
					
				}
			} else if (Value.length==19) {
				
				
			} else {
				alert('Please completely fill out this field');
				field.style.backgroundColor='#FFFF00';
				field.focus();
			}
		break
		
		
	}
	
}
function CheckForNew(Type,IsNew) {
	if (IsNew=="New") {
		var Element = "New"+Type;
		var NewForm=document.getElementById(Element);
		NewForm.style.display="block";
		switch(Type){
			case 'Client':
			
			
			break
			case 'Branch':
				document.NewBranchForm.ClientID.value=document.ClientInfo.ClientList.value;
			
			break
			case 'Contact':
				document.NewContactForm.ClientID.value=document.ClientInfo.ClientList.value;
				document.NewContactForm.BranchID.value=document.ClientInfo.BranchList.value;
			
			break		
		}
}
}
function CityStateCountyZipLookup(Section,FormName,idValue) {
	if (idValue.length<2){
		document.getElementById("hiddenDIV2").style.display="none"; 
		return;
	}
	document.getElementById("hiddenDIV2Inner").innerHTML="Searching....";
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http = new XMLHttpRequest();
		if (http.overrideMimeType) {
			http.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	var myRandom = parseInt(Math.random()*99999999);  // cache buster
	var url = "content.php?AjaxAction=CityCountyStateZipLookup&Form="+FormName+"&Section="+Section+"&Search=" + escape(idValue) + "&rand=" + myRandom;
	QuickLookupActive=true;
	http.open("GET", url, true);
	http.onreadystatechange = CityStateCountyZipPopup;
	http.send(null);
}
function CityStateCountyZipPopup() {
	if (http.readyState == 4 && 	QuickLookupActive==true) {
		document.getElementById("hiddenDIV2")
		.style.display="block"; 
		document.getElementById("hiddenDIV2Inner")
		.innerHTML=http.responseText;
	}
	
}
function UpdateField(Form,Value,Field){
	switch (Form) {
		case 'PropertyInfo':	
			var Field = document.PropertyInfo[Field];
			Field.value = Value;
		break;
		case 'NewClientForm':
			var Field = document.NewClientForm[Field];
			Field.value = Value;
		break;
		
		
	}
	document.getElementById("hiddenDIV2").style.display="none"; 	
}
function CloseQuickLookup(){
	QuickLookupActive=false;
}
function UpdateFields(Form,City,State,County,Zip) {
	switch (Form) {
		case 'PropertyInfo':
			var EditForm = document.PropertyInfo;
			EditForm.PropCity.value = City;
			EditForm.PropState.value = State;
			EditForm.PropCounty.value = County;
			EditForm.PropZip.value = Zip;
		break;
		case 'NewClientForm':
			var EditForm = document.NewClientForm;
			EditForm.City.value = City;
			EditForm.State.value = State;
			EditForm.Zip.value = Zip;
		break;		
		
		
	}
	document.getElementById("hiddenDIV2").style.display="none"; 
}
function ProcessTab(Tab) {
	var DivTab = document.getElementById('TabContents'+Tab);
	
	switch(Tab){
		case 'Client Info':
			var Info = document.ClientInfo
			ContentString = "content.php?AjaxAction=Order&NewOrderAjax=UpdateClientInfo&ID="+OrderID+"&ClientOrderNum="+Info.ClientOrderNum.value+"&LoanNumber="+Info.LoanNumber.value+"&BranchList="+Info.BranchList.value+"&ContactList="+Info.ContactList.value+"&ServiceList="+Info.ServiceList.value+"&PropertyTypeList="+Info.PropertyTypeList.value+"&PurposeList="+Info.PurposeList.value+"&RcvdDate="+Info.RcvdDate.value;
			GetDynamicContent(ContentString,"TabContents"+Tab)
		break
		case 'Property Info':
			var Info = document.PropertyInfo
			ContentString ="content.php?AjaxAction=Order&NewOrderAjax=UpdatePropertyInfo&ID="+OrderID+"&PropStreetNumber="+Info.PropStreetNumber.value+"&PropDirection="+Info.PropDirection.value+"&PropStreetName="+Info.PropStreetName.value+"&PropCity="+Info.PropCity.value+"&PropCounty="+Info.PropCounty.value+"&PropState="+Info.PropState.value+"&PropZip="+Info.PropZip.value;
			GetDynamicContent(ContentString,"TabContents"+Tab)
		break
		case 'Borrower Info':
			var Info = document.BorrowerInfo
			ContentString = "content.php?AjaxAction=Order&NewOrderAjax=UpdateBorrowerInfo&ID="+OrderID+"&Borrower1Last="+Info.Borrower1Last.value+"&Borrower1First="+Info.Borrower1First.value+"&OwnerID="+Info.OwnerID.value+"&Occupant="+Info.Occupant.value+"&Borrower1HP="+Info.Borrower1HP.value+"&Borrower1WP="+Info.Borrower1WP.value+"&Borrower2HP="+Info.Borrower2HP.value+"&Borrower2WP="+Info.Borrower2WP.value+"&Borrower2Last="+Info.Borrower2Last.value+"&Borrower2First="+Info.Borrower2First.value;
			GetDynamicContent(ContentString,"TabContents"+Tab)

		break
		case 'Contact Info':
		
		break
		case 'Billing Info':
		
		break
		case 'Instructions':
		
		break
		case 'Notes':
		
		break
	}
}
function Leap(Year)	{
	if ( (Year % 4) == 0){
		// It is exactly divisible by 4

		if ( (Year % 100) == 0)	{
			// It is exactly divisible by 100
			// Is it also exactly divisible by 400?
			Result = ( (Year % 400) == 0);
		} else	{
			return true;
		}
	} else {
		// It is not exactly divisible by 4
		// It is not a leap year
		return false;
	}
}
function TestComment(){
	var ValidEmail=/^[a-z0-9][^\(\)<\>\@\,\;\"\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i;
	var email = document.contactform.email;
	var name = document.contactform.name;
	var comment = document.contactform.comment;
	var Error=false;
	var Message = "Please completely fill out this form\n\r"
	if (!ValidEmail.test(email.value)) {
		Message = Message + "Invalid E-mail address\n\r";
		email.style.backgroundColor='#FFFF00';
		Error = true;
	}
	if (name.value=="") {
		Message = Message +  "Missing your name\n\r";
		name.style.backgroundColor='#FFFF00';
		Error = true;
	}
	if (Error) {
		alert(Message);
		return false;		
	}
	return true;
}
function DownloadBegin(){
	var obj = document.getElementById('Downloader');
	obj.style.display='block';
	InitializeTimer();
}

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 10
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
       	var obj = document.getElementById('Downloader');
		obj.style.display='none';
		self.status = DefaultStatusText
    }
    else
    {
        self.status = secs + " Seconds Remain";
        var obj = document.getElementById('Counter');
		obj.innerHTML=secs;
		secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
		
    }
}
