var noPop = false;
function leave() {
	if (!noPop){
		window.open('http://www.eurodrugspharmacy.com/reductilspecialoffer.htm','','toolbar=no,menubar=no,location=no,height=580,width=600');
	}
}

function setBool(){
	noPop = true;
}

function highlight(el, tag) {
	while (el.nodeName != tag) {
	    if (el.nodeName == "BODY") return;
		el = el.parentNode;
	}
	el.style.borderColor = "#ff0000";
	el.style.borderWidth = "3px";
	el.style.borderStyle = "solid";
	el.scrollIntoView(false);
}

function dehighlight(el, tag) {
    try {
        while (el.nodeName != tag) {
            if (el.nodeName == "BODY") return;
            el = el.parentNode;
        }
        el.style.borderColor = "";
        el.style.borderWidth = "";
        el.style.borderStyle = "";
    } catch (e) {}
}

// if the condition has NOT been met, the tag will be highlighted and check() will return true
function check(condition, el, tag, el2, tag2) {
    if (condition == false) {
	    highlight(el, tag);
		if (el2) highlight(el2, tag2);
	    return true;
	} else {
	    dehighlight(el, tag);
		if (el2) dehighlight(el2, tag2);
		return false;
	}
}

function removeSpaces(string)
{
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
		  {
			IsNumber = false;
		  }
      }
   return IsNumber;   
}

function validCode(zip)
{
	var zip = removeSpaces(zip);
	
	if (!(zip.length == 5 || zip.length == 9 || zip.length == 10))
	{
		return false;
	}
	
	if ((zip.length == 5 || zip.length == 9) && !IsNumeric(zip))
	{
		alert(zip.length);
		return false;
	}
	
	if (zip.length == 10 && zip.search && zip.search(/^\d{5}-\d{4}$/) == -1)
	{
		return false;
	}
		return true;
	
/*	// Check that a Canadian postal code is valid
	if (zip.search)
	{
		zip = removeSpaces(zip);
		if (zip.length == 6 && zip.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1)
		{
			alert('hello');
			return true;
		}
		else if (zip.length == 7 && zip.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	return true;
	*/
}


function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function validate(f) {
	var e = f.elements;
	var product;
	product = check_product(f);
	if(product == true)
	{
		if (check(e["Shipping First Name"].value != "", e["Shipping First Name"], "TD")) {
			alert("First Name is required");
			return false;
		} 
		else if (check(e["Shipping Last Name2"].value != "", e["Shipping Last Name2"], "TD")) {
			alert("Last Name is required");
			return false;
		}
		else if (check(e["mail2"].value != "", e["mail2"], "TD")) {
			alert("Please enter your email address");
			return false;
		}
		else if (check(isValidEmail(e["mail2"].value), e["mail2"], "TD"))
		{   
			alert("Please Enter Valid Email Id");
			return false;
		}
		else if (check(e["from"].value != "", e["from"], "TD")) {
			alert("Please confirm your email address");
			return false;
		}
		else if (check(isValidEmail(e["from"].value), e["from"], "TD"))
		{   
			alert("Please Enter Valid Email Id");
			return false;
		}
		else if (check(e["from"].value == e["mail2"].value, e["from"], "TD"))
		{   
			alert("Please confirm your email address");
			return false;
		}
		else if (check(e["Shipping Country"].value != "", e["Shipping Country"], "TD")) {
			alert("Please enter your Country");
			return false;
		}
		else if (check(e["Shipping Address 12"].value != "", e["Shipping Address 12"], "TD")) {
			alert("Please enter your complete Shipping Address");
			return false;
		}
		else if (check(e["Shipping_Town_City2"].value != "", e["Shipping_Town_City2"], "TD")) {
			alert("Please enter your complete Shipping Address");
			return false;

		}	
		else if (check(e["Shipping Postal Code2"].value != "", e["Shipping Postal Code2"], "TD")) {
			alert("Please enter your complete Shipping Address");
			return false;
		}	
		else if (check(e["Shipping Phone"].value != "", e["Shipping Phone"], "TD")) {
			alert("Please enter your phone number");
			return false;
		}
		else if (check(IsNumeric(e["Shipping Phone"].value), e["Shipping Phone"], "TD")) {
			alert("Please enter your correct phone number");
			return false;
		}
		else if (check(e["Billing Country"].value != "", e["Billing Country"], "TD")) {
			alert("Billing Country is required.");
			return false;
		}
		else if (check(e["Billing Address 12"].value != "", e["Billing Address 12"], "TD")) {
			alert("Billing Address is required.");
			return false;
		}
		else if (check(e["Billing_Town_City2"].value != "", e["Billing_Town_City2"], "TD")) {
			alert("Billing City is required.");
			return false;

		}
		else if (check(e["Billing Postal Code2"].value != "", e["Billing Postal Code2"], "TD")) {
			alert("Billing Zip Code is required.");
			return false;
		}
		//-When "credit card" is selected from the "payment options" section, "card
		//type" "card holder" "expiration" "CVV2 code" "card holder" should all be
		//required, however they should not be required if "money order" is selected
		//as a payment option.
		else if (check(e["Card Holder2"].value != "", e["Card Holder2"], "TD")) {
			alert("Card Holder's name is required.");
			return false;
		}
		else if (check(e["select2"].value != "Please Select", e["select2"], "TD")) {
			alert("Card Type is required.");
			return false;
		}
		else if (check(e["Credit Card Number"].value != "", e["Credit Card Number"], "TD")) {
			alert("Card Number is required.");
			return false;
		}
		else if (check(e["select3"].value != "0", e["select3"], "TD")) {
			alert("Card Expiration Date is required.");
			return false;
		}
		else if (check(e["select5"].value != "-1", e["select5"], "TD")) {
			alert("Card Expiration Date is required.");
			return false;
		}
		else if (check(e["CCV22"].value != "", e["CCV22"], "TD")) {
			alert("CCV2 Code is required.");
			return false;
		}

		setBool();

		return true;
	}
	else
	{
		return false;
	}
}

