// JavaScript Document


//*********************    FORM VALIDATION    ************************************
// DEFINE ERROR MESSAGE FOR FORM VALIDATION FUNCTION
err = 0;
errc = ['OK'];
errc[1] = 'Please verify the zip code (5 or 9 digits).';
errc[2] = 'Please enter the telephone number, including area code.';
errc[3] = 'Please check the expiration date: four digits, e.g., 0709 is the correct date for July, 2009.';
errc[4] = 'This card is not working; can you check the card number again?';
errc[5] = 'Please do not leave this field blank.';
errc[6] = 'Please fill in the email address.';
errc[7] = 'Please check the expiration date: apparently it is expired.';
errc[8] = 'Please check the expiration date: it is not valid.';
errc[9] = 'Please complete the entry.';
errc[10] = 'Please enter a numerical value (may include an exponent e+N).';
errc[11] = 'Please enter a complete URL.';

function cardno(s) {                    // Credit Card Number
  s = s.replace(/\D+/g, '');
  err = checksum(s) ? 0 : 4;
  return [s];
}

function email(s) {                     // Email Address
  a = s.match(/\S+@([-\w]+\.)+\w+/g);
  err = a != null && a.length ? 0 : 6;
  return a;
}

function expires(s) {                   // Expiration Date
  s = nwts(s);
  var m = new Date()
  var m_year = m.getFullYear()%100, m_month = m.getMonth();
  if (s.length != 4 || isNaN(s))
  err = 3;
  else {
    s_month = s.substring(0,2)-0;
    s_year = s.substring(2, 4)-0;
    if (m_year > 70 && s_year < 30) s_year += 100;
/* "Let your great grandson worry about that." */
    if (s_year < m_year || s_year > m_year + 6) err = 8;
    else if (s_year == m_year && !(s_month > m_month)) err = 7;
    else err = 0;
  }   
  return [s];
}

function number(s) {                    // Any Number, Including Exponent
  s = nwts(s)-0;
  err = isNaN(s) ? 10 : 0;
  return [err ? 0 : s];
}

function tel(s) {                       // Telephone, With Area Code + Opt Prefixes
  var a = s.replace(/\D+/g, '-');
  a = a.match(/(\d+-?)*(\d{3}-?){2}\d{4}/g);
  err = a != null && a.length ? 0 : 2;
  return a;
}

function text(s) {                      // Text String, Used For Required Input Fields
  s = trimwts(s);
  err = s.length ? 0 : 5;
  return [s];
}

function url(s) {                       // URLS
  a = s.match(/\w{2,}:\/{2}([-\w]+\.)+\w+\S*/g);
  err = a != null && a.length ? 0 : 11;
  return a;
}

function words(s) {                     // Multiple Words In A Field
  s = trimwts(s);
  err = /\s/.test(s) ? 0 : 9;
  return [s];
}

function zip(s) {                       // zip code
  var a = nwts(s).match(/\d{5}(-?\d{4}){0,1}/g);
  err = a != null && a.length ? 0 : 1;
  return a;
}

function nwts(s) {                      // no white space
  return s.replace(/\s+/g, '');
}

function trimwts(s) {                   // trim white space
  return s.replace(/^\s+|\s+$/g, '');
}

/* this code is copied from
http://www.codelib.net/home/jkm/checksum.js */
function checksum(s) {                  // thanks to daniel_amor@hp.com for AMEX specs
  var p=0, e=8, t=0, c=[], r=0, l=0, i;
  if (s.length != 16) {
    t = 1;
    e = s.length == 13 && 6 || s.length == 15 && 7;
  }
  for (i=p; i<e; i++)
  r += (c[i] = s.charAt(i*2+t) * 2) > 9
  ? Math.floor(c[i]/10 + c[i]%10)
  : c[i];
  for (i=p; i<e+t; i++) l += s.charAt(i*2+1-t)-0;
  l += r;
  return e && l%10 == 0;
}

function valid(element, check) {
  if (element.type == 'text' || element.type == 'textarea') {
    return check(element.value);
  } 
  else { 
    return check(element);
  }
}

present_element = null, present_error = null;

// MAIN VALICATION FUNCTION
function validate(form, list) {
  for (i=0; i<form.elements.length; i++) {
    var element = form.elements[i];
    var n = element.name, out;
    if (list[n] && list[n].verify) {
      out = valid(element, list[n].verify);
      if (err && (list[n].force || present_element != element || present_error != err)) {
        present_error = err;
        present_element = element;
        alert(list[n].message || errc[err]);
        element.focus();
        return false;
      } else if (!err && list[n].filter)
      element.value = out.join(', ');
    }
  }
var total = 0;
var max = form.hood.length;
for (var idx = 0; idx < max; idx++) {
if (eval("document.infoForm.hood[" + idx + "].checked") == true) {
    total += 1;
   }
 }
 if (total == '0') {
    alert("Please select a neighborhood");
	return false;
 }
 var el = document.forms[0].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
    alert("Please choose an answer for "+el[i].name+".");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
  return true;
}
//*********************    FORM VALIDATION    ************************************

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 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;
}

var writeWin = " ";
function popitup(url) { 
writeWin = window.open('','bizcard',"height=300,width=420,scrollbars=no,titlebar=no,status=no,toolbar=no,menubar=no,location=no,directories=no,resizable=no,left=200,top=200");
var ePen ='<html><head><title>Business Card</title></head>'; 
ePen += '<body><p><img src=' + url + '></p><table width="400" border="0" cellpadding="0" cellspacing="0"><tr><td align="center"><a href=# onClick="javascript:window.print()">PRINT</a> <a href=# onClick="javascript:window.close()">CLOSE</a></td></tr><tr><td align="right"><font size="1">&copy;2004 GMT Sales, Inc.</font></td></tr></table></body></html>';
var wd = writeWin.document;
wd.open();
wd.write(ePen);
wd.focus();
wd.close();
}

// OPEN WINDOW Ads
function NewWindow(mypage) {

winName='advertisement'
winprops = 'width=760,height=680,resizable=yes,scrollbars=yes'
win = window.open(mypage, winName, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// OPEN WINDOW calendar
function NewWindowEvent(mypage) {

winName='event'
w='450'
h='400'
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;

winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,resize'
win = window.open(mypage, winName, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// Confirm Delete
function confirmDelete(varCancel,varOK) {
if (confirm("Are you sure you want to delete record?")) {
  pageRedirect(varOK)
  //alert ("DELETE!");
}
else {
  pageRedirect(varCancel)
  //alert ("DO NOT DELETE!");
   }
}


// Redirect
function pageRedirect(varPage) {
  window.location = varPage; 
}


//category count for listingadmin.asp
var freeItemCount=0 
var maxFreeItems=3

function setItems(item)
{ 
  if(item.checked)
  {freeItemCount=freeItemCount+1}

else
 {freeItemCount=freeItemCount-1} 

if (freeItemCount>maxFreeItems)
  {item.checked=false 
  freeItemCount=freeItemCount-1 
  alert('You may only choose '+maxFreeItems+' Categories.') 
  } 
} 