//====================================================================
// This is the main javascript sheet. (c) Singles Travel Club (c) 2004  
//====================================================================
//====================================================================
//	Form Fields Validations
//====================================================================
//--------------------------------------------------------------------
// Validate and Format a 10 digit phone number
// courtesy Real Gagnon (http://www.rgagnon.com)
//--------------------------------------------------------------------
function formatPhone(field) {
	/* strip whitespace head & tail */
	var phoneTemp = trimAll(field.value);
	var phoneOK  = /^\s*\(?[1-9]\d{2}\)?\s*\-?\d{3}\s*\-?\s*\d{4}$/;
	/* validate for ten digits; dashes, spaces & parentheses (optional) */
	
	if (!phoneTemp == "") {
		if (!phoneOK.test(phoneTemp)) {
			invalidMsg(field,"Please enter a valid 10 digit phone number.");
			field.focus();
			field.select();
			return true;
		}
	
		/* numbers only */
		phoneTemp = phoneTemp.replace(/[^\d]/g,'');
		/* build the *Phone* string as (###) ###-#### */
		phoneTemp = '(' + 
		phoneTemp.substring(0,3) + ') ' + 
		phoneTemp.substring(3,6) + '-' + 
		phoneTemp.substring(6,10);
	
		field.value = phoneTemp;
		return true;
	}
}

function trimAll(strValue) {	// Strip everything except digits.
  var STR="";
  var _exp =/\d+/g;
  var _array;
  while(_array=_exp.exec(strValue))
	STR+=_array.join();
  return(STR);
}
//--------------------------------------------------------------------
//	Validation and submit to email Function
//--------------------------------------------------------------------
function doSubmit (form)  {
	var namePattern = /^(\w+)/;
	var phonePattern = /^(\(+\d{3}\)\s\d{3}-\d{4})/; 
	var emailPattern = /^.+\@.+\..+$/;   
	

	if (form.SendTo.value == "") {
		invalidMsg(form.SendTo,"Please Select a Recipient");
		return;
		}

	if (form.Name.value.match(namePattern) == null) {     
		invalidMsg(form.Name,"Required - Please enter a name");
		return;
		}

	if (form.Reply_Via[0].checked) {
		if (form.Email.value == "you@your-isp.com" || form.Email.value == "" || !form.Email.value.match(emailPattern)) {
			invalidMsg(form.Email,"Required - Please enter a valid email address");
			return;
			}
	}

		if (form.Phone.value == "") {
			if (!form.Phone.value.match(phonePattern)) {
				invalidMsg(form.Phone,"Required - Please enter a phone number");
				return;
			}
		}

	if (form.Reply_Via[2].checked) {
		if (form.Fax.value == "") {
			invalidMsg(form.Fax,"Fax number is required for a Fax reply");
			return;
			}
	}

	if (form.Reply_Via[3].checked) {
		if (form.Cell.value == "") {
			invalidMsg(form.Cell,"Cell number is required for a Cell reply");
			return;
			}
	}

//--------------------------------------------------------------------
// User form fields all valid, so save to cookie and submit
//--------------------------------------------------------------------
	saveProfile(form);  // User Profile form fields all valid, so save to cookie
	formAction();				// Set form action

	alert ("Form Submitted!");
	document.mailer.submit(document.mailer.action += mailtoandsubject); 
	return;
}

function invalidMsg (loc, msg) {
	alert (msg);
	loc.focus();
	}
//--------------------------------------------------------------------
//	Set up Form ACTION
//--------------------------------------------------------------------
function formAction() {
	document.mailer.action = "mailto:";
	mailtoandsubject = ((document.mailer.SendTo.options[document.mailer.SendTo.selectedIndex].value)); 
}

//====================================================================
//	Rainbow Text
//====================================================================
//----------------------------- To use -------------------------------
// <script>
//  <!--
//    makeRainbow("TextTextTextTextTextText");
//  // -->
// </script>
//--------------------------------------------------------------------
function createHexArray(n) {
  this.length = n;
  for (var i = 1; i <= n; i++)
  this[i] = i - 1;

  this[11] = "A";
  this[12] = "B";
  this[13] = "C";
  this[14] = "D";
  this[15] = "E";
  this[16] = "F";
  return this;
}
hx = new createHexArray(16);

function convertToHex(x) {
  if (x < 17) x = 16;
  var high = x / 16;
  var s = high+"";
  s = s.substring(0, 2);
  high = parseInt(s, 10);
  var left = hx[high + 1];
  var low = x - high * 16;
  if (low < 1) low = 1;
  s = low + "";
  s = s.substring(0, 2);
  low = parseInt(s, 10);
  var right = hx[low + 1];
  var string = left + "" + right;
  return string;
}

function makeRainbow(text) {
  text = text.substring(0, text.length);
  color_d1 = 255;
  mul = color_d1 / text.length;
  for(var i = 0; i < text.length; i++) {
    color_d1 = 255*Math.sin(i / (text.length / 3));
    color_h1 = convertToHex(color_d1);
    color_d2 = mul * i;
    color_h2 = convertToHex(color_d2);

    k = text.length;
    j = k - i;
    if (j < 0) j = 0;
    color_d3 = mul * j;
    color_h3 = convertToHex(color_d3);

    document.write("<FONT COLOR=\"#" + color_h3 + color_h1 + color_h2 + "\">" + text.substring(i, i + 1) + "</FONT>");
  }
}

//===================================================================================
// Macromedia Functions
//===================================================================================
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
