//
//   Tests on image file extensions,etc
//
//   V.B.McKee Mar 2006
//
//   Copyright © 2006 KeeTech Ltd. All rights reserved.
//

function bImgExtnOK(extn)
{
  // ensure test is done lowercase
	extn = extn.toLowerCase();
  
  // is it recognisable?
  return ((extn == 'jpg') || (extn == 'jpeg') || (extn == 'gif') || (extn == 'png'));
}

function extncheck(fnm)
{
	// Trim the whole filename
	var ext = trim(fnm);

	// Find the last dot
	var dot = ext.lastIndexOf('.');

	// Possibly no dot at all
	if (dot == -1)
	{
		alert('The filename has no type extension !'+'\r\n'+
			'Please select a jpg/jpeg/gif/png file instead.');
		return false;
	}

	// Possibly nothing before dot
	if (dot == 0)
	{
		alert('The filename is too short !'+'\r\n'+
			'Please select a valid filename.');
		return false;
	}

	// Pick off the extension
	ext = ext.substring(dot+1,ext.length);

	// May be none of these types
	if(!bImgExtnOK(ext))
	{
		alert('You selected a .'+ext+' file !'+'\r\n'+
			'Please select a jpg/jpeg/gif/png file instead.');
		return false;
	}

	// Default outcome
	return true;
}

function badcharcheck(fnm)
{
	// Trim the whole filename
	fnm = trim(fnm);

	// Test for known bad characters
	if((fnm.lastIndexOf("'") != -1))
	{
		alert('Filename contains a bad character.'+'\r\n'+
			'Rename it omitting the single quote.');
		return false;
	}
	if((fnm.lastIndexOf('"') != -1))
	{
		alert('Filename contains a bad character.'+'\r\n'+
			'Rename it omitting the double quote.');
		return false;
	}
	if((fnm.search(/[?=+<>;,*|]/) != -1))
	{
		alert('Filename contains a bad character.'+'\r\n'+
			'Rename it omitting any of ?=+<>;,*|');
		return false;
	}

	// Default outcome
	return true;
}

function upldchecks(fnm)
{
	// Aggregate the name checks
	return (extncheck(fnm) && badcharcheck(fnm));
}

function bsidchecks(bsid)
{
  var msg = "";

  // must be non-blank
	if(bsid === "") return false;
  
  // must not be too long
	if(bsid.length > 10)
	{
		alert("The Photo ID you entered seems too long !" + "\r\n" +
		  "Please recheck the photo id on Bigstock's site." + "\r\n" +
			"If the problem persists, please contact us.");
		return false;
  }
  
  // search for non-numerics
	if(bsid.search(/\D/) != -1)
	{
    // Warn user
		alert("The Photo ID you entered contains a non-numeric character." + "\r\n" +
		  "Please recheck the photo id on Bigstock's site." + "\r\n" +
			"If the problem persists, please contact us.");
		return false;
  }
  
  // Parse it to an integer
  bsid = parseInt(bsid);
  
	// May not evaluate to a number
  if(isNaN(bsid))
	{
		alert("The Photo ID you entered is non numeric!" + "\r\n" +
		  "Please recheck the photo id on Bigstock's site." + "\r\n" +
			"If the problem persists, please contact us.");
		return false;
	}

	// May be too small or seem too big
  if((bsid < 1) || (bsid > 10000000))
	{
		alert("The Photo ID you entered seems outside the normal range !" + "\r\n" +
		  "Please recheck the photo id on Bigstock's site." + "\r\n" +
			"If the problem persists, please contact us.");
		return false;
	}

	// Default outcome
	return true;  
}

// These functions included in this file for convenience
function setprogress(thisBar,strWords,bStart)
{
  var wordsTag = document.getElementById(strWords);

  // Do upload checks and set a running progress bar appropriately
  if(bStart)
  {
    // Disable controls
  	document.frmupload.submit.disabled = true;
    document.frmupload.upfile.readonly = true;

    // Set words
    wordsTag.firstChild.nodeValue = "Upload in progress";

    // Restart progress bar
    thisBar.restartBar();
    
    return true;
  }
  else
  {
    // Enable controls
  	document.frmupload.submit.disabled = false;
    document.frmupload.upfile.readonly = false;

    // Set words
    wordsTag.firstChild.nodeValue = "Uploader currently inactive";

    // Reset progress bar
    thisBar.resetBar();
    
    return false;  
  }
}

function failprogress(thisBar,strWords)
{
  var wordsTag = document.getElementById(strWords);

  // Enable controls
	document.frmupload.submit.disabled = false;
  document.frmupload.upfile.readonly = false;

  // Set words
  wordsTag.firstChild.nodeValue = "Uploader timed out";

  // Hide progress bar
  thisBar.resetBar();
  
  return false;  
}


