if (document.URL.lastIndexOf("?") < 0) {
  var thisPage = document.URL.slice(document.URL.lastIndexOf("/")+1);
} else {
  var thisPage = document.URL.slice(document.URL.lastIndexOf("/")+1,document.URL.lastIndexOf("?"));
}

function sortSubmit(sParm) {
  document.forms[0].sortBy.value = sParm;
  document.forms[0].action = thisPage;
  document.forms[0].submit();
}

function filtSubmit(sParm) {
  document.forms[0].filtBy.value = sParm;
  document.forms[0].action = thisPage;
  document.forms[0].submit();
}

function aedSubmit(cmd,tgt) {
//alert("aed["+cmd+"]["+tgt+"]\nThis is disabled, for now.")

  document.forms[0].aedCmd.value = cmd;
  document.forms[0].aedTgt.value = tgt;
  document.forms[0].action = thisPage;
  document.forms[0].submit();
  //window.location.href = thisPage + '?aedCmd=' + cmd + '&aedTgt=' + tgt;
}

function aedSubmit2(cmd,tgt) {
//alert("aed["+cmd+"]["+tgt+"]\nThis is disabled, for now.")

  document.forms[0].aedCmd.value = cmd;
  document.forms[0].aedTgt.value = tgt;
  document.forms[0].action = 'incentives.php';
  document.forms[0].submit();
  //window.location.href = thisPage + '?aedCmd=' + cmd + '&aedTgt=' + tgt;
}

function aedSubmit3(cmd,tgt) {
//alert("aed["+cmd+"]["+tgt+"]\nThis is disabled, for now.")

  document.forms[0].aedCmd.value = cmd;
  document.forms[0].aedTgt.value = tgt;
  document.forms[0].action = 'product_lines_info.php?id='+tgt;
  document.forms[0].submit();
  //window.location.href = thisPage + '?aedCmd=' + cmd + '&aedTgt=' + tgt;
}

function aedSubmit4(cmd,tgt) {
//alert("aed["+cmd+"]["+tgt+"]\nThis is disabled, for now.")

  document.forms[0].aedCmd.value = cmd;
  document.forms[0].aedTgt.value = tgt;
  document.forms[0].action = 'personnel_info.php?id='+tgt;
  document.forms[0].submit();
  //window.location.href = thisPage + '?aedCmd=' + cmd + '&aedTgt=' + tgt;
}

function aedSubmit5(cmd,tgt) {
//alert("aed["+cmd+"]["+tgt+"]\nThis is disabled, for now.")

  document.forms[0].aedCmd.value = cmd;
  document.forms[0].aedTgt.value = tgt;
  document.forms[0].action = 'member_maint.php';
  document.forms[0].submit();
  //window.location.href = thisPage + '?aedCmd=' + cmd + '&aedTgt=' + tgt;
}

function aedSubmit6(cmd,tgt) {
//alert("aed["+cmd+"]["+tgt+"]\nThis is disabled, for now.")

  document.forms[1].aedCmd.value = cmd;
  document.forms[1].aedTgt.value = tgt;
  document.forms[1].action = 'incentives.php';
  document.forms[1].submit();
  //window.location.href = thisPage + '?aedCmd=' + cmd + '&aedTgt=' + tgt;
}

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];}
}

//-----------------------------------------------------------------------------------------------------------------------
function validateForm(form) {

var i=checkFormRequiredFields(form);
var tmp;
	if (i){
		 switch (i.name) {
			case "un":
				tmp = "User Name";
				break;
			case "pw":
				tmp = "Password";
				break;
			case "fn":
				tmp = "First Name";
				break;
			case "ln":
				tmp = "Last Name";
				break;
			case "uo":
				tmp = "Organization";
				break;
			case "ua":
				tmp = "Address Line 1";
				break;
			case "uc":
				tmp = "City";
				break;				
			case "us":
				tmp = "State";
				break;				
	
			}// End switch
			alert(tmp + " is required.");
			i.focus();
			return false;}// end if
			
var strMsg = "";

//strMsg = strMsg + validateUSState(document.forms[0].us);
//strMsg = strMsg + validateUSZip(document.forms[0].uz);
strMsg = strMsg + validateUSPhone(document.forms[0].up);
strMsg = strMsg + validateUSPhone(document.forms[0].uf);

if (document.forms[0].em.value != ""){
    strMsg = strMsg + validateEmail(document.forms[0].em);
}
if (document.forms[0].wa.value != ""){
strMsg = strMsg + validateWebAddress(document.forms[0].wa);
}

if (strMsg.length==0) {
return true;
}
else {

alert (strMsg);

return false;
}//end else


}//End validateForm(objForm) {
//-----------------------------------------------------------------------------------------------------------------------

function validateEmail(src) {

     var emailReg ="^[\\w-\.]{1,}\@([\\da-zA-Z-]{1,}\.){1,}[\\da-zA-Z-]{2,3}$";

     var regex = new RegExp(emailReg);
     
	 if (!regex.test(src.value)) {
	 return "\n Invalid e-mail.\n";	 
	 }
	 else{
	 return "";
	 }

     

} // End function validate_email(src)

function validateWebAddress(src) {

    //var webReg ="/^[http|https|ftp]:\/\/([\\da-zA-Z-]{1,}\.){1,}[\\da-zA-Z-]{2,3}$/";
    
    var webReg ="^((http|https|ftp):\/\/)?([\\da-zA-Z-]{1,}\.){1,}[\\da-zA-Z-]{2,3}$";
    
    var regex = new RegExp(webReg);
    
	if (!regex.test(src.value)) {
	return "\n Invalid web address.\n";	
	}
	else{
	return "";
	}



} // End function validateWebAddress(src)

function validateUSZip (src) {

	
	 var regex  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

	 if (!regex.test(src.value)) {
	 
	 return "\n Invalid zip. Please type in format: '99999' or '99999-9999'.\n";
	 }
     else{
	 return "";
	 }

}  // End function validate_uszip(src)

function trim(str)
{
  return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}

function validateUSPhone(src) {

	 
	 var regex  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
	 
	 if (!regex.test(trim(src.value))) {
	 return "\n Invalid number. Please type in format: '(999) 999-9999' or '(999)999-9999'.\n";
		}
     else{
	 return "";
	 }

}  // End function validateUSPhone(src)

//-----------------------------------------------------------------------------------------


function validateInteger(src) {

	 
	 var regex  = /(^-?\d\d*$)/;;
	 
	 if (!regex.test(src.value)) {
	 return "\n Invalid number.\n";
		}
     else{
	 return "";
	 }

}  // End function validateInteger(src)

function checkFormRequiredFields( form1 ) {
	var i, anInput;
	var elements = form1.elements;
	
	for( i=0; i < elements.length; i++ ) {
	
		if ((elements.item(i).name == "un") || (elements.item(i).name == "pw") || (elements.item(i).name == "fn") || (elements.item(i).name == "ln") || (elements.item(i).name == "uo") || (elements.item(i).name == "ua") || (elements.item(i).name == "uc") || (elements.item(i).name == "us")){
			
			if( elements.item(i).value.length == 0 ) {
		
			  return elements.item(i);
			  }
			}
	}
} // End checkFormRequiredFields

function validateDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
  //alert("sa" + strValue);
  //check to see if in correct format
  
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3);//find date separator
    //alert("strSeparator" + strSeparator);
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1]);

    //check if month value and day value agree
    //alert(arrayLookup[arrayDate[0]] );
    //alert("intDay"+intDay);
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
        
    //check for February
    var intYear = parseInt(arrayDate[2]);
    var intMonth = parseInt(arrayDate[0]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days

    //check month values are ok
    if ((arrayDate[0] > 0) &&  (arrayDate[0] < 13))
        return true;
    }
    
    

  }
  return false; //any other values, bad date
}  // End function validateDate(src)



function validateUpdatedon(p){
    var i, src, ret;
    var allValid = true;
    for (i=0; i < p; i++) {
        
        src = eval("document.forms[0]." + "salesupdatedon_" + i + ".value");
        //alert(src);
        ret = validateDate(src);
        if (!ret) {
  //          alert ("You have entered an invalid date in the updated on field.");
    //        allValid = false;
            }
        
        else {
            var strSeparator = src.substring(2,3);//find date separator

            var arrayDate = src.split(strSeparator); //split date into month, day, year
            var intMonth = parseInt(arrayDate[0]);
            var intDay = parseInt(arrayDate[1]);
            var intYear = parseInt(arrayDate[2]);
            
            var givenDate = new Date(intYear, intMonth-1, intDay);
            var givenvalue = Date.parse(src);
            var currDate = new Date();
            var currentvalue = Date.parse(currDate);
            //alert("given"+givenvalue);
            //alert("current"+currentvalue);
            
            if ( givenvalue > currentvalue) {
//               alert ("you cannot enter a future date in the updated on field.");
//               return false;
                }
             }
        }
    if (!allValid)
        return false;
    else
        return true;
}





//-----------------------------------------------------------------------------------------

/*
function validate_name(src)
{
var checkName = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,-"
var nameCheck
var allValid = true
	        nameCheck = src.value
	        for (i = 0;  i < nameCheck.length;  i++)
	        {
                ch = nameCheck.charAt(i);
                        for (j = 0;  j < checkName.length;  j++)
                        if (ch == checkName.charAt(j))
                                break;
                        if (j == checkName.length)
                        {
                                allValid = false;
                                break;
                        }
	        }
	        if (!allValid)
	        {
			 alert ("Invalid Name");
			 src.focus();
             return false;
	        }else
			{
			return true;
			}

}*/

