
/***
 ***  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 info_validate(form)
{
	
	//***********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 == "")
		if (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)
	{
		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 **********
	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((13 > form.card_number.value.length)||(form.card_number.value.length > 16)) 
				{
					alert("Card number must between 14 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;
		}
	}
}




function cc_validate(form)
{
	
	//***********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  address **********
	if (form.address)

	{
		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 State **********
	if (form.id_lookup_state)
	{
		if (form.id_lookup_state.value == "" && form.state.value == "")
		{
			alert("Please select or enter a State");   
			return false;
		}
	}
	
	
	//***********check  zip code **********
	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;
//			}	
//		}		
	}
	
	if (form.id_lookup_country)
	{
		if (form.id_lookup_country == "")
		{
			alert("Please select a Country");   
			return false;
		}
	}
	
	//****** New javascript with improvements
	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 billing country is US, you have to choose an US state.");
				return false;
			}
			else
			{
				if (form.state.value != "")
				{
					alert("Your billing country is US, and you chosen an US state. Please remove the name of the State you worte down.");
					return false;
				} 
			}
		}
		else
		{
			if (form.state.value == "")
			{
				alert("You are required to type in the name of your billing state/province since your billing country is outside of the US.");
				return false;
			}
			if (form.id_lookup_state.value != "")
			{	
				alert("Your billing country is not US, so you can not choose an US state");
				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((13 > form.card_number.value.length)||(form.card_number.value.length > 16)) 
				{
					alert("Card number must between 14 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;
		}
	}
}



function contact_validate(form)
{

	//***********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;
			}	
		}	
	}
	
	

	if (form.state)
	{
		if (form.state.value == "")
		{
			alert("Please enter a State");   
			return false;
		} 
		else 
		{
			if(!(isNaN(form.state.value)))
			{
				alert("State can't contain numbers");   
				return false;
			}	
		}	
	}
	

	
	//***********check  zip code **********
	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;
//			}	
//		}		
	}
	
	if (form.country)
	{
		if (form.country.value == "")
		{
			alert("Please select a country");   
			return false;
		}
	}
	
	
	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  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;
			}	
		}    
	}
	
}

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 = "";
	}
}


function openWindow(url, w, h, hasControls) 
{
 if(hasControls == false) {
  win = window.open(url, 'win', 'width=' + w + ',height=' + h);
 }
 else {
  win = window.open(url, 'win', 'width=' + w + ',height=' + h + ',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0');
 }
 win.focus();
}

function OpenWindow1(theURL,winName,features) { //v2.0
  NewWindow=window.open(theURL,winName,features);
}

function OpenWindow(theURL,winName,features) { //v2.0
  var winheight = screen.height;
  var winwidth = screen.width;
  
  NewWindow=window.open(theURL,winName,features);
}

function OpenWindowAutoSize(theURL,winName,features) {
  var winheight = screen.height;
  var winwidth = screen.width;
  
  if (winwidth <= 640) {
    var wheight = 350;
    var wwidth = 600;
  }
  else if (winwidth <= 800) {
    var wheight = 500;
    var wwidth = 700;
  }
  else {
    var wheight = 650;
    var wwidth = 900;
  }
  
  NewWindow=window.open(theURL,winName,'height=' + wheight + ',width=' + wwidth + ',' + features);
}  