
function validateEmail(obj) {
   var str=obj.value;
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = str;
   if(reg.test(address) == false) {
      alert('Invalid Email Address');
      return false;
   }
   return true;
 }

function validateURL(obj) {
   var str=obj.value;
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    if(RegExp.test(url)){
        return true;
    }else{
        return false;
    } 
 }

function  validateNumber(obj,thesize,themessage) {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
   var strString=obj.value;

   if (strString.length < thesize ) {
	    blnResult = false;
   } else {

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)     {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)        {
         blnResult = false;
         }
      }
   }

if (!blnResult)
{
	alert(themessage);
	obj.focus();
}
   return blnResult;
}


function validatePhone(obj)   {
   var strValidChars = "0123456789-";
   var strChar;
   var blnResult = true;
   var strString=obj.value;

   if (strString.length && strString.length !=12) {
	    blnResult = false;
   } else {

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)     {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)        {
         blnResult = false;
         }
      }
   }

if (!blnResult)
{
	alert("Invalid Phone Number Format");
	obj.focus();
}
   return blnResult;
}

function validateZip(obj)   {
   var strValidChars = "0123456789-";
   var strChar;
   var blnResult = true;
   var strString=obj.value;

   if (strString.length <5 || strString.length >10) {
	    blnResult = false;
   } else {

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)     {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)        {
         blnResult = false;
         }
      }
   }

if (!blnResult)
{
	alert("Invalid Zip Code Format");
	obj.focus();
}
   return blnResult;
}


 

function getelementsbyPrefix(inPrefix,inRoot){
var elem_array = new Array;
if(typeof inRoot.firstChild!= 'undefined'){
var elem = inRoot.firstChild;
while (elem!= null){
if(typeof elem.firstChild!= 'undefined'){
elem_array = elem_array.concat(getelementsbyPrefix(inPrefix,elem));
}
if(typeof elem.id!= 'undefined' && elem.id != '' && elem.id != '[object]'){
var reg = new RegExp ( '^'+inPrefix+'.*' );
if(elem.id.indexOf(reg)){
elem_array.push(elem);
}
}
elem = elem.nextSibling;
}
}
return elem_array;
}


function validateContactUs(theform) {

	if (theform.firstname.value == '')
	{
		alert("First Name is Required");
		theform.firstname.focus();
		return false;
	}
	if (theform.lastname.value == '')
	{
		alert("Last Name is Required");
		theform.lastname.focus();
		return false;
	}

	if (theform.email.value == '')
	{
		alert("Email Address is Required");
		theform.email.focus();
		return false;
	}

	if (theform.comments.value == '')
	{
		alert("Please provide a message");
		theform.comments.focus();
		return false;
	}

	if (!validateEmail(theform.email))
	{
		return false;
	}

	return true;
}


function validateCueSheet(theform) {

	if (theform.fullname.value == '')
	{
		alert("Your Name is Required");
		theform.fullname.focus();
		return false;
	}

	if (theform.email.value == '')
	{
		alert("Your Email Address is Required");
		theform.email.focus();
		return false;
	}

	if (!validateEmail(theform.email))
	{
		return false;
	}

	if (theform.name.value == '')
	{
		alert("Ride Name is Required");
		theform.name.focus();
		return false;
	}

	if (theform.distance.value == '' || theform.distance.value == 0)
	{
		alert("Distance is Required");
		theform.distance.focus();
		return false;
	}
	if (!validateNumber(theform.distance,1,"Distance must be numeric"))
	{
		theform.distance.focus();
		return false;
	}

	if (theform.town.value == '')
	{
		alert("Ride Town is Required");
		theform.town.focus();
		return false;
	}

	if (theform.rating.value == '')
	{
		alert("Ride Rating is Required");
		theform.rating.focus();
		return false;
	}

	if (theform.filename.value == '')
	{
		alert("Cue Sheet is Required");
		theform.filename.focus();
		return false;
	}

	return true;
}



function popupwindow(page,field1) {
    if (page == "xxx") {
      window.open('xxx.php?id='+ field1 + '&a=' + page, page,'toolbar=0,status=0,width=800,height=600,resizable=0,scrollbars=1');
     }
}

function popupatmail(url) {
    if (url != "") {
      window.open(url, 'AtMail','toolbar=0,status=0,width=800,height=600,resizable=0,scrollbars=1');
     }
}

function viewIDImage(theimage) {
    if (theimage != "") {
      window.open('http://www.nytoa.org/admin/idimages/'+ theimage, 'ID Cards','toolbar=0,status=0,width=800,height=600,resizable=0,scrollbars=1');
     }
}




function GeneratePassword(thisform) {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

    var length=6;
    var sPassword = "";

    var noPunction = true;
    var randomLength = false;

    if (randomLength) {
        length = Math.random();

        length = parseInt(length * 100);
        length = (length % 7) + 6
    }


    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }

    thisform.password.value = sPassword

    return true;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}