/***
 ***  Javascript file for the shopping cart Version 2.0
 ***  Administration module
 ***/


/***********************************
	Javascript validation for Shipping module in the administration section
	***********************************/
function info_validate(form)
{
	//***********check  shipping name **********
	if (form.shipping_name)
	{
		if (form.shipping_name.value == "")
		{
			alert("Please enter Shipping name");   
			return false;
		} 
	}
	
	//***********check  shipping cost
	if (form.shipping_cost)
	{
		if (form.shipping_cost.value == "")
		{
			alert("Please enter Shipping cost");   
			return false;
		} 
		else 
		{
			if(isNaN(form.shipping_cost.value))
			{
				alert("Shipping cost can't contain letters");   
				return false;
			} 
		}	
	}
	
	//***********check Minimun threshold
	if (form.threshold)
	{
		if(isNaN(form.threshold.value))
		{
			alert("Minimun threshold can't contain letters");   
			return false;
		} 
	}
	//***********check  MAximum threshold
	if (form.maxthreshold)
	{
		if(isNaN(form.maxthreshold.value))
		{
			alert("Maximum threshold can't contain letters");   
			return false;
		} 
		if (parseFloat(form.maxthreshold.value) <	parseFloat(form.threshold.value))
		{
			alert("The value of Maximum threshold can't be lower or equal to Minimum threshold");   
			return false;
		}
	}	
	//**************  Add the validation for list_countries, at least one has to be check
	if (form.list_countries)
	{
		if (form.list_countries.value == "")
		{
			alert("At least one country has to be check.");
			return false;
		}
	}
}

/***********************************
	Javascript validation for Shipping module in the administration section
	***********************************/
function handling_validate(form)
{
	//***********check  shipping name **********
	if (form.handling_type)
	{
		if (form.handling_type.value == "")
		{
			alert("Please enter Handling name");   
			return false;
		} 
	}
	
	//***********check  shipping cost
	if (form.handling_cost)
	{
		if (form.handling_cost.value == "")
		{
			alert("Please enter Handling cost");   
			return false;
		} 
		else 
		{
			if(isNaN(form.handling_cost.value))
			{
				alert("Handling cost can't contain letters");   
				return false;
			} 
		}	
	}
	
	//***********check  shipping cost
	if (form.threshold)
	{
		if (form.threshold.value == "")
		{
			alert("Please enter Threshold");   
			return false;
		} 
		else 
		{
			if(isNaN(form.threshold.value))
			{
				alert("Threshold can't contain letters");   
				return false;
			} 
		}	
	}
	
}

/***********************************
	Javascript validation for Items for products in the administration section			HA, 09 Jun 2004.
	Added the validation for USPS														ha, Jun 24, 2004
	***********************************/
function item_validate(form)
{
	//***********check  shipping name **********
	if (form.weight)
	{
		if (form.weight.value == "" || form.weight.value == "0")
		{
			alert("Fedex and/or USPS shipping are available, so items have to have a weight value.");   
			return false;
		} 
	}

}

/*****************************
	Validate the add option in affiliate module 					HA, JUL 13, 2004
	***********************************/
function val_Commission(form)
{
	//***********check  shipping cost
	if (form.commission)
	{
		if (form.commission.value == "")
		{
			alert("Please enter commission value percentage");   
			return false;
		} 
		else 
		{
			if(isNaN(form.commission.value))
			{
				alert("Commission value percentage can't contain letters or simbols");   
				return false;
			} 
		}	
	}
}
	

/*****************************
	login validation 					Quan Tran 8/4/2004
	***********************************/
function login_check(form)
{
	//***********check email **********
	if (form.email)
	{
		var email = form.email.value;
		var emailFilter=/^.+@.+\..{2,3}$/;
		
		if (email == "")
		{
			alert("Please provide your email address/login id");   
			return false;
		} 
/*		else 
		{
			if(!(emailFilter.test(email)))
			{
				alert("Your email account is invalid ");   
				return false;
			}	
		}
*/
	}


	if (form.password)
	{
		if (form.password.value == "")
		{
			alert("Please provide your password.");   
			return false;
		}
	}
}


//utility useful functions

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;
	removeSpace(formField);
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function removeSpace(obj) {
	var origStr=obj.value;
	//var newStr="";
	while (origStr.substring(0,1)==" ") {
	    var lent=origStr.length;
		if (lent == 0)
			break;
		origStr=origStr.substring(1,lent);
		}	
	lent=origStr.length;	
    if (lent !=0) {
		while (origStr.substring(lent-1,lent)==" ") {
			if (lent == 0)
				break;
			origStr=origStr.substring(0,lent-1);
			var lent=origStr.length;
		}
	}	 
	obj.value=origStr;		
 }
	




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))
		var test=isNaN(formField.value);
		if (isNaN(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 month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	
	return result;
}

function Confirmit(mess) {
		var confirmVar=confirm(mess);
		return confirmVar;		
	}	


function iamhere() {
alert('common.js is linked');
}





