/*<!--- do not compress --->
File:  		JsUtilities.js
Description: All the JavaScript Utilities that are used by Homes.com.


Author:		Kiran Adapa
Date:		Feb 2, 2000
*/

// A function to validate the E-mail
 function validEmail(email) {
    invalidChars = " /:,;"

  if (email == "") {      // cannot be empty
   return false
  }
  for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
   badChar = invalidChars.charAt(i)
   if (email.indexOf(badChar,0) > -1) {
    return false
   }
  }
  atPos = email.indexOf("@",1)   // there must be one "@" symbol
  if (atPos == -1) {
   return false
  }
  if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
   return false
  }
  periodPos = email.indexOf(".",atPos)
  if (periodPos == -1) {     // and at least one "." after the "@"
   return false
  }
  if (periodPos+3 > email.length) {  // must be at least 2 characters after the "."
   return false
  }
  return true
}

// Check form field content.
function checkField (thisform, fieldvalue) {
	if (thisform.BookmarkName.value == "") {
	   alert ('Please enter ' +  fieldvalue);
	   return false;
	}
	return true;
}

// Check form for E-mail addresses
 function checkForm (emailform) {
 	  if (emailform.email_target_email.value == "") {
	      alert ('Please enter "Send to" address.');
		  return false
	  }
	  else {  
		   if (!validEmail(emailform.email_target_email.value)) {
		    alert('Invalid "Send to" email address')
		    emailform.email_target_email.focus()
		    emailform.email_target_email.select()
		    return false
		   }
	  }
	  
	   if (emailform.email_from.value == "") {
	      alert ('Please enter "Your email" address.');
		  return false
	   }
	   else {
	   	   if (!validEmail(emailform.email_from.value)) {
		    alert('Invalid email address')
		    emailform.email_from.focus()
		    emailform.email_from.select()
		    return false
		   }
	   }

	  return true
 }

// Check thisform's multiple select list and give an error if selected 
// items count is more than maxcount.
function checkSelectionCount (thisform, selname, maxcount) {
   var j = 0;


   for (var i=0; i<thisform.elements[selname].options.length; i++) {

   	   if(thisform.elements[selname].options[i].selected) {
		   	if ((thisform.elements[selname].options[i].value == 'PUBLIC') ||
				(thisform.elements[selname].options[i].value == 'PRIVATE')) {
				alert('Do not select the school type identifiers - "PUBLIC" or "PRIVATE"');
				return false;
			}
	   }
	   
   }

   
   for (var i=0; i<thisform.elements[selname].options.length; i++) {
   	   if(thisform.elements[selname].options[i].selected) j++;		   
   }

   if (j > maxcount) {
	   alert("Please select " + maxcount + " or fewer items.\nCurrently selected items: " + j);
       return false;
   }
   else {
       return true;
   }

}

// Pops up a child window with the given window name and dimensions
/*
function popupWindow(winname,w,h) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	cwin=window.open("", winname,
	"nomenubar,status,scrollbars,noresizable,width=" + w + ",height=" + h);
return true;
}
*/

// Pops up a child window with the given window name and dimensions.
// Takes the optional arguments to set the menubar, resize, and scrollbars attributes of Window.
function popupWindow(winname,  w, h, menu, resize, scroll) {
	popupWindowURL("", winname,  w, h, menu, resize, scroll);
return true;
}

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;
	cwin.focus();
return true;
}

// Submits the form associated with "thiswin" window and displays the results in 
// "popupname" window of dimensions 'w' and 'h'.  Uses popupWindow function. 
function submitClose(thiswin, popupname, w, h, menu, resize, scroll) {
	if (popupname == null) popupname = "resultswindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	popupWindow(popupname, w, h, menu, resize, scroll);
	
	thiswin.document.forms[0].submit();
	
	thiswin.close();
}

// Checks(status true) or unchecks(status false) all the checkboxes associated with the form.
function CheckUncheck(status) {
   for (i = 0; i < document.forms[0].length; i++) {
      document.forms[0].elements[i].checked = status
   }
}

// Reads user cookie
function ReadCookie (CookieName) {
  var CookieString = document.cookie;
//  var CookieSet = CookieString.split (';');
  var CookieSet = splitArray(CookieString, ';');
  var SetSize = CookieSet.length;
  var CookiePieces
  var ReturnValue = "";
  var x = 0;

  for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {

//    CookiePieces = CookieSet[x].split ('=');
    CookiePieces = splitArray(CookieSet[x], '=');

    if (CookiePieces[0].substring (0,1) == ' ') {
      CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
    }

    if (CookiePieces[0] == CookieName) {
      ReturnValue = CookiePieces[1];
    }

  }

  return ReturnValue;
}

// Swaps one Character for Another
function SwapStr (str, fnd, rpl) {
  if (rpl.indexOf(fnd) != -1) return str;

  while ((pos = str.indexOf(fnd)) != -1) {
    str = str.substring(0,pos) + rpl + str.substring(pos+fnd.length,str.length);
  }

  return str;
}

// A JavaScript 1.0 version of split
function splitArray(joinedString, splitString) {
	var tempArray = new Array(0);
	var arrayCounter = 0;
	var elementBoolean	= true;
	for (var i = 0; i < joinedString.length; i++) {
		if(elementBoolean) {
			tempArray[arrayCounter] = "";
			elementBoolean 		  = false;
			}
		if(joinedString.charAt(i) == splitString) { 
			arrayCounter++;
			elementBoolean = true;
			continue;
			}
		tempArray[arrayCounter] += joinedString.charAt(i);
		}
	return tempArray;
	}


//If a checkbox is selected in a form, this will give a 
//confirmation as to whether they really want to check 
//the box or not. Clicking "Ok" will leave the checkbox alone. 
//Clicking "Cancel" will make the checkbox become unchecked.
//
//Inputs:	thisform (the form object identifier)
//		checkname (the name of the form variable for the checkbox)
function VerifyCheckBox(thisform, checkname) {
   if (thisform.elements[checkname].checked) {
	   if (confirm("Are you sure?")) {
		        thisform.elements[checkname].checked=true;
			return true;
		} else {
	   		thisform.elements[checkname].checked=false;
			return false;
		}
	}
}

//	If the page has been called in a frame, this will reload the location 
//	in the top of the browser window.
function VerifyLoadedOnTop() {
	if (top.frames.length!=0)
	top.location=self.document.location;
	return true;
}

