
/***
 ***  Javascript file for the shopping cart Version 2.0
 ***  Shopping cart for mvcgen.clients.COM
 ***/

/***********************************
	Javascript validation for most of forms used by the shopping cart in the front END
	***********************************/
function IsFutureDate( str1 )
     {
          // Parse the string in DD/MM/YYYY format
          re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/
          var arr = re.exec( str1 );
          var dt1 = new Date( parseInt(arr[3]), parseInt(arr[2], 10) - 1, parseInt(arr[1], 10) );
     
          return dt1 > new Date();
     }

	 
function info_validate(form)
{
	if (form.name)
	{
		if (form.name.value == "")
		{
			alert("Please enter your name");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.name.value)))
			{
				alert("Name can't contain numbers");   
				return false;
			}	
		}	
	}
	//***********check email **********
	if (form.email)
	{
		var email = form.email.value;
		var emailFilter=/^.+@.+\..{2,3}$/;
		
		if (email == "")
		{
			alert("You have to specify an email account");   
			return false;
		} 
		else 
		{
			if(!(emailFilter.test(email)))
			{
				alert("Your email account is invalid ");   
				return false;
			}	
		}	
	}
	
	//***********check  first name **********
	if (form.fname)
	{
		if (form.fname.value == "")
		{
			alert("Please enter first name");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.fname.value)))
			{
				alert("First name can't contain numbers");   
				return false;
			}	
		}	
	}
	
	//***********check  last name **********
	if (form.lname)
	{
		if (form.lname.value == "")
		{
			alert("Please enter last name");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.lname.value)))
			{
				alert("Last name can't contain numbers");  
				return false;
			}	
		}		
	}

	//*********** Check the radio button "login". If it is TRUE should specify a password else can go empty  *************
	if (form.login && form.password)
	{
		if (form.login[0].checked && form.password.value == "")
		{
			alert("You have to specify a password");
			return false;
		}
	} 
	else 
	{
		/***********
			check password for the REGISTER MODULE.
			Can not be empty and have to be at least 5 characters.
			Both password fields have to have the same information.
		 */
		if (form.password)
		{
			if (form.password.value == "")
			{
				alert("The password field is empty");   
				return false;
			}
			else
			{
				if (form.password.value.length < 5)
				{
					alert("The password field must be at least five characters long");   
					return false;
				}
				else
				{
					if (form.password2)
					{
						if (form.password2.value == "")
						{
							alert("Confirm password field is empty");   
							return false;
						}
						else
						{
							if (form.password.value != form.password2.value)
							{
								alert("The content of password fields are diferent");   
								return false;
							}
						}
					}
				}
			}
		}		
	}

	/***********
		check password for the LOGIN MODULE.
		Can not be empty and have to be at least 5 characters.
		
		This validation seems do not being used any long. check and removed if need it.
	 */
	if (form.login_password)
	{
		if (form.login_password.value == "")
		{
			alert("The password field is empty");   
			return false;
		}
		else
		{
			if (form.login_password.value.length < 5)
			{
				alert("The password field must be at least five characters long");   
				return false;
			}
		}
	}
	
	/************
		check password when updating personal information.
		Checks that is the same value in both fields AND are not empty
	 */
	if (form.new_password_1)
	{
		if (form.new_password_1.value != "")
		{
			if (form.new_password_1.value.length < 5)
			{
				alert("The new password field must be at least five characters long");   
				return false;
			}
			else
			{
				if (form.new_password_2.value == "")
				{
					alert("Confirm new password field is empty");   
					return false;
				}
				else
				{
					if (form.new_password_1.value != form.new_password_2.value)
					{
						alert("The content of new password fields are diferent");   
						return false;
					}
					else
					{
						if (form.old_password.value == "")
						{
							alert("You have to give the old password field");   
							return false;
						}
					}
				}
			}
		}
	}

	//***********check  phone number **********
	if (form.phone)
	{
		var phone = form.phone.value.replace(/[\(\)\.\-\ ]/g, '');
		if (phone == "")
		{
			alert("Please enter phone number");   
			return false;
		} 
		else 
		{
			if(isNaN(phone))
			{
				alert("Phone number is invalid");   
				return false;
			}	
		}    
	}
	
	//***********check  address **********
	if (form.address[0])
	{
		if (form.address[0].value == "")
		{
			alert("Please enter Address");   
			return false;
		}	
	}
	
	//***********check  city **********
	if (form.city)
	{
		if (form.city.value == "")
		{
			alert("Please enter city");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.city.value)))
			{
				alert("City can't contain numbers");   
				return false;
			}	
		}	
	}
	
	//*********** check  zip code ********* 
	/**
	
		That was commented out to have conflict with the validation for internationalization 				HA Jul 07, 2006
		
	if (form.zip)
	{
		if (form.zip.value == "") {
			alert("Please enter zip code");   
			return false;
		} 
		else 
		{
			if(isNaN(form.zip.value))
			{
				alert("Zip code can't contain letters");   
				return false;
			}	
		}		
	}
	*/
	
	//***********check  CREDIT CARD INFORMATION name on card
	if (form.name_on_card)
	{
		if (form.name_on_card.value == "")
		{
			alert("Please enter name on credit card");   
			return false;
		}
		else 
		{
			if(!(isNaN(form.name_on_card.value)))
			{
				alert("Name on credit card can't contain numbers");   
				return false;
			}	
		}	
	}
	
	//***********check  card type
	if (form.card_type)
	{
		if (form.card_type.value == "") 
		{
			alert("Please pick credit card");   
			return false;
		}
	}
	
	//***********check  card number
	if (form.card_number)
	{
		if (form.card_number.value == "")
		{
			alert("Please enter card number");   
			return false;
		} 
		else 
		{
			if(isNaN(form.card_number.value))
			{
				alert("Card Number can't contain letters");   
				return false;
			} 
			else 
			{
				if((form.card_number.value.length < 15)||(form.card_number.value.length > 16)) 
				{
					alert("Card number must between 15 and 16 numbers long");   
					return false;
				}
			}	
		}	
	}
	
	
	//***********check  exp month
	if (form.exp_month)
	{
		if (form.exp_month.value == "") 
		{
			alert("Please pick card expiration month");   
			return false;
		}	
	}
	
	//***********check  exp month
	if (form.exp_year)
	{
		if (form.exp_year.value == "")
		{
			alert("Please pick card expiration year");   
			return false;
		}
		else{
			//***********check  valid date
			uDate = '31/' + form.exp_month.value + '/' + form.exp_year.value;
			//alert(uDate);
			if(!IsFutureDate(uDate)){
				alert("Expiration date is incorrect");   
				return false;
			}
		}
	}
	
	
	/*if (form.id_lookup_state.value == "")
	{
		alert("Please choose state.");
		return false;
	}*/
	
	//***********check  zip code **********
	if (form.zip.value == "" || isNaN(form.zip.value))
	{
			if (form.id_lookup_country)
		{
			countryValue = form.id_lookup_country.value.substr(0, 2);
			if (countryValue == "US")
		{
			alert("Please enter Zip/Postal code or check that it does not have letters");   
			return false;	
		}
		}
	}
	/** Function to validate country, state and zip code with the new feature of internationalization of the shopping cart.
		for now will be just be used in the address book module. The one for the shipping options is in validate_ship.cfm 
		because use coldfusion variables.									HA, JUL 07, 2004.									
*/
	if (form.id_lookup_country)
	{
		countryValue = form.id_lookup_country.value.substr(0, 2);

		if (countryValue == "US")
		{
			if (form.id_lookup_state.value == "")
			{
				alert("If your shipping country is US, you have to choose an US state.");
				return false;
			}
			else
			{
				if (form.state.value != "")
				{
					alert("Your shipping country is US, and you already chosen an US state. Please remove the name of the State you typed.");
					return false;
				} 
			}
			//***********check  zip code **********
			if (form.zip.value == "" || isNaN(form.zip.value))
			{
				alert("Please enter Zip/Postal code or check that it does not have letters");   
				return false;	
			}
		}
		else
		{
			if (form.state.value == "")
			{
				alert("Your shipping country is other than US, please enter the province you want to ship to.");
				return false;
			}
			if (form.id_lookup_state.value != "")
			{	
				alert("Your shipping country is not US, so you can not choose an US state");
				return false;
			}	
			//***********check  zip code **********
			if (form.zip.value == "")
			{
				alert("Please enter Zip/Postal code");   
				return false;	
			}	
		}
	}/**/
	
}

/************    New validation for address book   		HA, JUL 07, 2004               *********   */
function validate_addressBook(form)
{
	
	/***********  NickName validation    *******/
	if (form.nickname)
	{
		if (form.nickname.value == "")
		{
			alert("Please enter the Nickname for that address");
			return false;
		}
	}

	//***********check  first name **********
	if (form.fname)
	{
		if (form.fname.value == "")
		{
			alert("Please enter first name");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.fname.value)))
			{
				alert("First name can't contain numbers");   
				return false;
			}	
		}	
	}
	
	//***********check  last name **********
	if (form.lname)
	{
		if (form.lname.value == "")
		{
			alert("Please enter last name");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.lname.value)))
			{
				alert("Last name can't contain numbers");  
				return false;
			}	
		}		
	}

	//***********check  address **********
	if (form.address)
	{
		if (form.address.value == "")
		{
			alert("Please enter Address");   
			return false;
		}	
	}
	
	//***********check  city **********
	if (form.city)
	{
		if (form.city.value == "")
		{
			alert("Please enter city");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.city.value)))
			{
				alert("City can't contain numbers");   
				return false;
			}	
		}	
	}

	/** Function to validate country, state and zip code with the new feature of internationalization of the shopping cart.
		for now will be just be used in the address book module. The one for the shipping options is in validate_ship.cfm 
		because use coldfusion variables.									HA, JUL 07, 2004.								*/	
	if (form.id_lookup_country)
	{
		countryValue = form.id_lookup_country.value.substr(0, 2);

		if (countryValue == "US")
		{
			if (form.id_lookup_state.value == "")
			{
				alert("If your shipping country is US, you have to choose an US state.");
				return false;
			}
			else
			{
				if (form.state.value != "")
				{
					alert("Your shipping country is US, and you already chosen an US state. Please remove the name of the State you typed.");
					return false;
				} 
			}
			//***********check  zip code **********
			if (form.zip.value == "" || isNaN(form.zip.value))
			{
				alert("Please enter Zip/Postal code or check that it does not have letters");   
				return false;	
			}
		}
		else
		{
			if (form.state.value == "")
			{
				alert("Your shipping country is other than US, please type the name of the State you want to ship.");
				return false;
			}
			if (form.id_lookup_state.value != "")
			{	
				alert("Your shipping country is not US, so you can not choose an US state");
				return false;
			}	
			//***********check  zip code **********
			if (form.zip.value == "")
			{
				alert("Please enter Zip/Postal code");   
				return false;	
			}	
		}
	}
	
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

/** Function to choose US country when a US state is chosen 			HA Jun 29, 2004  */
function choose_country(form)
{
	if (form.id_lookup_state.value != "")
	{
		form.id_lookup_country.value = "US";
		form.state.value = "";
	}
	else 
	{
		form.id_lookup_country.value = "";
	}
}

