/**
 * This Javascript file contains commonly used functions.
 *
 * @author   fuentesc
 * @history  01/14/2008  fuentesc  Created
 *
 */

// Hopefully not much more than these function will be needed

function popupWindowURL(url, winname,  w, h, menu, resize, scroll, x, y) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	if (resize == null) resize = 1;
	
	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";

	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	
	if (x == null || y == null) {
		cwin=window.open(url,winname, + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);
	}
	else {
		cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);
	}
	if (!cwin.opener) cwin.opener=self;
	
	// Allow an object to be passed to the new window
	if (arguments[9] && typeof arguments[9] == "object") cwin.initObj = arguments[9];
	
	cwin.focus();

	return cwin;
}


function popupFullWindow(url, winname,  w, h) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	
	cwin=window.open(url,winname,"status,menubar,resizable,scrollbars,toolbar,location" + ",width=" + w + ",height=" + h);
	
	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return;
}

function Trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function ValueChangeSubmit(FormName,FieldName,Value) {
	var Form = document[FormName];
	var Field = Form[FieldName];
	Field.value = Value;
	Form.submit();
	return false;
}

//given a form checkbox name, this function will set all checkboxes of the name specified with the Status value specified
function CheckAll(CheckBox, Status) {
	if (CheckBox.length > 1) {
		for (var i=0; i < CheckBox.length; i++) {
			if (CheckBox[i].disabled == false) CheckBox[i].checked = Status;
	 	}
	} else {
		if (CheckBox.disabled == false) CheckBox.checked = Status;
	}
}

//Returns the total amount of items checked in checkbox object "CheckBox"
function TotalChecked(CheckBox) {
	var CheckBoxCounter=0;
	if (CheckBox.length > 1) {
		for (var i=0; i < CheckBox.length; i++) 
			if (CheckBox[i].checked) CheckBoxCounter = CheckBoxCounter + 1;
	} else {
		if (CheckBox.checked) CheckBoxCounter = 1;
	}
	return CheckBoxCounter;
}

//if any checkboxes are checked, this function will return a true. Otherwise false.
function IsChecked(CheckBox) {
	if (CheckBox.length > 1) {
		for (var i=0; i < CheckBox.length; i++) {
			if (CheckBox[i].checked) {
				return true;
			}
	 	}
		return false;
	} else {
		if (CheckBox.checked) {
			return true;
		} else {
			return false;
		}
	}
}

// Function to validate if a string is a number
function isNumeric(inString) {
	if (isEmpty(inString) || isNaN(parseFloat(inString)))
		return false;

	return true;
}

// Function to validate if a string is empty
function isEmpty(inString) {
	if (inString == null || inString == "")
		return true;
		
	return false;
}

// Function to validate if a string is a date format: 01/01/2005 or 01-01-2005
function isDate(inString) {
	if (isEmpty(inString))
		return false;

	var tmpStringArray = inString.split('/');

	if (tmpStringArray.length != 3)
		tmpStringArray = inString.split('-');

	if (tmpStringArray.length != 3) return false;

	if (isNaN(tmpStringArray[0]) || isNaN(tmpStringArray[1]) || isNaN(tmpStringArray[2])) return false;
	if (tmpStringArray[0] < 1    || tmpStringArray[1] < 1    || tmpStringArray[2] < 0)    return false;
	if (tmpStringArray[0] > 12   || tmpStringArray[1] > 31   || tmpStringArray[2] > 9999) return false;

	if (isNumeric(Date.parse(tmpStringArray.join('/'))))
		return true;

	return false;
} 

// This function will validate the length of the text in a HTML textarea field.
function checkTextLength(myTextArea,maxLength) {
	if (myTextArea.value.length > maxLength) {
		var msg = 'The text in this field is ' + myTextArea.value.length + ' characters long. '
		        + 'The maximun allowed is ' + maxLength	+ '.\nPlease modify your text and try again.';
		alert(msg);
		myTextArea.focus();
		return false;
	}
	return true;
}

// Function to validate if a string is a date format(MM/YYYY or MM/YY): 01/2005 or 01-2005
function isDateAbbrev(inString) {
	if (isEmpty(inString))
		return false;
	var tmpStringArray = inString.split('/');

	if (tmpStringArray.length != 2)
		tmpStringArray = inString.split('-');

	if (tmpStringArray.length != 2) return false;
	
	if (isNaN(tmpStringArray[0]) || isNaN(tmpStringArray[1])) return false;
	if (tmpStringArray[0] < 1    || tmpStringArray[1] < 0)    return false;
	if (tmpStringArray[0] > 12   || tmpStringArray[1] > 9999) return false;

	return true;
}

function ValueProvided() {
	var areaSearch = $('input[name=AreaSearch]').val();
	var lastInput = $('#choiceZIP').text();
	lastInput = String(lastInput).substring(String(lastInput).length, String(lastInput).length - 5);
	if (isNumeric(areaSearch) && areaSearch.length == 5) {
		if ($('#SearchAreaChoices:visible').length && areaSearch == lastInput) {
			$('#searchindex1').submit();
			return true;
		} else {
			if ( $('#ProgressIndicator:visible').length ) {
				// Ajax call is still awaiting a response
				return false;
			}
			$('#searchindex1').after('<div id="ProgressIndicator" class="Loading"><div class="LoadingText">Searching...</div></div>');
			$.ajax({
				type: "GET",
				url: "/index.cfm",
				data: "Action=MLSIsZipCode&AreaSearch="+areaSearch,
				timeout:5000,
				success: function(data){
					data = parseInt(data);
					$('#ProgressIndicator').remove();
					if (data=='0') {
						// MLS without a ZIP match (prefix it with a pound sign)
						$('input[name=AreaSearch]').val('#'+$('input[name=AreaSearch]').val());
						$('#searchindex1').submit();
						return true;	// force MLS search
					} else if (data=='1') {
						// MLS has a ZIP match
						$('#SearchAreaChoices').show();
						$('#choiceZIP').text("ZIP code "+areaSearch);
						$('#choiceMLS').text("MLS # "+areaSearch);
						return false;	// need an answer
					} else {
						$('#searchindex1').submit();
						return true;	// let it pass through
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$('#ProgressIndicator').remove();
					$('#searchindex1').submit();
					return true;	// let it pass through
				}
			});
		}
		return false;	// need an answer
	} else if (areaSearch == $('input[name=AreaSearch]')[0].defaultValue || areaSearch.length == 0) {
		$('input[name=AreaSearch]').val( $('input[name=AreaSearch]')[0].defaultValue );
		return false;	// nothing to submit
	} else {
		return true;	// let it pass through
	}
}
