/*
Javascipt functions for navigation bar on AuctionStuf.php page.
*/

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("-");
 		
 		result = (elems.length == 3); // should be three components
 		
 		if (result)
 		{
 			var year = parseInt(elems[0],10);
			var month = parseInt(elems[1],10);
  			var day = parseInt(elems[2],10);
 			
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[0].length == 2) || (elems[0].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format YYYY-MM-DD for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

//Login Form Validation
function loginValid(theForm)
{
	if (!validRequired(theForm.login_usr,"User ID"))
		return false;
	
	if (!validRequired(theForm.login_pswd,"Password"))
		return false;
	
	return true;
}

//Register1 Form Validation
function register1Valid(theForm)
{
	if(!validRequired(theForm.register1_usr, "UserName"))
		return false;
	
	if(!validRequired(theForm.register1_pswd, "Password"))
		return false;
	
	if(!validRequired(theForm.register1_re_pswd, "Re-Type Password"))
		return false;
	
	if(!(theForm.register1_usr.value.length > 5 && theForm.register1_usr.value.length < 17)){
		alert('Username should be between 6 and 16 characters');
		theForm.register1_usr.focus();
		return false;
	}
	
	if(!(theForm.register1_pswd.value.length > 5 && theForm.register1_pswd.value.length < 17)){
		alert('Password should be between 6 and 16 characters');
		theForm.register1_pswd.focus();
		return false;
	}
	
	return true;
}

//Register2 Form Validation
function registerValid(theForm)
{
	if(!validRequired(theForm.register_name, "Name"))
		return false;
	
	if (!validEmail(theForm.register_email, "Email",true))
		return false;
	
	if (!validNum(theForm.register_phone,"Phone",true))
		return false;
	
	if(!validRequired(theForm.register_add1, "Address 1"))
		return false;
	
	if (!validNum(theForm.register_ccnumber,"Card Number",true))
		return false;
	
	if(!validRequired(theForm.register_ccname, "Name on Card"))
		return false;
		
	if (!validNum(theForm.register_ccyear,"Year",true))
		return false;
	
	if (!(theForm.register_ccyear.value.length == 4)
		|| !(theForm.register_ccyear.value > 2003)
		|| !(theForm.register_ccyear.value < 2050)){
			alert('Enter a Valid Expiration year (eg. 2006)');
			theForm.register_ccyear.focus();
			return false;
		}
	
	return true;
}

//Bid Form Validation
function bidValid(theForm)
{
	var reg = /^\d+(\.\d\d?)?/ //Verify whether the amount entered is correct
	if(!validRequired(theForm.bid_bidprice, "Your Price"))
		return false;
	var pr = parseFloat(theForm.bid_bidprice.value);
	var minpr = parseFloat(theForm.bid_minprice.value);
	if(!theForm.bid_bidprice.value.match(reg) ||
		pr < 0){
		alert('Enter a correct Bid Amount (eg. 85.50, 120)');
		theForm.bid_bidprice.focus();
		return false;
		}
	if(minpr > pr){
		alert('Bid Amount should be equal or higher than minimum bid price');
		theForm.bid_bidprice.focus();
		return false;
		}
	
	return true;
}

//Administrator User Information Update Validation
function aduserValid(theForm)
{
	if(!validRequired(theForm.aduser_name, "Name"))
		return false;
	
	if (!validEmail(theForm.aduser_email, "Email",true))
		return false;
	
	if (!validNum(theForm.aduser_phone,"Phone",true))
		return false;
	
	if(!validRequired(theForm.aduser_add, "Address"))
		return false;
	
	return true;
}

//Administrator Login Information Form Validation
function ccloginValid(theForm)
{
	if (!validRequired(theForm.cclogin_usr,"User ID"))
		return false;
	
	if (!validRequired(theForm.cclogin_pswd,"Password"))
		return false;
	
	return true;
}

function ccchangeValid(theForm)
{
	
	if (!validNum(theForm.ccchange_no,"Card Number",true))
		return false;
	
	if (!validRequired(theForm.ccchange_name, "Name on Card"))
		return false;
		
	if (!validNum(theForm.ccchange_year,"Expiration Date",true))
		return false;
		
	if (!(theForm.ccchange_year.value.length == 4)
		|| !(theForm.ccchange_year.value > 2003)
		|| !(theForm.ccchange_year.value < 2050)){
			alert('Enter a Valid Expiration Date (eg. 2006)');
			theForm.ccchange_year.focus();
			return false;
		}
	
	if (!validNum(theForm.ccchange_month,"Month",true))
		return false;
		
	if (!(theForm.ccchange_month.value < 13)
		|| !(theForm.ccchange_month.value > 0)
		|| !(theForm.ccchange_month.value.length == 2)){
			alert('Enter a Valid Month (eg. 04, 08 etc.)');
			theForm.ccchange_month.focus();
			return false;
		}
	
	return true;
}


//Function update photograph validation\
function photoupdateValid(theForm)
{
	if (!validRequired(theForm.imgfile,"File Name"))
		return false;
	
	return true;
}

//Item Add/Update Form Validation
function aditemValid(theForm)
{
	var reg = /^\d+(\.\d\d?)?/ //Verify whether the amount entered is correct
	
	if(!validRequired(theForm.aditem_title, "Title"))
		return false;
		
	if(!validRequired(theForm.aditem_stprice, "Starting Price"))
		return false;
		
	var stpr = parseFloat(theForm.bid_stprice.value);
	if(!theForm.aditem_stprice.value.match(reg) ||
		stpr < 0){
		alert('Please enter a legitimate Starting price');
		theForm.aditem_stprice.focus();
		return false;
		}
		
	if(!validRequired(theForm.aditem_incr, "Bid Increment"))
		return false;
		
	if(!theForm.aditem_incr.value.match(reg) ||
		theForm.aditem_incr.value <= 0){
		alert('Please enter a legitimate Bid Increment amount');
		theForm.aditem_incr.focus();
		return false;
		}
	
	if(!validDate(theForm.aditem_stdate, "Start Date", true))
		return false;
	
	if(!validDate(theForm.aditem_date, "End date", true))
		return false;
	
	if(!validRequired(theForm.aditem_descr, "Item Description"))
		return false;
	
	return true;
}
