function validation(formElemName, elemType, upToSnuff, alertText) {
  this.formElemName = formElemName;
  this.elemType = elemType;
  this.upToSnuff = upToSnuff;
  this.alertText = alertText;
}

var Name1 = new validation('Name1', 'text', 'isText(str)', 'Please enter your First and Last Name. \nDo not use ALL Caps or Quotes. \" \".');
var Address = new validation('Address', 'text', 'isText(str)', 'Please enter your postal address. \nDo not use ALL Caps or Quotes. \" \".');
var City = new validation('City', 'text', 'isText(str)', 'Please enter your city. Do not use ALL Caps.');
var State = new validation('State', 'text', 'isSelect(formObj)', 'Please select a postal address state.');
var Zip = new validation('Zip', 'text', 'isZip(str)', 'Please check your zip code format,\nUS: xxxxx or xxxxx-xxxx \nCN: xxxxxx or xxx-xxx.');
var E_mail = new validation('E_mail', 'text', 'isEmail(str)', 'Please enter your email address and/or check the format, \ni.e. emailname@domainname.com.');
var E_mail2 = new validation('E_mail2', 'text', 'checkEmail2(formObj,form)', 'Please re-enter your email in the second field the same as in the first one.');
var How_found = new validation('How_found', 'text', 'isText(str)', 'Please tell us how you found us. Do not use ALL Caps or Quotes\n If it was an FNO Member, enter Name or Affiliate number.');
var Fest_biz_search = new validation('Fest_biz_search', 'text', 'isRadio(formObj)', 'Do you want to be listed in the FNO Festival Biz Directory\? \n Free exposure for your business.');
var Type = new validation('Type', 'text', 'isSelect(formObj)', 'Please select your interest in festivals.');
var userid = new validation('userid', 'text', 'isUserid(str)', 'Please choose a User ID. \nUse 3-12 letters/numbers only, no spaces.');
var pw = new validation('pw', 'text', 'isPassword(str)', 'Please choose a Password. \nUse 3-12 lowercase letters/numbers only, no spaces.');
var URL = new validation('URL', 'text', 'isURL(str)', 'Please enter the URL of your web site, i.e. www.company.com. \nIf you do not have a web site leave this box blank.');
var Description = new validation('Description', 'text', 'isDescription(formObj,form)', 'Please enter a Description for your company. 500 characters max. \n Do not use all CAPS or quotes " ".');
var Fest_biz_search = new validation('Fest_biz_search', 'text', 'isCheck(formObj)', 'Please select if you want to list your company in the FNO Festival Biz Directory or not.');


var elems = new Array( Name1, Address, City, State, Zip, URL, E_mail, E_mail2, Type, Description, Fest_biz_search, userid, pw );

// mc fields
var txtBillingEmail = new validation('txtBillingEmail', 'text', 'isEmail(str)', 'Please enter your email address and/or check the format, \ni.e. emailname@domainname.com.');
var txtBillingFirstName = new validation('txtBillingFirstName', 'text', 'isText(str)', 'Please enter your billing First Name. \nDo not use ALL Caps or Quotes. \" \".');
var txtBillingLastName = new validation('txtBillingLastName', 'text', 'isText(str)', 'Please enter your billing Last Name. \nDo not use ALL Caps or Quotes. \" \".');
var txtBillingAddress1 = new validation('txtBillingAddress1', 'text', 'isText(str)', 'Please enter your billing postal address. \nDo not use ALL Caps or Quotes. \" \".');
var bill_country = new validation('bill_country', 'text', 'isSelect(formObj)', 'Please select your billing country. Do not use ALL Caps.');
var bill_state = new validation('bill_state', 'text', 'isSelect(formObj)', 'Please select a billing address state.');
var txtBillingCity = new validation('txtBillingCity', 'text', 'isText(str)', 'Please enter your billing city. Do not use ALL Caps.');
var txtBillingZIP = new validation('txtBillingZIP', 'text', 'isZip(str)', 'Please check your billing zip code format,\nUS: xxxxx or xxxxx-xxxx \nCN: xxxxxx or xxx-xxx.');
var txtBillingFAX = new validation('txtBillingFAX', 'text', 'str=="" || isPhoneNum(str)', 'Please check billing fax number format, include area code: (xxx) xxx-xxxx.');
var txtBillingPhone = new validation('txtBillingPhone', 'text', 'isPhoneNum(str)', 'Please enter a valid billing phone number, including area code: (xxx) xxx-xxxx.');

var Create_an_FNO_Account = new validation('Create_an_FNO_Account', 'text', 'isRadio(formObj)', 'Please check if want to create FNO account during checkout?');

var txtShippingEmail = new validation('txtShippingEmail', 'text', 'formObj.chkSameAsAbove.checked || isEmail(str)', 'Please enter your email address and/or check the format, \ni.e. emailname@domainname.com.');
var txtShippingFirstName = new validation('txtShippingFirstName', 'text', 'formObj.chkSameAsAbove.checked || isText(str)', 'Please enter your shipping First Name. \nDo not use ALL Caps or Quotes. \" \".');
var txtShippingLastName = new validation('txtShippingLastName', 'text', 'formObj.chkSameAsAbove.checked || isText(str)', 'Please enter your shipping Last Name. \nDo not use ALL Caps or Quotes. \" \".');
var txtShippingAddress1 = new validation('txtShippingAddress1', 'text', 'formObj.chkSameAsAbove.checked || isText(str)', 'Please enter your shipping postal address. \nDo not use ALL Caps or Quotes. \" \".');
var ship_country = new validation('ship_country', 'text', 'formObj.chkSameAsAbove.checked || isSelect(formObj)', 'Please select your shipping country. Do not use ALL Caps.');
var ship_state = new validation('ship_state', 'text', 'formObj.chkSameAsAbove.checked || isSelect(formObj)', 'Please select a shipping address state.');
var txtShippingCity = new validation('txtShippingCity', 'text', 'formObj.chkSameAsAbove.checked || isText(str)', 'Please enter your shipping city. Do not use ALL Caps.');
var txtShippingZIP = new validation('txtShippingZIP', 'text', 'formObj.chkSameAsAbove.checked || isZip(str)', 'Please check your shipping zip code format,\nUS: xxxxx or xxxxx-xxxx \nCN: xxxxxx or xxx-xxx.');
var txtShippingFAX = new validation('txtShippingFAX', 'text', 'formObj.chkSameAsAbove.checked || str=="" || isPhoneNum(str)', 'Please check shipping fax number format, include area code: (xxx) xxx-xxxx.');
var txtShippingPhone = new validation('txtShippingPhone', 'text', 'formObj.chkSameAsAbove.checked || str=="" || isPhoneNum(str)', 'Please check shipping phone number format, include area code: (xxx) xxx-xxxx.');

function isDescription(formObj,form) {
  if(!form.Fest_biz_search[0].checked) return true;
  return isText(formObj.value);
}

function isText(str) {
 if(str==""){return false;}
if(str.indexOf("\"") != -1){return false;}

re=/[0-9a-z]{1,}/;
return(re.test(str));
}

function isSelect(formObj) {
    // state field type is changing dynamically
    if(formObj.type == 'text') return(isText(formObj.value));
    return (formObj.selectedIndex != 0)
}

function isRadio(formObj) {
  for (j=0; j<formObj.length; j++) {
    if (formObj[j].checked) {
      return true;
    }
  }
  return false;
}

function isCheck(formObj) {
  for (var c=0; c<formObj.length; c++) {
	  if (formObj[c].checked) {
	    return true;
	  }
  }
  return false;
}

function isPersonName(str) {
  re=/^[-A-Za-z]+\.?(\s+[-A-Za-z]+,?)+$/;
  tmp = re.test(str);
  return tmp;
}

function isEmail(str) {
  re=/^[^@\s]+@[^@\s.]+(\.[^@\s.]+)+$/;
  tmp = re.test(str);
  return tmp;
}

function checkEmail2(formObj,form) {
  return form.E_mail.value == '' || form.E_mail.value.toLowerCase() == formObj.value.toLowerCase();
}

function isURL(str) {
  if (str.toLowerCase() == 'na') return true;
  re=/^((https?:\/\/|)([a-z0-9_-]+\.)*[a-z0-9-]+\.[a-z0-9-]+(\/\S*|)|\s*)$/i;
  tmp = re.test(str);
  return tmp;
}

function isZip(str) {
   re=/^[0-9]{5}(-[0-9]{4})?$/;
   tmp = re.test(str);
   if (tmp == false) {
    re=/^[0-9A-Za-z]{3}\s?-?[0-9A-Za-z]{3}$/;
    tmp = re.test(str);
   }
  return tmp;
}

function isPhoneNum(str) {
  re=/^\(?[0-9]{3}[-)\s.\/]+[0-9]{3}[-\s.\/]+[0-9]{4}$/;
  tmp = re.test(str);
  return tmp;
}

function isUserid(str) {
  re=/^[a-zA-Z0-9]{3,12}$/;
  tmp = re.test(str);
  return tmp;
}

function isPassword(str) {
  re=/^[a-z0-9]{3,12}$/;
  tmp = re.test(str);
  return tmp;
}

function validate_signup(form) {
  var formElemName = "";
  var alertText = "";
  var formObj = "";
  var str = "";

  for (i=0; i<form.length; i++) {
  for (j=0; j<elems.length; j++) {
    formElemName = elems[j].formElemName;
    if (form[i].name == formElemName) {
    formObj = eval("form." + formElemName);
    if (formObj) {
	    alertText = elems[j].alertText;
	    str = formObj.value;

	    if (eval(elems[j].upToSnuff)) {
		  if (formObj.value) {
			re = /([A-Z]{4})/g;
			myArray = str.match(re);
			if (myArray) {
			alert('Please do not use all caps');
		}
			else {  
			continue;
			}
		  } 
	    } else{
	      alert (alertText);
	      var goToObj = formObj;
	      if (goToObj.select) goToObj.select();
	      if (goToObj.focus) goToObj.focus();
	      else { if (goToObj[0].focus) goToObj[0].focus(); }
	      return false;
	    }
    }
    }
  }
  }
  // rch: sometimes we need to go back and reenter company name
  //if (form.submit_now) form.submit_now.disabled=true; 
  return true;
}

var in_validateTypeField = false;
function validateTypeField(form) {
    if (!form.Type.selectedIndex) {
	in_validateTypeField = true;
	alert('Select your interest in festivals first');
	form.Type.focus();
	in_validateTypeField = false;
	return false;
    }
    return true;
}

var ajax = new Ajax();

function remote_validate(f) {
    if (!ajax) ajax = new Ajax();
    formElemName = f.name;
    Obj = eval(formElemName);
    formObj = eval("f.form." + formElemName);
    alertText = Obj.alertText;
    str = formObj.value = formObj.value.replace(/^  */, '').replace(/  *$/, '');;

    if (eval(Obj.upToSnuff)) {
  	  if (formObj.value) {
   		re = /([A-Z]{4})/g;
		myArray = str.match(re);
   		if (myArray) {
			alert('Please do not use all caps');
		} else {  
			el = document.getElementById(f.name+'_status');
			if (el) el.innerHTML = '<i>Validating...</i>'; else alert('no HTML element with id="'+f.name+'_status" ');
			var vurl= '/order_wr_validate.php?E_mail='+urlencode(f.value);
			if (f.form.Name1) vurl += '&Name1='+urlencode(f.form.Name1.value);
			else if (f.form.txtBillingFirstName) vurl += '&Name1='+urlencode(f.form.txtBillingFirstName.value+' '+f.form.txtBillingLastName.value);
			if (f.form.Phone) vurl += '&Phone='+urlencode(f.form.Phone.value);
			else if (f.form.txtBillingPhone) vurl += '&Phone='+urlencode(f.form.txtBillingPhone.value);
			ajax.doGet(vurl,signup_ajax_response,f.name,'text');
			return true;
   		}
  	  } 
    } else if (!in_validateTypeField) {
// rch: avoid too many prompts because of validateTypeField
	alert (alertText);
	var goToObj = formObj;
	if (goToObj.select) goToObj.select();
	if (goToObj.focus) goToObj.focus();
	else { if (goToObj[0].focus) goToObj[0].focus(); }
    }
    f.form.submit_now.disabled=true; 
    return false;
};

var signup_ajax_response = function(f,str) {
	el = document.getElementById(f+'_status');
	el.innerHTML = str;
	if (str == 'OK') {
		document.getElementById(f).style.backgroundColor = 'white';
		el.style.color = 'green';
		document.getElementById('submit_now').disabled=false; 
	} else {
		document.getElementById(f).style.backgroundColor = '#FFAAAA';
		el.style.color = '#FF0000';
		document.getElementById('submit_now').disabled=true; 
		if (f == 'txtBillingEmail' && str.match(/already have/)){
			document.getElementById('checkout_info_fields').style.display = 'none';
		}
		alert(str);
		if (f == 'E_mail' && str.match(/already have/) && !(document.getElementById(f).form.admin_form && document.getElementById(f).form.admin_form.value)) {
			window.location = "http://festivalnet.com/help/current_member_use_upgrade.html"; 
		}
	}
}

function redirectByTypeField(Type) {
	if (Type.selectedIndex >= 0 ) {
		var val = Type.options[Type.selectedIndex].value;
		if (val == '') val = Type.options[Type.selectedIndex].text;
		if ( val == 'Art Council/Guild' || val == 'Association/Organization' || val == 'Event Production/Planning' || val == 'Event Organizer' || val == 'Event Venue' || val == 'Festival/Event/Show' || val == 'Show Promoter/Producer') {
			window.location = '/add_events.html'; 
			return true;
		}
	}
	return false;
}

function CJL_getCurrentStyle(elem, prop)
{
   if( elem.currentStyle )
   {  
      var ar = prop.match(/\w[^-]*/g);
      var s = ar[0];
      
      for(var i = 1; i < ar.length; ++i)		   
      {
         s += ar[i].replace(/\w/, ar[i].charAt(0).toUpperCase());
      }
           
      return elem.currentStyle[s]
   }
   else if( document.defaultView.getComputedStyle )
   {
      return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
   }
}

