/**
 * General javascript functions...
 */


// Image rollover functions...

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// Only allow specific keys to be pressed in text boxes...
function theseKeysOnly(e,goods) {
	var key, keychar;
	key = getKey(e);
	if (key == null)
	return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	if (goods.indexOf(keychar) != -1) return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 ) return true;
	return false;
}

function getKey(e) {
	if (window.event) return window.event.keyCode;
	else if (e) return e.which;
	else return null;
}




// Submit form on Enter...
function handleKeyPress(e,form) {
	var key=e.keyCode || e.which;
	if (key==13) {
		if (valLogin(form)) { form.submit(); }
	}
}





// Track remaining bytes of data available in a textarea...
function typeTrack(frmField, lblField, maxLen) {
	if ((maxLen - frmField.value.length) < 0) {
		alert("Maximum number of characters exceeded." +
			"\nTruncating information to the first " + maxLen + " characters only.");
		var str = new String(frmField.value);
		str = str.substring(0,maxLen);
		frmField.value = str;
		document.getElementById(lblField).innerHTML = (maxLen - frmField.value.length) + " characters remaining";
	} else {
		document.getElementById(lblField).innerHTML = (maxLen - frmField.value.length) + " characters remaining";
	}
}

function stopTypeTrack(lblField, txtContent) {
	document.getElementById(lblField).innerHTML = txtContent;
}




/* Trim function */

function trim(s) {
	// Remove leading spaces and carriage returns...
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
		s = s.substring(1,s.length);
	}
	
	// Remove trailing spaces and carriage returns...
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
		s = s.substring(0,s.length-1);
	}
	return s;
}





/**
 * Page-specific javascript functions...
 */


/* Validate login form - include_footer.php */

function valLogin(frm) {
	if (frm.user.value == '') {
		if (frm.pass.value == '') {
			alert("Please enter a User Name and Password to log in.");
			return false;
		} else {
			alert("Please enter a User Name to log in.");
			return false;
		}
	} else {
		if (frm.pass.value == '') {
			alert("Please enter a Password to log in.");
			return false;
		} else {
			return true;
		}
	}
}



/* Validate contact us form - contact.php */

function valContactUs(frm) {
	
	if (frm.comments.value == '') {
		alert("Please enter a comment or question before trying to send your message."
			+ "\n\nThank You.");
		return false;
	}

	if (frm.email.value != "") {
		if (frm.email.value.indexOf('@') == -1 || frm.email.value.indexOf('.') == -1) {
			alert("The E-mail Address field does not contain a valid E-mail Address."
				+ "\nPlease be sure you enter the address in the following format:"
				+ "\nname@domain.com"
				+ "\n\nThank You.");
			return false;
		}
	} else {
		areYouSure = window.confirm("You did not include an email address."
			+ "\n" + "Are you sure you want to send this message anonymously?"
			+ "\n" + "Unless you provide a phone number in the Comments area, we will not be able to contact you."
			+ "\n"
			+ "\n" + "Click 'OK' to send the message anyway."
			+ "\n" + "Click 'Cancel' to return without sending the message.");
		if (!areYouSure) {
			return false;
		}
	}
	
	return true;
}



/* Validate edit text - admin.php */

function valEditText(frm,textId) {

	if (eval("trim(frm.textContent_" + textId + ".value)") == "") {
		alert("Please type some text before updating.\nThank you.");
		return false;
	}
	
	return true;
}







/**
 * Available Homes javascript functions...
 *
 */
 

function availableHomesValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this home?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}

function availableHomesValAdd(frm) {
	if (trim(frm.address.value) == "") {
		alert("Please type an address before uploading.");
		frm.address.focus();
		return false;
	}
	
	if (trim(frm.typeId.value) == "Choose One") {
		alert("Please select a type before uploading.");
		frm.typeId.focus();
		return false;
	}
	
	return true;
}

function availableHomesValUpdate(frm,homeId) {
	var addy = eval("frm.address_" + homeId + ".value");
	if (trim(addy) == "") {
		alert("Please type an address before uploading.");
		eval("frm.address_" + homeId + ".focus();");
		return false;
	}
	
	var typ = eval("frm.typeId_" + homeId + ".value");
	if (trim(typ) == "Choose One") {
		alert("Please select a type before uploading.");
		eval("frm.typeId_" + homeId + ".focus();");
		return false;
	}
	
	return true;
}


function availableHomesValUploadImage(frm) {
	if (trim(frm.pic_for_upload.value) == "") {
		alert("Please click \"Browse...\" to select a photo before uploading.");
		document.images['upload'].style.display='inline';
		return false;
	}
	
	return true;
}


function availableHomesValDelImage() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this image?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}







/**
 * Communities javascript functions...
 *
 */
 
 

function communitiesValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this community?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}

function communitiesValAdd(frm) {
	if (trim(frm.communityName.value) == "") {
		alert("Please type a community name before uploading.");
		return false;
	}
	
	return true;
}

function communitiesValUpdate(frm,communityId) {
	var nme = eval("frm.communityName_" + communityId + ".value");
	if (trim(nme) == "") {
		alert("Please type a community name before uploading.");
		return false;
	}
	
	return true;
}


function communitiesValUploadImage(frm) {
	if (trim(frm.pic_for_upload.value) == "") {
		alert("Please click \"Browse...\" to select a photo before uploading.");
		document.images['upload'].style.display='inline';
		return false;
	}
	
	return true;
}


function communitiesValDelImage() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this image?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}






/**
 * Home Styles javascript functions...
 *
 */
 
 

function homeStylesValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this home style?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}

function homeStylesValAdd(frm) {
	if (trim(frm.styleName.value) == "") {
		alert("Please type a style name before uploading.");
		return false;
	}
	
	return true;
}

function homeStylesValUpdate(frm,styleId) {
	var nme = eval("frm.styleName_" + styleId + ".value");
	if (trim(nme) == "") {
		alert("Please type a style name before uploading.");
		return false;
	}
	
	return true;
}


function homeStylesValUploadImage(frm) {
	if (trim(frm.pic_for_upload.value) == "") {
		alert("Please click \"Browse...\" to select a photo before uploading.");
		document.images['upload'].style.display='inline';
		return false;
	}
	
	return true;
}


function homeStylesValDelImage() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this image?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}







/**
 * Links javascript functions...
 *
 */
 
 


function linksValCreate(frm) {
	if (trim(frm.new_category.value) == "") {
		alert("Please type a category before adding this link.");
		return false;
	}
	
	if (trim(frm.display_name.value) == "") {
		alert("Please type a link display name before adding this link.");
		return false;
	}
	
	return true;
}



function linksValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this link?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}






/**
 * Gallery javascript functions...
 *
 */
 
 

function galleryValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this photo?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}

function galleryValUpload(frm) {
	if (trim(frm.new_gallery.value) == "") {
		alert("Please type a gallery name before uploading.");
		document.images['upload'].style.display='inline';
		return false;
	}
	
	if (trim(frm.pic_for_upload.value) == "") {
		alert("Please click \"Browse...\" to select a photo before uploading.");
		document.images['upload'].style.display='inline';
		return false;
	}
	
	return true;
}






/**
 * Testimonials javascript functions...
 *
 */
 
 

function testimonialsValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this testimonial?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}

function testimonialsValAdd(frm) {
	
	if (trim(frm.homeowner.value) == "") {
		alert("Please type a Home Owner Name before adding.");
		document.images['add'].style.display='inline';
		return false;
	}
	
	if (trim(frm.test_desc.value) == "") {
		alert("Please type a Testimonial before adding.");
		document.images['add'].style.display='inline';
		return false;
	}
	
	return true;
}

function testimonialsValEdit(ho,td,img_nme1, img_nme2) {
	
	if (trim(ho) == "") {
		alert("Please type a Home Owner Name before updating.");
		document.images[img_nme1].style.display='inline';
		document.images[img_nme2].style.display='inline';
		return false;
	}
	
	if (trim(td) == "") {
		alert("Please type a Testimonial before updating.");
		document.images[img_nme1].style.display='inline';
		document.images[img_nme2].style.display='inline';
		return false;
	}
	
	return true;
}






/**
 * Accolades javascript functions...
 *
 */
 
 

function accoladesValDel() {
	v_AreYouSure = window.confirm("Are you sure you want to delete this accolade?"
		+ "\n"
		+ "\n" + "Click 'OK' to delete."
		+ "\n" + "Click 'Cancel' to return without deleting.");
	if (v_AreYouSure == true) {
		return true;
	} else {
		return false;
	}
}

function accoladesValAdd(frm) {
	
	if (trim(frm.accTitle.value) == "") {
		alert("Please type an Accolade Title Name before adding.");
		document.images['add'].style.display='inline';
		return false;
	}
	
	if (trim(frm.accText.value) == "") {
		alert("Please type the Accolade Text before adding.");
		document.images['add'].style.display='inline';
		return false;
	}
	
	if (trim(frm.thumb_for_upload.value) != "") {
		if (trim(frm.file_for_upload.value) == "") {
			alert("You are attempting to upload a thumbnail without a file."
				+ "\n" + "Please click \"Browse...\" to select a file attachment before uploading.");
			document.images['add'].style.display='inline';
			return false;
		}
	} else {
		if (trim(frm.file_for_upload.value) != "") {
			alert("You are attempting to upload a file without a thumbnail."
				+ "\n" + "Please click \"Browse...\" to select a thumbnail image before uploading.");
			document.images['add'].style.display='inline';
			return false;
		}
	}
	
	return true;
}

function accoladesValEdit(frm, accId, ph, ac, td, img_nme1, img_nme2) {
	
	if (trim(ac) == "") {
		alert("Please type an Accolade Title before updating.");
		document.images[img_nme1].style.display='inline';
		document.images[img_nme2].style.display='inline';
		return false;
	}
	
	if (trim(td) == "") {
		alert("Please type the Accolade Text before updating.");
		document.images[img_nme1].style.display='inline';
		document.images[img_nme2].style.display='inline';
		return false;
	}
	
	if (ph == "false") {
		if (trim(eval("frm.thumb_for_upload_" + accId + ".value")) != "") {
			if (trim(eval("frm.file_for_upload_" + accId + ".value")) == "") {
				alert("You are attempting to upload a thumbnail without a file."
					+ "\n" + "Please click \"Browse...\" to select a file attachment before uploading.");
				document.images[img_nme1].style.display='inline';
				document.images[img_nme2].style.display='inline';
				return false;
			}
		} else {
			if (trim(eval("frm.file_for_upload_" + accId + ".value")) != "") {
				alert("You are attempting to upload a file without a thumbnail."
					+ "\n" + "Please click \"Browse...\" to select a thumbnail image before uploading.");
				document.images[img_nme1].style.display='inline';
				document.images[img_nme2].style.display='inline';
				return false;
			}
		}
	}
	
	return true;
}


