/************************************************************************
******************** Generic Form Verification Function *****************
************************************************************************/
function verify_form(f)
{
    var msg;
    var empty_fields="";
    var errors="";
    var temp = "";
    var strTemp = "";
	var SomeEdited = true;

	if (f.thisorthat) {
		var thisthat = f.thisorthatfields.split(",");

		SomeEdited = false;
		for(var i = 0; i < thisthat.length; i++) {
		  var e = eval("f." + thisthat[i]);
		  
		  //alert(e.name + " " + e.optional);
			if (((e.type == "text") || (e.type == "textarea") || (e.type == "file"))) {
				if (isblank(e)) {
				  empty_fields += "\n       " + e.id;
				  continue;
				} else {
				  SomeEdited = true;
				  break;
				}
			}
		}
		

	}

    msg  = "__________________________________________________\n\n";
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "__________________________________________________\n\n";

    if (!SomeEdited) {
      msg += "- atleast one of these fields is required:" + empty_fields + "\n";
      alert(msg);
      return false;
    }

	empty_fields = "";

    for(var i = 0; i < f.length; i++) {
      var e = f.elements[i];

      //alert(e.name + " " + e.optional);

	    if (((e.type == "text") || (e.type == "textarea") || (e.type == "file") || (e.type == "password")) && e.optional == false) {
			if (isblank(e)) {
			  empty_fields += "\n       " + e.id;
			  
			  continue;
			}
		}

	   if ((e.type == "text") || (e.type == "textarea") || (e.type == "password")) {
			if (isblank(e)) {
			  strTemp = "";
			} else {
			  strTemp = e.value;
			}
		}	   	
	   
	   
	   if (e.dependents) {
			if (((e.type == "text") || (e.type == "textarea") || (e.type == "file"))) {
				if (!isblank(e)) {
				  var dependents = e.dependentfields.split(",");
		
				  for(var j = 0; j < dependents.length; j++) {
					var d = eval("f." + dependents[j]);
					  
					//alert(e.name + " " + e.optional);
					if (((d.type == "text") || (d.type == "textarea") || (d.type == "file"))) {
						if (isblank(d)) {
						  errors += "\n- The field '" + d.id + "' is required if you entered a value for " + e.id;
						}
					}
				  }
				}
			}
	   }		

	   if(strTemp.length > 0){
			if (e.numeric || (e.min != null) || (e.max != null)) {
			  var v = parseFloat(e.value);
			  if (isNaN(e.value) ||
				  ((e.min != null) && (v < e.min)) ||
				  ((e.max != null) && (v > e.max))) {
			
				   errors += "\n- The field '" + e.id + "' must be a number";
				   e.value = ""
			
				   if (e.min != null)
					  errors += " that is greater than " + e.min;
				   if (e.max != null && e.min != null)
					  errors += " and less than " + e.max;
				   else if (e.max != null)
					  errors += " that is less than " + e.max;
			
				   errors += "\n";
			  }
			}	  
	   }

	   if(strTemp.length > 0){
			if (e.phone) {
			  var objRegExp  = /^[1-9]\d{2}\-\d{3}\-\d{4}$/;
		  
			  //check for valid us phone format xxx-xxx-xxxx
			  if (!objRegExp.test(e.value)) {
				errors += "\n- The field '" + e.id + "' must be a phone number in the format xxx-xxx-xxxx";
			  }
		   }
	   }
		
 	   if(strTemp.length > 0){
			if (e.email) {
			  var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/;
			  
			  //check for valid email
			  if (!objRegExp.test(e.value)) {
				errors += "\n- The field '" + e.id + "' is not valid format ";
			  }
		   }
	   }		
    }

    if (empty_fields) {
      msg += "- the following required field(s) are empty:" + empty_fields + "\n";
      alert(msg);
      return false;
    }

    if (errors) {
      msg += errors + "\n";
      alert(msg);
      return false;
    }

   /*for(var i = 0; i < f.length; i++) {
       e = f.elements[i];

		if ((e.type == "text") || (e.type == "textarea"))
		   {e.value = CleanApostrophe(e.value);}
   }*/
}

/************************************************************************
************************* Blank Field Check *****************************
************************************************************************/
function isblank(s)
{
	if ((s.value == null) || (trim(s.value) == ""))
		return true;
	else 
		return false;
}


/* Check if date is a valid date */
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

/* Check Field is Integer */
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

/* Check if chars in string s are in bag (array of chars) */
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function isCurrency(f)

{
	var nNum = 0;			// Total numbers for currency value.
	var nDollarSign = 0;	// Total times a dollar sign occurs.
	var nDecimal = 0;		// Total times a decimal point occurs.
	var nCommas = 0;		// Total times a comma occurs.
	var txtLen;				// Length of string passed.
	var xTxt;				// Assigned object passed.
	var sDollarVal;			// Assigned dollar amount with or without commas.
	var bComma;
	var decPos;				// Assigned value of numbers or positions after decimal point.
	var nNumCount = 0;		// Total number between commas.
	var i;					// For forloop indexing.
	var x;					// Assigned each indivual character in string.

	// Set the xTxt variable to the object passed to this function.

	// Assign the length of the string to txtLen.

	xTxt = f;
	txtLen = f.value.length

	for(i = 0; i < txtLen; i++)
	{
		// Assign charater in substring to x.

		x = xTxt.value.substr(i, 1);
		if(x == "$")
			nDollarSign = nDollarSign + 1; // Sum total times dollar sign occurs.
		else if(x == ".")
			nDecimal = nDecimal + 1; // Sum total times decimal point occurs.
		else if(x == ",")
			nCommas = nCommas + 1; // Sum total times comma occurs.
		else if(parseInt(x) >= 0 || parseInt(x) <= 9)
			nNum = nNum + 1; // If the character is a number sum total times a number occurs.
		else
		{
			// Error occurs if any other character value is in the string
			// othere then the valid characters.
			alert("ERROR! \n\nYou have entered an illegal value!\nPlease enter only: Dollar" +
				  " Signs, Commas, Decimal Points, and numbers between 0...9!");
			return false;
		} // end else
	} // end for

	if(nDollarSign > 1)
	{
		alert("ERROR! \n\nYou have entered more then one dollar sign!\nPlease only enter one!");
		return false;
	} // end if

	if(nDecimal > 1)
	{
		alert("ERROR! \n\nYou have entered more then one decimal point!\nPlease only enter one!");
		return false;
	} // end if

	if(nDollarSign == 1)
	{
		// Make sure dollar sign in the first character in string
		// if there is a dollar sign present.
		if(xTxt.value.indexOf("$") != 0)
		{
			alert("ERROR!  \n\nThe dollar sign you entered is not in the correct position!");
			return false;
		} // end if
	}// end if

	if(nDecimal == 1)
	{
		// Get the number of numbers after the decimal point in
		// the string if there is a decimal point present
		decPos = (txtLen - 1) - xTxt.value.indexOf(".");

		// Floating point cannot be more then two.
		// Valid format after decimal point.

		/**********************************/
		/*   $#.##, $#.#, $.#, $#., $.##  */

		/**********************************/

		if(decPos > 2)
		{
			alert("ERROR! \n\nThe decimal point you entered is not in the correct position!");
			return false;
		} // end if
	} // end if

	if(nCommas == 0)
	{
		// If no commas are present value is a valid US
		// currency.
		return true;
	}
	else
	{
		// Get total number of dollar number(s), removing
		// floating point numbers or cents.
		nNum = nNum - decPos;

		// Determine if dollar sign is in string so to be
		// removed.
		// After determining dollar sign, assign sDollarVal
		// numbers and comma(s)

		if(xTxt.value.indexOf("$", 0) == 0)
			sDollarVal = xTxt.value.substr(1, (nNum + nCommas));
		else
			sDollarVal = xTxt.value.substr(0, (nNum + nCommas));

		// Determine if a zero is the first number or if a
		// comma is the first or last character in the string.

		if(sDollarVal.lastIndexOf("0", 0) == 0 )
		{
			alert("ERROR! \n\nYou cannot start the dollar amount out with a zero!");
			return false;
		}
		else if(sDollarVal.lastIndexOf(",", 0) == 0)
		{
			alert("ERROR! \n\nYou cannot start the dollar amount out with a comma!");
			return false;
		}
		else if(sDollarVal.indexOf(",", (sDollarVal.length - 1)) == (sDollarVal.length - 1))
		{
			alert("ERROR! \n\nYou cannot end the dollar amount with a comma!");
			return false;
		}
		else
		{
			// Initialize bComma indicating a comma has not been
			// occured yet.

			bComma = false;
			for(i = 0; i < sDollarVal.length; i++)
			{
				// Assign charater in substring to x.
				x = sDollarVal.substr(i, 1);
				if(parseInt(x) >= 0 || parseInt(x) <= 9)
				{
					// If x is a number add one to the number counter.
					nNumCount = nNumCount + 1;

					// Sense comma(s) are present number counter cannot
					// be more then three before the first or next comma.

					if(nNumCount > 3)
					{
						alert("ERROR! \n\nYou have a mis-placed comma!");
						return false;
					} // end if
				}
				else
				{
					// If the number counter is less then three and
					// the comma indicator is true the comma is either
					// mis-placed or there are not enough values.

					if(nNumCount != 3 && bComma)
					{
						alert("ERROR! \n\nYou have a mis-placed comma!");
						return false;
					} // end if

					// Reset the number counter back to zero.
					nNumCount = 0;


					// Set the comma indicator to true indicating
					// that the first comma has been found and that
					// there now MUST be three numbers after each
					// comma until the loop hits the end.

					bComma = true;
				} // end if
			} // end for

			// Determine if after the loop ended that there
			// was a total of three final numbers after the
			// last comma.

			if(nNumCount != 3 && bComma)
			{
				alert("ERROR! \n\nYou have a mis-placed comma!");
				return false;
			} // end if
		} // end if
	} // end if

	// Return true indicating that the value is a valid
	// currency.
	return true;
}

/* Check if there are any inavlid characters in a field */
function CleanApostrophe(myString)
{
   var pattern = /'/g;
   var newString = myString.replace(pattern,"''");
   return newString;
}

/* Trim function */
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

/* Check if column selected if selected submit form */
function CheckAndSubmit(checkString)
{
	var col = document.forms.getcolumn.column;

	if (col.options[col.selectedIndex].text != checkString) { 
		//alert(col.options[col.selectedIndex].text);
		document.getcolumn.submit();
	}
}

/* Check if column selected if selected submit form */
function GoToLink(element)
{
    var gotolink;
	gotolink = document.getElementById(element).value;
	//alert(document.getElementById(element).value);
	
	if (gotolink != "") {
		document.location.href="#" + gotolink;
	}
	
}


function CheckAndSubmit2(checkString)
{
	var col = document.forms.getalbum.album;

	if (col.options[col.selectedIndex].text != checkString) { 
		//alert(col.options[col.selectedIndex].text);
		document.getalbum.submit();
	}
}

function ComparePasswords(orig, copy, formname)
{
	var origtext = eval("document.forms." + formname + "." + orig + ".value");
	var copytext = eval("document.forms." + formname + "." + copy + ".value");
	
	if (origtext != copytext)
		alert ("The two passwords don't match. Please re-enter.");
}

function ChangeShipToInfo(formname)
{
	var EditForm = eval("document.forms." + formname);
	var isCopyText = EditForm.same.checked;

	var ShipState = EditForm.bstate.options[EditForm.bstate.selectedIndex].value;

	if (isCopyText) {
		if (EditForm.bstate.value == "NJ") {
			EditForm.saddr1.value = EditForm.baddr1.value;
			EditForm.saddr2.value = EditForm.baddr2.value;
			EditForm.scity.value = EditForm.bcity.value;
			//EditForm.sstate.selectedIndex =	EditForm.bstate.selectedIndex;
			EditForm.szip.value = EditForm.bzip.value;
		} else {
			alert("We are currently shipping only to NJ");
			EditForm.same.checked = false;
		}
	} else {
		EditForm.saddr1.value = "";
		EditForm.saddr2.value = "";
		EditForm.scity.value = "";
		EditForm.szip.value = "";
		EditForm.sstate.selectedIndex =	0;
	}
}

// Functions for creating cascading drop down menus --- START
function update(e, dd)
{
	for (j=1; j < dd.length; j++)
	{
		dd[j][0] = true;
	}

	for (j=1; j < dd[0].length; j++)
	{
		for (i=1; i < dd.length; i++)
		{
			current = dd[i][j].split("|");
			value = current[0];
			choice = current[0];
			if (current.length == 2) choice = current[1];
			if (value != document[dd[0][0]][dd[0][j]][document[dd[0][0]][dd[0][j]].selectedIndex].value) dd[i][0] = false;
		}
		if (e == document[dd[0][0]][dd[0][j]])
		{
			dropdown(j+1,dd);
			for (k=j+2; k < dd[0].length; k++)
			{
				document[dd[0][0]][dd[0][k]].length = 0;
			}
			break;
		}
	}
}

function dropdown(item,dd)
{
	var pre1 = "";
	var j = 1;
	document[dd[0][0]][dd[0][item]].options.length = 0;
	document[dd[0][0]][dd[0][item]].options[0] = new Option('Select ' + dd[0][item], '');
	document[dd[0][0]][dd[0][item]].options[0].selected = true;
	for (i=1; i < dd.length; i++)
	{
		if (dd[i][0] || item == 1)
		{
			current = dd[i][item].split("|");
			value = current[0];
			choice = current[0];
			if (current.length == 2) choice = current[1];
			if (value != pre1)
			{
				var op = new Option(choice, value);
				document[dd[0][0]][dd[0][item]].options[j] = op;
				j++;
				pre1 = value;
			}
		}
	}
}

// Functions for creating cascading drop down menus --- END