
// Old style function used in billaddress and shipaddress during checkout.
// will eventually get rid of this in favor of using Validate() below.
function validate_address_info(){
	var reqFields = new Object();
	reqFields['first_name'] = 'your first name';
	reqFields['last_name'] = 'your last name';
	reqFields['address'] = 'your address';
	reqFields['city'] = 'your city';
	reqFields['state'] = 'your state / province';
	reqFields['zip_code'] = 'your postal zip code';
	reqFields['country'] = 'your country';
	reqFields['phone'] = 'your phone number';
	reqFields['email'] = 'your email address';

	var els = document.forms['form1'].elements;

	var missing = new Array();
	for (var k in reqFields)
	{
		//alert(eks[k].value); //!!
		if (trim(els[k].value) != '') continue;
		missing[missing.length] = reqFields[k];
	}

	if (missing.length)
	{
		var msg = 'One or more required fields were not filled in:\n\n';
		for (var i = 0; i < missing.length; ++i) msg += ' - ' + missing[i] + '\n';
		msg += '\nPlease enter a value into each to continue.\n';
		alert(msg);

		return false;
	}

	return true;
}

function trim(str)
{
	var ret = str.toString();
	ret = ret.replace(/^\s*/, '');
	ret = ret.replace(/\s*$/, '');
	return ret;
}


function getThisPage(){

   // Get page name
   var stringPage = new String(window.location);
   pageArray = stringPage.split('/');
   var thisPage =  pageArray[(pageArray.length - 1)];

   // Get rid of query string
   var thisPage = (thisPage.split('?'))[0];

	return thisPage;
}




function isValidEmail(field){
   var email = field.value;
   if (email == null || email == ''){
      return false;
   }
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
      return true;
   }else{
      return false;
   }
}

function isEmpty(field){
   var value=trim(field.value);
   if (value == '' || value.length == 0){
      return true;
   }else{
      return false;
   }
}

function isValidDate(field){
   return true;
}

function validateForm(validations){
   var field;
   var checkType;
   var errMsg;
   var errMsgFull = '';
   var i;
   var elm;
   var submit = true;

   var values = eval(validations)

   for(i=0;i<values.length;i++){
      field = 		values[i][0];
      checkType = values[i][1];
      errMsg = 	values[i][2];

      elm = document.getElementById(field);

      if (checkType=='isSame'){  // For this particular type of check, a fourth argument is required.
         field2 = values[i][3];
         elm2 = document.getElementById(field2);
      }

      switch (checkType){
         // Two fields are the same
         case('isSame'):
            if(elm.value != elm2.value){
               submit = false;
               errMsgFull = errMsgFull + errMsg + "\n";
            }
            break;

         // Not Blank
         case('notBlank'):
            if (isEmpty(elm)){
               submit = false;
               errMsgFull = errMsgFull + errMsg + "\n";
            }
            break;
         // Email.
         case('isEmail'):
            if(! (isValidEmail(elm))){
               submit=false;
               errMsgFull = errMsgFull + errMsg + "\n";
            }
            break;
         // Date
         case('isDate'):
            if(!(isValidDate(elm))){
               submit=false;
               errMsgFull = errMsgFull + errMsg + "\n";
            }
            break;
      }
   }

   if (submit){
      return true;
   }else{
      alert(errMsgFull);
      return false;
   }
}
