

//-----------------------------------------StripLeadingTrailing Blanks Function------------------------
var blanks = " \t\n\r";  // aka whitespace chars
function stripLeadingTrailingBlanks(s)
  { 
  s = stripLeadingBlanks(s);
  s = stripTrailingBlanks(s);
  return s;
  }
  function stripLeadingBlanks(s)
  { 
  var i = 0;
  while ((i < s.length) && (blanks.indexOf(s.charAt(i)) != -1))
     i++;
  return s.substring(i, s.length);
  }
function stripTrailingBlanks(s)
  { 
  var i = s.length - 1;
  while ((i >= 0) && (blanks.indexOf(s.charAt(i)) != -1))
     i--;
  return s.substring(0, i+1);
  }  
  
function isEmpty(s)
  {
	return ((s == null) || (s.length == 0));
  }
function isBlank(s)
  {
	 var i;
	// Is s empty?
  if (isEmpty(s))
    return true;

  // Search through string's chars one by one until we find first
  // non-blank char, then return false; if we don't, return true
  for (i=0; i<s.length; i++)
    {   
    // Check that current character isn't blank
    var c = s.charAt(i);
    if (blanks.indexOf(c) == -1) 
      return false;
    }
  // All characters are blank
  return true;
}

function isEmail(s)
  { 
  if (isBlank(s)) 
    return false;
  
  // there must be >= 1 character before @, so we start
  // start looking at character position 1 (i.e. second character)
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@"))
    i++

  if ((i >= sLength) || (s.charAt(i) != "@")) 
    return false;
  else 
    i += 2;

  // look for .
  while ((i < sLength) && (s.charAt(i) != "."))
    i++

  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
    return false;
  else 
    return true;
  }	
	
//-----------------------------------------------------------------------end-----------------------------
//---------------------------------------Phone Function------------------------------------------------------------------
function FormatPhone(t)
{
	var a =t.value;
	var lenIP =a.length;
	switch (lenIP)
	{
		case 1 :
				a = "(" + a;
				t.value =a;
				break;
				
		case 4 :
				a =a + ") ";
				t.value =a;
				break;
		case 9 :
				a =a + "-";
				t.value =a;
				break;
	}
}
			
//--------------------------------------------------------------------------------end of phone function-----------------------
//-------------------------------------------------Email Validation--------------------------------
function isEmail(s)
  { 
  if (isBlank(s)) 
    return false;
  
  // there must be >= 1 character before @, so we start
  // start looking at character position 1 (i.e. second character)
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@"))
    i++

  if ((i >= sLength) || (s.charAt(i) != "@")) 
    return false;
  else 
    i += 2;

  // look for .
  while ((i < sLength) && (s.charAt(i) != "."))
    i++

  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
    return false;
  else 
    return true;
  }	
	
//----------------------------------------------------------end of Email--------------------------------------------------------------------	

//-------------------------------------------------Form Input Validations-----------------------------------------------
function Ltrim(strText) 
				{ 
				   // this will get rid of leading spaces 
					while (strText.substring(0,1) == ' ') 
						strText = strText.substring(1, strText.length);
					return strText;
				} 
function validate()
	{
		
			if (document.PostListings.Title1.value =='')
						{
							alert("Please enter the Title ");
							eval("document.PostListings.Title1.focus()")
							return false;
						}
		
				for (j=1;j<6;j++)
				{ 			
					//alert(eval("document.PostListings.ISBN" + j + ".value.length"));			
					//alert(eval("document.PostListings.Title" + j + ".value"));
					//if (eval("document.PostListings.Title" + j + ".value != ''"))
					if (eval("document.PostListings.Title" + j + ".value != ''") || eval("document.PostListings.ISBN" + j + ".value != ''") || eval("document.PostListings.Edition" + j + ".value != ''") || eval("document.PostListings.Author" + j + ".value != ''") || eval("document.PostListings.AskingPrice" + j + ".value != ''") )
					{
						
						var str=eval("document.PostListings.Title" + j + ".value");						
						str = str.toUpperCase();
						//strrep = str.replace(" ","");
						if ((str.toUpperCase())=="THE" || (str.toUpperCase())=="A")
						{
							alert("Please omit leading ''A'' or ''The''");
							eval("document.PostListings.Title" + j + ".focus()");
							return false;						
						}						
						str = Ltrim(str);						
						str= " " + str
						//alert((str.indexOf(" THE ",0)));
						if ((str.indexOf(" THE ",0)==0)||(str.indexOf(" A ",0)==0))
						{
							alert("Please omit leading ''A'' or ''The''");
							eval("document.PostListings.Title" + j + ".focus()");
							return false;						
						}
						//if ((str.indexOf("THE ",0)!= -1)||(str.indexOf("A ",0)!= -1) || (str.indexOf(" THE ",0)!= -1)||(str.indexOf(" A ",0)!= -1))
						//alert(str.indexOf(" THE ",0));
						//if ((str.indexOf(" THE ",0)!= -1)||(str.indexOf(" A ",0)!= -1))
						//{
						//	alert("Please omit leading ''A'' or ''The''");
						//	eval("document.PostListings.Title" + j + ".focus()");
						//	return false;
						//}						
						
						if (eval("document.PostListings.Title" + j + ".value =='' "))
						{
							alert("Please enter the Title ");
							eval("document.PostListings.Title" + j + ".focus()")
							return false;
						}
						if (eval("document.PostListings.ISBN" + j + ".value =='' "))
						{
							alert("Please enter the ISBN ");
							eval("document.PostListings.ISBN" + j + ".focus()")
							return false;
						}
						//alert(eval("document.PostListings.ISBN" + j + ".value.length"));
						//alert(eval("document.PostListings.ISBN" + j + ".value"));
						
						if (eval("document.PostListings.ISBN" + j + ".value.length > 1 && document.PostListings.ISBN" + j + ".value.length < 10 "))
						{
							
							alert("Please enter a valid ISBN ");
							eval("document.PostListings.ISBN" + j + ".focus()")
							return false;
						}
						if (eval("document.PostListings.ISBN" + j + ".value.length == 1")) 
						{
							if (eval("document.PostListings.ISBN" + j + ".value != 0"))
							{
								//alert(eval("document.PostListings.ISBN" + j + ".value != 0"));
								alert("Please enter a valid ISBN ");
								eval("document.PostListings.ISBN" + j + ".focus()")
								return false;
							}
						}
						if (eval("document.PostListings.Edition" + j + ".value =='' "))
						{
							alert("Please select the Edition ");
							eval("document.PostListings.Edition" + j + ".focus()")
							return false;
						}
						if (eval("document.PostListings.Author" + j + ".value =='' "))
						{
							alert("Please enter the Author's Name");
							eval("document.PostListings.Author" + j + ".focus()")
							return false;
						}
						if (eval("document.PostListings.AskingPrice" + j + ".value =='' "))
						{
							alert("Please enter the Asking Price ");
							eval("document.PostListings.AskingPrice" + j + ".focus()")
							return false;
						}
						if (eval("isNaN(document.PostListings.AskingPrice" + j + ".value) "))
						{
							alert("Please enter an integer value for Asking Price ");
							eval("document.PostListings.AskingPrice" + j + ".focus()")
							return false;
						}
					
						/*if (eval("document.PostListings.CourseNumber" + j + ".value != ''"))
							{
								if (eval("isNaN(document.PostListings.CourseNumber" + j + ".value) "))
									{
										alert("Please enter an integer value for Course Number ");
										eval("document.PostListings.CourseNumber" + j + ".focus()")
										return false;
									}
								
							}	*/
						
						
						
					}						
				}

				
				if (document.PostListings.firstname.value==0)
					{
						alert("Please enter your first name");
						document.PostListings.firstname.focus();
						return false;
					}
				/*

				if (document.PostListings.lastname.value=="")
					{
						alert("Please enter your last name");
						document.PostListings.lastname.focus();
						return false;
					}
					if (document.PostListings.zipcode.value=="")
					{
						alert("Please enter your zip");
						document.PostListings.zipcode.focus();
						return false;
					}
				if (((isNaN(document.PostListings.zipcode.value) && document.PostListings.zipcode.value!='')) || (document.PostListings.zipcode.value< 0))
						{
							alert("Please enter a valid Zip Code");
							document.PostListings.zipcode.focus();
							return false;
						}	
				*/
				if (document.PostListings.Email.value=="")
				{
					alert("Please enter your Email Address");
					document.PostListings.Email.focus();
					return false;
				}
					emailval = stripLeadingTrailingBlanks(document.PostListings.Email.value);
					if(!isEmail(emailval))
					{
						alert("Please enter a valid email address: ?????@?????.???");
						document.PostListings.Email.focus();
						 return false;
					}	
	
				
		}		
				
		
		
		
		
		
	

	function replace(string,text,by) 
{
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) 	return string;
		
    var i = string.indexOf(text);
    if ((!i)&&(text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
  
    return newstr;
}
	