/*
Author	:Harman
Date	:10/21/2004
Purpose	:javascript functions for data validation and more
*/
//////////////////////////////////////////////////////////////
//Purpose	:confirm delete
//output	:true or false
/////////////////////////////////////////////////////////////
function ConfDelete(){
	var msg = "Are you sure you want to delete this record"; 
	
	if(confirm(msg)){
		return true;
	}else{
		return false;
	}
}//end

//////////////////////////////////////////////////////////////	
//Purpose	:redirect URL
//input		:page name
//output	:none
//////////////////////////////////////////////////////////////	
function GoTo(url){
	window.location.href = url;
}//end

//////////////////////////////////////////////////////////////
//Purpose	:find if the string is an integer
//input		:string
//output	:true or false
/////////////////////////////////////////////////////////////
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}//end


//////////////////////////////////////////////////////////////
//Purpose	:check for empty fields
//input		:field name
//output	:true or false
/////////////////////////////////////////////////////////////
function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else{
	   return false; 
   }
}

/////////////////////////////////////////////////////////////////////////////
//Purpose	:function to validate email address
//input		:email
//output	:true or false
//////////////////////////////////////////////////////////////////////////////
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    }
	
    return true;
}
//sub function for email cvalidation
function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
//////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
//Purpose	:format phone numbers
//input		:field name
//output	:none
///////////////////////////////////////////////////////////////////////////
function formatPhone(string){
	var raw_num = "";
	var len = 0;
	var strip_num="";
	var long_dist = "";
	var area_code="";
	var next3 ="";
	var last4 = "";
	var fmt_num="";
				  
	raw_num = string.value;
	if(raw_num != "")
	{
		strip_num = raw_num.replace(/[-\(\)\s]/g, '');
		
		if(isInteger(strip_num))
		{
			len = strip_num.length;
			if(len == 10)
			{
				area_code = strip_num.substring(0, 3);
				next3 = strip_num.substring(3, 6);
				last4 = strip_num.substring(6, 10);
				fmt_num = area_code+"-"+next3+"-"+last4;
				//alert(fmt_num);
			}
			else if(len == 11)//1-800 num
			{
				long_dist = strip_num.substring(0,1);
				if(long_dist == '1')
				{
					area_code = strip_num.substring(1, 4);
					next3 = strip_num.substring(4, 7);
					last4 = strip_num.substring(7, 11);
					fmt_num = long_dist+"-"+area_code+"-"+next3+"-"+last4;
				}
				else
				{
					 fmt_num = "";
					 string.focus();
					 alert("Invalid Entry. Number Entered Cannot be less than\n10 digits and more than 11 digits long. All 800 and\nother long distance numbers must start with a 1.");
					
				}
			}
			else
			{
				string.value = fmt_num;
				string.focus();
				alert("Invalid Entry. Number Entered Cannot be less than\n10 digits and more than 11 digits long.");
			}
			string.value = fmt_num;
		}
		else
		{
		  string.value = fmt_num;
		  string.focus();
		  alert("Invalid Entry. Enter numbers only");
		}	
	}else
	   string.value = "";
}//end function formatPhone(fld)

//popup creater
function popUp(URL, width, height, scroll) {
	day = new Date();
	id = day.getTime();
	eval("popup" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars="+ scroll +",location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height="+ height +"');");
}

/*
* Check if at least one of the checkbox is checked
* cbx field takes checkbox object which may be a single
* checkbox of a group witht he same name ex: frm.elements['selected_files[]']
*/
function isCheckboxBlank(cbx){
	var i;
	var tot_checked = 0;
	
	//find if its an array, cos it wont work
	//if there is just one checkbox
	if(isArray(cbx)){
		for(i=0; i<cbx.length; i++){ 
			if(cbx[i].checked) {
				tot_checked++; 
			}//end if
		}//end for
	}else{if(cbx.checked) {tot_checked = 1;}}

	if(tot_checked > 0) return false;
	else return true;
}//end 

/**
* check if the object is an array or not
*/
function isArray(obj){ return(typeof(obj.length) == "undefined") ? false : true;}
/**
* Trim or remove whitespace from the ends of strings
*/
function trim(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g,""); }