
function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function currencyFormat(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;

for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';

if (len == 1) fld.value = aux;
if (len == 2) fld.value = aux;

//if (len == 1) fld.value = '0'+ decSep + '0' + aux;
//if (len == 2) fld.value = '0'+ decSep + aux;

if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);

fld.value += decSep + aux.substr(len - 2, len);

}

return false;
}

function SortArticles(TheChkValue)
{
AreThereRecords = document.PW.recordThere.value;

if (AreThereRecords == "1")
	{
	switch(TheChkValue)
		{
		case "Title":
			TheTitles = GetTheValues(document.PW.TheTitles);
			splitTheTitles = TheTitles.split(",")
			TheTemp = splitTheTitles[0]
			splitTheTitles[0] = splitTheTitles[1]
			splitTheTitles[1] = TheTemp
			//document.PW.TheTitles.sort()
			alert(document.PW.TheTitles.sort())
			
			break;
		case "PDate":
			ThePDates = document.PW.PDates.value;
			break;
		case "SeqNum":
			TheSeqNum = document.PW.TheSeqNum.value;
			break;
		}	
	}
}


function GetTheValues(TheObj)
	{
	
	TheValue = ""
	
	for (a=0; a < TheObj.length; a++)
		{
		if (TheValue == "")
			{
			TheValue = TheObj[a].value;
			}
		else
			{
			TheValue = TheValue + "," + TheObj[a].value;
			}
		}
	return TheValue
	
	}

function ChkImageName()
{
FileName1 = document.PW.FILE1.value
FileName2 = document.PW.FILE2.value

FileName1isok = true;
FileName2isok = true;

if (FileName1 != "")
	{
	
	split_Fname1 = FileName1.split("\\")

	FName1_len = split_Fname1.length;
	
	FinalName1 = split_Fname1[FName1_len-1];
	
	if (FinalName1.indexOf("'") != -1 || FinalName1.indexOf("/") != -1 || FinalName1.indexOf("\"") != -1 || FinalName1.indexOf("(") != -1 || FinalName1.indexOf(")") != -1|| FinalName1.indexOf(",") != -1)
		{
		FileName1isok = false;
		}
	else
		{
		FileName1isok = true;
		}
		
	}

	
if (FileName2 != "" && FileName1isok == true)
	{	
	split_Fname2 = FileName2.split("\\")

	FName2_len = split_Fname2.length;
	
	FinalName2 = split_Fname2[FName2_len-1];
	
	if (FinalName2.indexOf("'") != -1 || FinalName2.indexOf("/") != -1 || FinalName2.indexOf("\"") != -1 || FinalName2.indexOf("(") != -1 || FinalName2.indexOf(")") != -1)
		{
		FileName2isok = false;
		}
	else
		{
		FileName2isok = true;
		}
	}

if (FileName2isok == false || FileName1isok == false)
	{
	alert ("Filename cannot contain Special characters like ', \", \\, /, \), and \( . Please rename the file and then upload it.")
	}
else
	{
	document.PW.submit();
	}
	
}

function validate_phone(phone)
{
var count = 0;
var phonenumber = "";
for (k = 0; k < phone.length; k++) {
      if (phone.charAt(k) != " " && phone.charAt(k) != "-") { 
        phonenumber = phonenumber + phone.charAt(k);     
        count++;
      }
   }
   if (count < 10 || count > 10)
     return false;
   else {
     if (isNumber(phonenumber))
       return phonenumber;
     else
       return false;
   } 
} 

function validate_email(checkThisEmail)
{	
var myEMailIsValid = true;
var myAtSymbolAt = checkThisEmail.indexOf('@');
var myLastDotAt = checkThisEmail.lastIndexOf('.');
var mySpaceAt = checkThisEmail.indexOf(' ');
var myLength = checkThisEmail.length;


// at least one @ must be present and not before position 2
// @yellow.com : NOT valid
// x@yellow.com : VALID

if (myAtSymbolAt < 1 ) 
 {myEMailIsValid = false}


// at least one . (dot) afer the @ is required
// x@yellow : NOT valid
// x.y@yellow : NOT valid
// x@yellow.org : VALID

if (myLastDotAt < myAtSymbolAt) 
 {myEMailIsValid = false}

// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
// x.y@yellow. : NOT valid
// x.y@yellow.a : NOT valid
// x.y@yellow.ca : VALID

if (myLength - myLastDotAt <= 2) 
 {myEMailIsValid = false}


// no empty space " " is permitted (one may trim the email)
// x.y@yell ow.com : NOT valid

if (mySpaceAt != -1) 
 {myEMailIsValid = false}


if (myEMailIsValid == true)
 {}
else
 {}


return myEMailIsValid
}


function convertString(thisString)
{
convertedString = parseInt(thisString);
return convertedString;
}

// check to see if we have a number
function isNumber(number) {
  for (i = 0; i < parseInt(number.length); i++)
    if( number.charAt(i) > "9" || number.charAt(i) < "0")
      return false;
  return true;
}
