var submitType = "";

function checkFields(formName){
	var focusField="";
	var radioFields = new Array();
	if (submitType == "Delete"){
		return confirm('Permanently delete this listing?');
	} else if (submitType == "Save"){
		if (formName.address.value == ""){
			alert("Please enter an address for this listing prior to saving.");
			formName.address.focus();
			return false;
		} else {
			formName.status.value = "saved";
			return confirm('Your listing will not be submitted.  It will remain in your folder until you submit it to the Ad Manager.  Continue?');
		}
	} else if(submitType == "Submit"){
		var skippedFields = new Array();
		  // any more arguments sent will be fields that the user doesn't want to validate
		for (var i=1; i<checkFields.arguments.length; i++) {
			//alert(checkFields.arguments[i].name);
			skippedFields[checkFields.arguments[i].name] = 1;
		}
		var result="The following field(s) need to be filled out:\n\n";
		var errors=0;
		for (var i=0;i<formName.length;i++){
			if (skippedFields[formName.elements[i].name] != 1){ 			// First off, make sure I am not skipping this one
				if (formName.elements[i].type == "text" || 
				    formName.elements[i].type == "password" ||
				    formName.elements[i].type == "textarea"){			// For text fields
					if (formName.elements[i].value == ""){  		// Is it empty?
						var n=cleanupName(formName.elements[i].name);	// Get modified name for display
						result += n+"\n";				// add the modified form name to the result list
						if (!focusField) focusField=formName.elements[i];
						errors++;
					}
				} else if (formName.elements[i].type == "select-one"){		// For dropdown menus.  different way of checking value in netscape
					if (formName.elements[i].options[formName.elements[i].selectedIndex].value == ""){  		// Is it empty?
						var n=cleanupName(formName.elements[i].name);	// Get modified name for display
						result += n+"\n";				// add the modified form name to the result list
						if (!focusField) focusField=formName.elements[i];
						errors++;
					}
				} else if (formName.elements[i].type == "file"){		// For file fields
					if (formName.elements[i].value == ""){  		// Is it empty?
						var n=cleanupName(formName.elements[i].name);	// Get modified name for display
						result += n+"\n";				// add the modified form name to the result list
						if (!focusField) focusField=formName.elements[i];
						errors++;
					}
				} else if (formName.elements[i].type == "radio"){
					var radioObj = formName.elements[i];
					if (radioFields[radioObj.name] != true)
						radioFields[radioObj.name] = radioObj.checked;
				}
			}
		}
		for (i in radioFields)  { 
			//alert(radioFields[i]);
			if (radioFields[i] != true)  {
				var n=cleanupName(i);	// Get modified name for display
				result += n+"\n";	// add the modified form name to the result list
				errors++;
			}
		}
			// ***** Keller Williams specific
		if (formName.open_house[0].checked && formName.open_days[3].checked){
			result += "Open Days\n";
			errors++;
		}
		if (errors>0){
			alert(result);
			if(focusField) focusField.focus();
			return false;
		}
		if (formName.listing_id.value){
			formName.status.value = "modified";
		} else {
			formName.status.value = "submitted";
		}
		return true;
	} else {
		return false;
	}
}
function checkIntegerFields(formName){
	var skippedFields = new Array();
	  // any more arguments sent will be fields that the user doesn't want to validate
	for (var i=1; i<checkIntegerFields.arguments.length; i++) {
		skippedFields[checkIntegerFields.arguments[i].name] = 1;
	}
	var result="The following field(s) need to be 2-digit numeric values:\n\n";
	var errors=0;
	for (var i=0;i<formName.length;i++){
	  if (skippedFields[formName.elements[i].name] != 1 && formName.elements[i].type != "submit"){ 			// First off, make sure I am not skipping this one
	  	if (!isInteger(formName.elements[i].value)) {
	  		var n=cleanupName(formName.elements[i].name);	// Get modified name for display
	  		result += n+"\n";				// add the modified form name to the result list
	  		errors++;
	  	}
	  }
	}
	if (errors>0){
		alert(result);
		return false;
	}
	return true;
}


function checkRadios() {
 var el = document.forms[0].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
    alert("Please select a value for "+cleanupName(el[i].name)+".");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 

function checkPasswordFields(pass1,pass2){
	if (pass1.value != pass2.value){
		alert ('Password fields do not match!');
		return false;
	}
	return true;
}
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function cleanupName(formName){
	var n=formName.charAt(0).toUpperCase();				// store the first character of formName into n (convert to uppercase)
	for (var a=1;a<formName.length;a++){				// loop thru formName, starting at the 2nd character
		if (formName.charAt(a).search(/[A-Z]/) != -1 || 	// if the character is uppercase or
		    formName.charAt(a).search(/[0-9]/) != -1) {	 	// the character is a number or 
			n+=" "+formName.charAt(a);			//   start a new word by inserting space, then the next character
		} else if (formName.charAt(a).search(/_/) != -1){	// the character is an underscore(_):
			n+=" "+formName.charAt(a+1).toUpperCase();	//   replace the underscore with a space and capatalize the next letter
			a++;
		} else {						// otherwise
			n+=formName.charAt(a);				//   just add the next character
		}
	}
	
	// Below is specifically for form fields that end in 'id'
	// It just eliminates those letters at the end.  Shouldn't hurt most other uses
	if (n.substring(n.length-2,n.length) == "id"){
		n = n.substring(0,n.length-2);
	}
	return n;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isInteger (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
} 

function redimDays(maxNum,f){
	for (var i=1;i<32;i++){
		f.options[i]= new Option(i,i);
	}
	f.length = maxNum;
}

function textareaCounter(messageId, counterId, maxChars) {
	var myTextArea=document.getElementById(messageId);
	var myCounter=document.getElementById(counterId);
	if (myTextArea.value.length > maxChars) 
	    	myTextArea.value = myTextArea.value.substring(0, maxChars);
	else 
		myCounter.value = maxChars - myTextArea.value.length;
}
