var postLocation="./pgbar.php";

/* 
 * add any extension that you do no want to upload to the list 
 * below they should be placed with in the /^ and / characters
 * separate each extension by a pipe symbol |
 */
 
var re = /^(\.php)|(\.sh)/;  // disallow shell scripts and php


/**
 * dofilter = true; to enable filtering
 */
var dofilter=true;

/**
 * this method will match each of the filenames with a
 * given list of banned extension. If any one of the
 * extensions match, an alert will be popped up and the
 * upload will not continue;
 */
 
function check_types() {
	if(dofilter==false)
		return true;
	with(document.forms[0])
	{
		/*
		 * with who uses with?
		 * i do, i am an ancient. ok?
		 */
		
		for(i=0 ; i < elements.length ; i++)
		{
			if(elements[i].value.match(re))
			{
				alert('Sorry ' + elements[i].value + ' is not allowed');
				return false;
			}
		}
	}
	return true;
}

function popUP(mypage, myname, w, h, scroll, titlebar)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}


function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}

function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) and optionally,
  a valid country suffix.  Since email has many
  forms this expression only tests for near valid
  address.  Some additional validation may be
  required.
*************************************************/
var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(strValue);
}

function ValidateUpload(frm)
{
	var mailExp  = /^([_&a-zA-Z0-9-]+(\.[_&a-zA-Z0-9-]+)*@[&a-zA-Z0-9-]+\.+[&a-zA-Z0-9-]+)/;
	var errMsg  = "Please correct the following item(s):\n\n";
	var errNum  = 0;

	if(!validateNotEmpty(frm.company.value))
	{
        frm.company.value == '';
		frm.company.focus();
		errMsg += ++errNum + ". Company Name cannot be blank.\n";
	}
	if(!validateNotEmpty(frm.contact.value))
	{
        frm.contact.value == '';
		frm.contact.focus();
		errMsg += ++errNum + ". Contact cannot be blank.\n";
	}
	if(!validateNotEmpty(frm.email.value))
	{
        frm.email.value == '';
		frm.email.focus();
		errMsg += ++errNum + ". Email Address cannot be blank.\n";
	}
	if(!validateEmail(frm.email.value))
	{
		errMsg += ++errNum + ". Email Address Invalid.\n";
		frm.email.focus();
	}	
	if(!validateNotEmpty(frm.phone.value))
	{
        frm.phone.value == '';
		frm.phone.focus();
		errMsg += ++errNum + ". Phone cannot be blank.\n";
	}
	if(!validateNotEmpty(frm.fax.value))
	{
        frm.fax.value == '';
		frm.fax.focus();
		errMsg += ++errNum + ". Fax cannot be blank.\n";
	}
	if(!validateNotEmpty(frm.website.value))
	{
        frm.website.value == '';
		frm.website.focus();
		errMsg += ++errNum + ". Website cannot be blank.\n";
	}
	if(!validateNotEmpty(frm.description.value))
	{
        frm.description.value == '';
		frm.description.focus();
		errMsg += ++errNum + ". Project Description cannot be blank.\n";
	}
	if(errNum != 0 )
	{
		alert(errMsg);
	}
	else
		postIt(frm);
}


function postIt(frm)
{

	baseUrl = postLocation;
	sid = frm.sessionid.value;
	iTotal = escape("-1");
	baseUrl += "?iTotal=" + iTotal;
	baseUrl += "&iRead=0";
	baseUrl += "&iStatus=1";
	baseUrl += "&sessionid=" + sid;
	popUP(baseUrl,"Uploader",460,262,false,false);
	frm.submit();
}
