//<SCRIPT>
//---------------------------
//RimskyS-001011, Client-Side include for Resident-Site pages
//---------------------------

//------------------------------
// Check whether cookies enabled
//------------------------------
function AreCookiesEnabled()
{
   document.cookie = "Enabled=true";
   var cookieValid = document.cookie;
   
   // if retrieving the VALUE we just set actually works 
   // then we know cookies enabled
   if (cookieValid.indexOf("Enabled=true") != -1)
   {
      cookiesEnabled = true;
   }
   else
   {
      cookiesEnabled = false;
   }
   
   return (cookiesEnabled);
}

//---------------------------
// trim left - (non regular expressions) 
//---------------------------
function ltrim(strIn){
var strTemp = new String(strIn);
  
  for (var i=0; i<= strIn.length; i++){
    // strip first empty char
    if (strTemp.substring(0, 1) == ' '){
      strTemp = strTemp.substring(1, strTemp.length);
    } else {
      break;
    }
  }
  return strTemp;
}

//---------------------------
// Replace ampersand with entitiy replacement (non regular expressions)
//---------------------------
function replaceAmpEntity(strIn){
  var AMPREPL = "&amp;"
  var strTemp = new String(strIn);
  var strOut = new String();
  
  // check if replacement already made
  if (strTemp.indexOf(AMPREPL) == -1){
  
    // do we need to start replacement
    if (strTemp.indexOf("&") >0 ){
    
      // replace all amps
      while (strTemp.indexOf("&") != -1){
        var pos = strTemp.indexOf("&");
        strOut = strOut + strTemp.substring(0, pos) + "&amp;";
        var len = strTemp.length+1;
        strTemp = strTemp.substring(pos+1, len); 
      }
      
      // add rest of the str 
      strOut = strOut + strTemp;
      
    } else {
      strOut = strTemp;
    }
  } else {
    strOut = strTemp;  
  }
  
  return strOut;
}

//-----------------------------------------------
// checkLimit - check upper lenght limit
//-----------------------------------------------
function checkLimit(object, pass_len){
  var len = object.value;  
  if (len.length > pass_len) { 
    var key = window.event.keyCode;
      
    if (key == 32 || key > 46){ 
      alert('You have reached the maximum number of characters for this field.'); 
      return false;
    } else {
      // control charachters
      return true;
    }
  }
  return true;
}

function checkPhoneNumber(textfield)
//' Function moved from resident/shared/servicerequest.asp
{
	var valid = "yes";
	var phno = textfield.value;
	var validNums = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM ~!@#$%^&*()_+|-=\[]{};:<>,./?";
	var validphno="";
	if (phno=="") {
		//showPhoneInputAlert()
		return false;
	}

	for (var i =0; i <= phno.length -1; i++) 
	{
		if (validNums.indexOf(phno.charAt(i)) != -1) 
		{
			validphno = validphno + phno.charAt(i);
		}
	}
	
	var lenvalidphno = validphno.length;
	switch (lenvalidphno)
	{
		case 7 :
				valid = "no";
				textfield.value=validphno;
				textfield.focus();
				textfield.select();
				//<%' alert("Enter Area Code");		 %>		
				break ;
		case 10 :
				break ;
		case 11 :
				if (validphno.charAt(0)==1)
				{
					textfield.value = validphno.substr(1,10) ;
				}
				else
				{
				 valid = "no" ;
				}
				break;
		default :
				valid = "no";
	}
	
	if (valid == "no") 
	{
		return false;		
	}
	else
		return true;			
}



function checkEmail(textfield)
//Validate e-mail address in a field
{
  re = /^\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*$/;
  return textfield.value.search( re ) != -1 ? true : false;
}


//RimskyS-001012, left-trim function
function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}


//RimskyS-001012, right-trim function
function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}


//RimskyS-001012, trim function
function trim(argvalue) {
  var tmpstr = ltrim(argvalue);

  return rtrim(tmpstr);
}

//</SCRIPT>
