function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please select or enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function validEmployeeNumber(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value.length > 8)
	{
		alert(fieldLabel + ' contains more than 8 digits.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function validRequiredDropDown(formField,fieldLabel)
{
	var result = true;
	
	var optionValue = formField.options[formField.selectedIndex].value;
	
	//alert('this is the selected value for the "' + fieldLabel +'" field.' + ' ->' + optionValue);
	
	if (optionValue == "none" || optionValue == "")
	{
		alert('Please select or enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
		return result;
	}
		
	return result;
}

function validRequiredDropDownNoMsg(formField,fieldLabel)
{
	var result = true;
	
	var optionValue = formField.options[formField.selectedIndex].value;
	
	//alert('this is the selected value for the "' + fieldLabel +'" field.' + ' ->' + optionValue);
	
	if (optionValue == "none")
	{
		//alert('Please select or enter a value for the "' + fieldLabel +'" field.');
		//formField.focus();
		result = false;
		return result;
	}
	
        if (optionValue == "")
	{
		//alert('Please select or enter a value for the "' + fieldLabel +'" field.');
		//formField.focus();
		result = false;
		return result;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789.,");
}

function removeCommas(s) {
  /*
  ** Remove NewLine, CarriageReturn and Tab characters from a String
  **   s  string to be processed
  ** returns new string
  */
  r = "";
  for (i=0; i < s.length; i++) {
    if (s.charAt(i) != ',') {
      r += s.charAt(i);
      }
    }
  return r;
  }

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter numbers only for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function setFormVariable(formVariableName, formVariableValue) {
    formVariableName.value = formVariableValue;
    return true;
}

function numCheck(argvalue) {

  if (argvalue.length == 0)
    return true;

  for (var n = 0; n < argvalue.length; n++)
    if ((argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9" ) && argvalue != ".")
      return false;

  return true;

}

function MsgOkCancel(argvalue) 
{ 
    var fRet; 
    fRet = confirm(argvalue); 
    return fRet;
} 


function filtery(pattern, list){
  /*
  if the dropdown list passed in hasn't
  already been backed up, we'll do that now
  */
  if (!list.bak){
    /*
    We're going to attach an array to the select object
    where we'll keep a backup of the original dropdown list
    */
    list.bak = new Array();
    for (n=0;n<list.length;n++){
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
    }
  }

  /*
  We're going to iterate through the backed up dropdown
  list. If an item matches, it is added to the list of
  matches. If not, then it is added to the list of non matches.
  */
  match = new Array();
  nomatch = new Array();
  for (n=0;n<list.bak.length;n++){
    if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())!=-1){
      match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }else{
      nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }
  }

  /*
  Now we completely rewrite the dropdown list.
  First we write in the matches, then we write
  in the non matches
  */
  for (n=0;n<match.length;n++){
    list[n].value = match[n][0];
    list[n].text = match[n][1];
  }
  for (n=0;n<nomatch.length;n++){
    list[n+match.length].value = nomatch[n][0];
    list[n+match.length].text = nomatch[n][1];
  }

  /*
  Finally, we make the 1st item selected - this
  makes sure that the matching options are
  immediately apparent
  */
  list.selectedIndex=0;
}

function saveFile(fname) 
{ 
    document.execCommand('SaveAs',null,fname) 
}
            
var isReady = false;

function doSaveAs(){ 
    if (document.execCommand){ 
        
        if (isReady) {
            
            document.execCommand('SaveAs');
        
        }
        
    } else { 
        
        alert('Feature available only in Internet Exlorer 4.0 and later.'); 
    }
}

function OpenWindow_FindSoldTo( x ){
    var winPop = window.open("get_customer_soldto_popup.jsp?param="+x,"winPop");
}

function OpenWindow_FindShipTo( x ){
    var winPop = window.open("get_customer_shipto_popup.jsp?param="+x,"winPop");
}

function OpenWindow_FindReseller( x ){
    var winPop = window.open("get_customer_reseller_popup.jsp?param="+x,"winPop");
}

function OpenWindow_FindChannelPartner( x ){
    var winPop = window.open("get_channel_partner_popup.jsp?param="+x,"winPop");
}

function OpenWindow_FindEndUser( x ){
    var winPop = window.open("get_customer_enduser_popup.jsp?param="+x,"winPop");
}

function OpenWindow_FindSoldToProvince( x ){
    var winPop = window.open("get_customer_soldto_province_popup.jsp?param="+x,"winPop");
}

function OpenWindow_FindEndUserProvince( x ){
    var winPop = window.open("get_customer_enduser_province_popup.jsp?param="+x,"winPop");
}