var err;
var errmsg;
var pass;
var passconfirm;
var confirmpasswordcheck;
var email1;
var email2;
var confirmemailcheck;
var strDate, strMon, strDay, strYear;
var ns  = (navigator.appName.indexOf("Netscape") != -1);
var redirect;

function init(){

	err = 0;
	errmsg = "Please complete/correct the following fields                                      \n";
	errmsg += "------------------------------------------------\n";
	
	pass = "";			//will hold the password info.
	passconfirm = "";	//will hold the second password info to confirm.
	//confirmpasswordcheck = true;
	
	email1 = "";
	email2 = "";
	//confirmemailcheck = true;
	
	strDate = "";
	strMon = "";
	strDay = "";
	strYear = "";
	
	redirect = "";
	
}// end function init

function validate(f){

	init();
	
	for(var i=0; i<f.length; i++){
	
		var obj = f.elements[i];
		
		if((obj.type=="text")&&(obj.name != "txt_address2")){
			v_text(obj);
		}
		
		if(obj.type=="password"){
			v_passwords(obj);
		}
		
	//	if(obj.name.substr(0,3)=="sel"){
	//		v_sel(obj);
	//	}
		
	}//end for

	
	if(email1 != ""){
		fCheckEmails();
	}
	
	if(pass != ""){
		fCheckPasswords();
	}
	
	
	if(err > 0){
	
		alert(errmsg);
		return false;
		
	} else {
		err = 0;	
		//last ditch check
		//validDate(strMon,strDay,strYear);
		
		if(err > 0){
			alert(errmsg);
			if(redirect != ""){
				document.location.href = redirect;
			}
			return false;
		}
	}
	
	return true;
	//return false;
	
} //end function validate

// wholesaler validation
function wholesale_validate(f){

	init();
	
	for(var i=0; i<f.length; i++){
	
		var obj = f.elements[i];
		
		if((obj.type=="text")&&(obj.name != "txt_address2")){
			wholesale_v_text(obj);
		}
		
		if(obj.type=="password"){
			v_passwords(obj);
		}
		
		if(obj.name.substr(0,3)=="sel"){
			wholesale_v_sel(obj);
		}
		
	}//end for
	
	if(email1 != ""){
		fCheckEmails();
	}
	
	if(pass != ""){
		fCheckPasswords();
	}
	
	if(err > 0){
	
		alert(errmsg);
		return false;
		
	}
	
	return true;
	
} //end function validate

function v_text(elem){

	var n = elem.name;
	var v = elem.value;
	if (n=="txtScreenName") {
	if (v.length>15) {
		err++;errmsg+="\tScreen Name cannot exceed 15 characters\n";
		}
	}
	if(v == ""){
			
		switch(n){
			case "txtScreenName":err++;errmsg+=" - Screen Name is missing or invalid.\n";break;
			case "txtFirstName":err++;errmsg+=" - First Name is missing or invalid.\n";break;
			case "txtLastName":err++;errmsg+=" - Last Name is missing or invalid.\n";break;
			case "txtEmail":email1=v;err++;errmsg+=" - Email Address is missing or invalid.\n";break;
			case "txtEmail2":email2=v;break;
		//	case "txtAddress":err++;errmsg+="\tAddress\n";break;
		//	case "txtCity":err++;errmsg+="\tCity\n";break;
		//	case "txtZip":err++;errmsg+="\tZip\n";break;
			
		//	case "txtPhone":err++;errmsg+="\tPhone Number\n";break;
		}
		
	}else{
		switch(n){
			case "txtScreenName":CheckScreenName(v);break;
			case "txtEmail":email1=v;break;
			case "txtEmail2":email2=v;break;
			case "txtPhone":CheckPhoneFormat(v);break;
		}
	}
	
}// end function v_text

// wholesale text boxes validation
function wholesale_v_text(elem){

	var n = elem.name;
	var v = elem.value;
	
	if(v == ""){
		switch(n){
			case "txtFirstName":err++;errmsg+="\tFirst Name\n";break;
			case "txtLastName":err++;errmsg+="\tLast Name\n";break;
			case "txtEmail":email1=v;err++;errmsg+="\tEmail Address\n";break;
			case "txtEmail2":email2=v;errmsg+="\tConfirm Email\n";break;
			case "txtStoreName":err++;errmsg+="\tStore Name\n";break;
			case "txtAddress":err++;errmsg+="\tAddress\n";break;
			case "txtCity":err++;errmsg+="\tCity\n";break;
			case "txtZip":err++;errmsg+="\tZip\n";break;
			case "txtTaxId":err++;errmsg+="\tFED Tax ID\n";break;
			case "txtPhone":err++;errmsg+="\tPhone Number\n";break;
			//case "txtCurrentlySelling":err++;errmsg+="\tCurrently Selling Products\n";break;
		}
	}else{
		switch(n){
			case "txtEmail":email1=v;break;
			case "txtEmail2":email2=v;break;
			case "txtTaxId":CheckTaxIdFormat(v);break;
			case "txtPhone":CheckPhoneFormat(v);break;
		}
	}
	
}// end function v_text

function v_passwords(elem){
	var n = elem.name;
	var v = elem.value;
	
	if(v != ""){
		if(n=="txtPassword"){
			pass = v;
		}else if(n=="txtPassword2"){
			passconfirm = v;
		}
	}else{	
		if(n=="txtPassword"){
			err++;
			errmsg+=" - Password is missing or invalid.\n";
		}
	}
}// end function v_passwords

function v_sel(elem){
	var i = elem.selectedIndex;
	var n = elem.name;
	var v = elem.options[i].value;
	
	if(i==0){
		switch(n){
			case "selState":err++;errmsg+="\tState\n";break;
			case "selCountry":err++;errmsg+="\tCountry\n";break;
			case "selDOB_Month":err++;errmsg+="\tDOB Month\n";break;
			case "selDOB_Day":err++;errmsg+="\tDOB Day\n";break;
			case "selDOB_Year":err++;errmsg+="\tDOB Year\n";break;
			case "selEmailType":err++;errmsg+="\tEmail Type\n";break;
			case "selGender":err++;errmsg+="\tGender\n";break;
		}
	}else{
		switch(n){
			case "selDOB_Month":strMon=v;break;
			case "selDOB_Day":strDay=v;break;
			case "selDOB_Year":strYear=v;break;
		}
	}
}// end function v_sel

function wholesale_v_sel(elem){
	
	var i = elem.selectedIndex;
	var n = elem.name;
	var v = elem.options[i].value;
	
	if(i==0){
		switch(n){
			case "selState":err++;errmsg+="\tState\n";break;
			case "selCountry":err++;errmsg+="\tCountry\n";break;
			//case "selBestSeller":err++;errmsg+="\tBest Seller\n";break;
		}
	}
}// end function v_sel

function fCheckEmails(){

	var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)([\-\w]*)(.[\w]{2,}){1,2}$/;
	
	if(emailRegxp.test(email1)){
	
		if(email1 != email2){
			err++;
			errmsg+=" - Email Addresses do not match\n";
		}
		
	}else{
	
		err++;
		errmsg+=" - Email Address is invalid\n";
		
	}
	
}// end function fCheckEmails

function fCheckPasswords(){
	
	var passReg = /\S{5,8}/;
	
	if(passReg.test(pass)){
			if(pass != passconfirm){
				err++;
				errmsg+=" - Passwords do not match\n";
			}
	}else{
		err++;
		errmsg+=" - Password is invalid. Password must be 5-8 characters in length and not include spaces.\n";
	}
		
}// end function fCheckPasswords

function CheckScreenName(val){

	var snReg = /^[a-zA-Z0-9_]{2,15}$/; //any alpha-numeric value occuring a min of 2 times, to a max of 15.
	
	if(snReg.test(val)){
		//screen name is ok
	}else{
		err++;
		errmsg += "\tScreen Name is invalid. Please use only alpa-numeric characters and be 2-15 characters in length.\n";
	}
	
}// end function CheckScreenName

function CheckTaxIdFormat(strVal){
	var snReg = /^[0-9]{2,3}-[0-9]{7}$/; //any numeric value that matches the format XX-XXXXXXX
	
	if(snReg.test(strVal)){
		//tax id format is ok
	}else{
		err++;
		errmsg += "\tFed Tax ID is invalid. \n";
	}
}// end function CheckTaxIdFormat

function CheckPhoneFormat(strVal){
	var reg = /^[\(]*\d{3}[\)-]*\d{3}[-]*\d{4}$/; //telephone number in the format (xxx) xxx-xxxx
	
	if(reg.test(strVal)){
		//format is valid
	}else{
		err++;
		errmsg+="\tTelephone number is invalid. Only enter numbers.\n";
	}
}//end function CheckPhoneFormat

//check date entered and user's age
function validDate(month,day,year){
	//alert("");
	if(((year % 4 == 0)&&(year % 100 != 0)) || ((year % 4 == 0)&&(year % 100 == 0)&&(year % 400==0))){
		//is a leap year
		if(month==2 && day > 29){
			err++;
			errmsg+= "\tInvalid birthdate\n";
		}
	}else{
		//not a leap year
		if(month==2 && day > 28){
			err++;
			errmsg+= "\tInvalid birthdate\n";
		}
	}
	if((month==4 || month==6 || month==9 || month==11) && (day > 30)){
		err++;
		errmsg+= "\tInvalid birthdate\n";
	}
	
	var today = new Date();
	var today_year = today.getYear();
	var today_month = today.getMonth();
	var today_day = today.getDate();

	today_year *= 1;
	
	if(ns){
		today_year+=1900;
	}
	
	today_month += 1;
	//today_day *= 1;
	//year *= 1;
	//month *= 1;
	//day *= 1;

	var now_date = new Date(today_year, today_month-1, today_day);
	var given_date = new Date(year, month-1, day);
	
	if((now_date - given_date) < 567993600000){ //18 and up
		err++;
		errmsg = "We’re sorry. Based on the information you have submitted, you are not eligible to register.\n";
		redirect = "/den/";
	}
	return;
}

function GetMonthName(num,abbr){
	var thisName = "";
	if(abbr=="true"){
		switch(num){
			case 1:thisName="Jan.";break;
			case 2:thisName="Feb.";break;
			case 3:thisName="Mar.";break;
			case 4:thisName="Apr.";break;
			case 5:thisName="May";break;
			case 6:thisName="Jun.";break;
			case 7:thisName="Jul.";break;
			case 8:thisName="Aug.";break;
			case 9:thisName="Sep.";break;
			case 10:thisName="Oct.";break;
			case 11:thisName="Nov.";break;
			case 12:thisName="Dec.";break;
		}
	}else{
		switch(num){
			case 1:thisName="January";break;
			case 2:thisName="February";break;
			case 3:thisName="March";break;
			case 4:thisName="April";break;
			case 5:thisName="May";break;
			case 6:thisName="June";break;
			case 7:thisName="July";break;
			case 8:thisName="August";break;
			case 9:thisName="September";break;
			case 10:thisName="October";break;
			case 11:thisName="November";break;
			case 12:thisName="December";break;
		}
	}
	
	return thisName;
}

function callDefaultMsg(newMsg){

	errmsg = "\tCould not submit your information\n";
	errmsg += "------------------------------------------------\n";
	errmsg += newMsg;
	
	alert(errmsg);

}// end function callDefaultMsg

function ShowAlertAndRedirect(msg, _href)
{
	alert(msg);
	document.location.href = _href;
}
