function validate_email(field,alerttxt)
{
    with (field)
    {
        apos=value.indexOf("@");
        dotpos=value.lastIndexOf(".");
        if (apos<1||dotpos-apos<2) 
            {alert(alerttxt);return false;}
        else 
            {return true;}
    }
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function validate_phone(field,alerttxt)
{
    var i;
    var diffChar = false;
    var validphone = true;
        
    with (field)
    {
        s=stripCharsInBag(value.toString(),validWorldPhoneChars);
        var firstChar = s.charAt(0);

        if (s.length < minDigitsInIPhoneNumber)
            validphone = false;
        for (i = 0; (i < s.length && validphone); i++)
        {   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) validphone=false;
            if (c != firstChar) diffChar = true;
        }
        validphone = validphone && diffChar;
    }
    
    if (!validphone) alert(alerttxt);
    return validphone;
}

// check to see if input is alphanumeric
function isValidCharacter(val)
{
	if (val.match(/^[\w&-_.\s"\'\$]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function validate_form(thisform,email,phone)
{
    with (thisform)
    {
        for(i=0; i< thisform.elements.length; i++)
        {
            if ( thisform.elements[i] == phone) {
          //      alert("validate phone")
                if (validate_phone(phone,"Please enter a valid phone number")==false)
                    {phone.focus();return false;}
                continue;
            }
            if (thisform.elements[i] == email) {
           //     alert("validate email")
                if (validate_email(email,"Please enter a valid e-mail address")==false)
                    {email.focus();return false;}
                continue;
            }
            if (thisform.elements[i].type == "text")
            {
                var val = (thisform.elements[i].value);
                if (val.length && !isValidCharacter(val))
                {
                    alert("Invalid Characters in responses. Please enter only characters a-z, A-Z, 0-9,\',\", -, _, ., or &");
                   thisform.elements[i].focus();
	                return false;
	            }
            }
        }
    }
}

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_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_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_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 doSection(idVariable){
//display the section if it's not displayed; hide it if it is displayed
if (idVariable.style.display=="none"){idVariable.style.display=""}
else{idVariable.style.display="none"}
}
function noSection(idVariable){
//remove the section when user clicks in the opened DIV
if (idVariable.style.display==""){idVariable.style.display="none"}
}

//   expandable section script
function dsp(loc){
   if(document.getElementById){
      var foc=loc.firstChild;
   //     var foc;
   //   foc=loc.firstChild.innerHTML?
     //    loc.firstChild:
     //    loc.firstChild.nextSibling;
   //   foc.innerHTML=foc.innerHTML=='+'?'-':'+';
      foc=loc.parentNode.nextSibling.style?
         loc.parentNode.nextSibling:
         loc.parentNode.nextSibling.nextSibling;
      foc.style.display=foc.style.display=='block'?'none':'block';
   }
}
