var myref=window.location.href
if (myref.indexOf("www.")>-1)
{  location.replace("http://craigsworld.org/index.html")
}
if (myref.indexOf("htm")<0)
{  location.replace("index.html")
}
var today = new Date( )
var startsecs=today.getSeconds()

var ayear = new Date( )
ayear.setTime(today.getTime( ) + 1000*60*60*24*365)
var a5min = new Date( )
a5min.setTime(today.getTime( ) + 1000*60*5)
var anhour = new Date( )
anhour.setTime(today.getTime( ) + 1000*60*60)
var aday = new Date( )
aday.setTime(today.getTime( ) + 1000*60*60*24)
var killit = new Date( )
killit.setTime(today.getTime( ) + -1)

var doccats=""
var docreg=""
var myright=""
var cwlogo="<font color=blue size=+3><b>CRAIGSWORLD<br>\n"
cwlogo+="<img src=\"./pics/earthbig.gif\" align=left name=\"cworld\">"
cwlogo+=".ORG<font size=+0><br>"
cwlogo+="FREE GLOBAL<br>CLASSIFIED<br>ADS AND EVENTS<br></b>"
var didwarn=false

var didtv=false
var mytv="<iframe src=\"http://craigsworld.org/cwtv.html?"
var mychannel="data/cwdeals.html"
if (myref.indexOf("regions")>0)
{  mychannel="cwcity.html"
}

mytv=mytv+mychannel+"\" height=600 width=700></iframe>"

var mytype=""
var postwin=""
if (myref.indexOf("?silent")>0)
{  self.close()
}
if (myref.indexOf("?postok")>0)
{  window.location="cwpostok.html"
}
if (myref.indexOf("?buyads")>0)
{  window.location="cwbuyad.html"
}
if (myref.indexOf("?updated")>0)
{  window.location="cwbought.html"
}
if (myref.indexOf("?verifyad")>0)
{  var splitit=myref.split("?")
   var gothere=splitit[1]
   gothere="verifyad.html?"+gothere
   window.location=gothere
}


function makecats(pass)
{  var pass1=pass
   var pass2=""
   if (pass1.length>1)
   {  pass2=pass1.substr(1,1)
      pass1=pass1.substr(0,1)
   }
   var msga="<a href=\"javascript:find('"
   var msgb="')\">"
   var msgc="</a> "
   if (pass2!="")
   {  msga="<option value=\""
      var msgb="\">"
      msgc="\n"
   }
   var msg=""
   var msgz=""
   var omsg=""
   var start=""
   var stop=""
   if (pass1!='7')
   {  if (pass1=='1')
      {  start="global"
         stop="camping"
      }
      if (pass1=='2')
      {  start="rent"
         stop="estate"
      }
      if (pass1=='3')
      {  start="dating"
         stop="escort"
      }
      if (pass1=='4')
      {  start="job"
         stop="job:resumes"
         if (pass2=="")
         {  msgc="</a><br>"
         }
      }
      if (pass1=='5')
      {  start="Ido"
         stop="Ido:resumes"
         if (pass2=="")
         {  msgc="</a><br>"
         }
      }
      if (pass1=='6')
      {  start="barter"
         stop="music"
      }
      var writing=false
      var omsg=""
      for (i=0; i<myadcats.length; i++)
      {   msg=myadcats[i]
          if (pass2!="")
          {  writing=true
             stop="Ido:resumes"
             msgz=msg
          }  else
          {  msgz=msg
             var msgs=msg.split(":")
             var test=msgs[1]
             if ((test) && (test!=""))
             {  msgz=test
             }
          }
          if ((msg.indexOf(start)>-1) && (!writing))
          {  writing=true
          }
          if (writing)
          {  var newcats=msga+msg+msgb+msgz+msgc
             omsg+=newcats
          }
          if ((msg.indexOf(stop)>-1) && (writing))
          {  writing=false
             i=1000
          }
      }
   }  else
   {  var lastreg=getCookie("myregion")
      omsg="<option value=\"0\">[select one]\n"
      var msga="<option value=\""
      var msgb1="\""
      var msgb2=""
      var msgb3=">"
      var msgc=""
      for (i=0; i<myadreg.length; i++)
      {   msg=myadreg[i]
          msgb2=""
          if ((((!lastreg) || (lastreg.length<1)) && (i==0)) || ((lastreg) && (lastreg.length>0) && (msg.indexOf(lastreg)>-1)))
          {  var msgb2=" selected"
          }
          var newcats=msga+msg+msgb1+msgb2+msgb3+msg+"\n"
          omsg+=newcats
      }
   }
   return omsg
}

function getCookie(cookLbl)
{  var search=cookLbl + "="
   if (document.cookie.length>0)
   {  offset=document.cookie.indexOf(search)
      if (offset!=-1)
      {  offset +=search.length
         end=document.cookie.indexOf(";",offset)
         if (end==-1)
            end=document.cookie.length
            return unescape(document.cookie.substring(offset, end))
       }
   }
}

function ckemail(inEm)
{ var testEm = inEm
  var AtSym    = testEm.indexOf('@')
  var Period   = testEm.lastIndexOf('.')
  var Space    = testEm.indexOf(' ')
  var Length   = testEm.length - 1   // Array is from 0 to length-1
  if  ((AtSym < 1) ||                     // '@' cannot be in first position
      (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
      (Period == Length ) ||             // Must be atleast one valid char after '.'
      (Space  != -1))                     // No empty spaces permitted
  { return 0
  }
  return 1
}
var dispreg=""
if (myref.indexOf("/cw")<0)
{  var dispreg=makecats('7')
}

var myads="<img src=\"./pics/kckban.jpg\" name=\"ad1\" onClick=\"doad('1')\" width=400\">"

function setCookie(cookLbl, value, expire)
{  document.cookie=cookLbl + "=" + escape(value)
   + ((expire==null) ? "" : ("; expires=" + expire.toGMTString( )))
}

function ckvillage()
{ var atype=document.pform.type.value
  if (atype=="globalvillage")
  {  document.rform.region.value="Global/Worldwide"
  }
}

function gettype(pass)
{ mytype=document.sform.type.value
  var msg="Please select an ad category to search"
  if (pass=="1")
  {  mytype=document.pform.type.value
     var msg="Please select an ad category"
  }
  if (mytype=="0")
  {  alert(msg)
     document.sform.type.focus()
     if (pass=="1")
     {  document.pform.type.focus()
     }
     return false
  }
  return true
}

function post()
{ ckvillage()
  var myuser=document.pform.user.value
  var myregion=document.rform.region.value
  var mytitle=document.pform.title.value
  var mytext=document.pform.adtext.value
  if (!gettype('1'))
  {  return
  }
  var myphoto=document.pform.photo.value
  if (!ckemail(myuser))
  {  alert("Enter a valid email address")
     document.pform.user.focus()
     return
  }
  if (mytitle=="")
  {  alert("Enter a title")
     document.pform.title.focus()
     return
  }
  if (mytext=="")
  {  alert("Enter your ad text")
     document.pform.adtext.focus()
     return
  }
  if (mytext.length>500)
  {  alert("Ad text was more than 500 characters.\n\nAye Matey...our pirates hacked off the long-portion and you need to check your text.")
     mytext=mytext.substr(0,499)
     document.pform.adtext.value=mytext
     document.pform.adtext.focus()
     return
  }
  if (mytype=="0")
  {  alert("Enter the type of ad")
     document.pform.type.focus()
     return
  }
  if (myphoto!="")
  {  var ending=myphoto.substr((myphoto.length)-4,4)
     if ((ending!=".jpg") && (ending!=".gif") && (ending!=".JPG") && (ending!=".GIF"))
     {  alert("Photos must end with .jpg or .gif")
        document.pform.photo.focus()
        return
     }
     var fixphoto=""
     var showphoto=myphoto
     if (myphoto.indexOf("geocities.com") > -1)
     {  for (i=0; i<myphoto.length; i++)
        {   if (myphoto.substr(i,1)=="/")
            {  showphoto=myphoto.substr(0,i+1)+"index.html"
            }
        }
     }
     if (showphoto.substr(0,7)!="http://")
     {  showphoto="http://"+showphoto
     }
     window.open(showphoto,'','toolbar=no, scrollbars=yes, resizeable=yes, location=yes, height=500 width=500 left=300 top=50')
     var ok=confirm("We opened a window to your photo. Check it now. If you disabled POPUPS you will have to reenable them to view the photo.\n\nDid it appear correctly?\n\nClick OK if it appears ok or\n\nClick CANCEL to try again.")
     if (!ok)
     {  alert("The ad photos are linked to your own website: i.e. www.mysite.com/myface.jpg\n\nWe do not download your photos here.\n\nIf you have a geocities.com website (i.e. www.geocities.com/mywebsite/myphoto.jpg) your ad will show your entire index page of your geocities website in a frame to the right of your ad text.\n\nWe suggest you take advantage of www.geocities.com offer to create and build your own FREE website and post any photographs you want on the FAR LEFT margin for best appearance in your ads here.\n\nMAKE SURE GEOCITIES INDEX PAGES END WITH .HTML (not .htm).\n\nUse the geocities.com COPY function to create the .html page from your index.htm at the geocities.com site builder.")
        document.pform.photo.focus()
        return
     }
  }
  setCookie("myuser",myuser,ayear)
  setCookie("myregion",myregion,ayear)
  setCookie("mytitle",mytitle,ayear)
  setCookie("mytext",mytext,ayear)
  setCookie("mytype",mytype,ayear)
  setCookie("myphoto",myphoto,ayear)
  window.location="cwp.html"
}

function newemp()
{  location.replace("./pss/cwreg.html?signup")
}

function oldemp()
{  location.replace("./pss/cwreg.html?member")
}

function gopop(pass)
{ window.open(pass)
}

function ckregion()
{ var whatarea=getCookie("myregion")
  if ((whatarea) && (whatarea.length>0))
  {  var test=document.rform.region.value
     if (test!=whatarea)
     {  document.rform.region.value=whatarea
        window.location="#seeregion"
     }
  }
  setTimeout("ckregion()",3000)
}

function cookreg(pass)
{ setCookie("myregion",pass,ayear)
  var test=getCookie("myregion")
  if ((!test) && (!didwarn))
  {  alert("WARNING: we attempted to set your REGION and this failed.\n\nApparently your cookies are disabled and this may cause ads and other screens to malfunction.\n\nCookies help determine your preferences when travelling between screens.\n\nMany sites require them (i.e. yahoo.com)\n\nTo access all features at this website you should enable cookies for the domain craigsworld.org")
     didwarn=true
  }
}

function setregion(pass)
{ if (pass=="?")
  {  test=document.rform.region.value
     cookreg(test)
  }  else
  {  for (i=0; i<myadreg.length; i++)
     {   var test=myadreg[i]
         if (test.indexOf(pass)>-1)
         {  cookreg(test)
         }
     }
  }
}

function ckcook()
{ var test=getCookie("myregion")
  if (test)
  {  if (document.rform.region.value!=test)
     {  document.rform.region.value=test
     }
  }
}

if (myref.indexOf("kck")>0)
{  doad('3')
}

function settv(pass)
{ changetv(pass)
  window.location="#seeframe"
}

function changetv(pass)
{ setCookie("gotv",pass,ayear)
}

function cktv()
{ if (!didtv)
  {  didtv=true
     var chans=myref.split("?")
     var mychannel=chans[1]
     var test=chans[2]
     if (test)
     {  if (test.length>0)
        {  mychannel=mychannel+"?"+test
        }
     }
     if ((!mychannel) || (mychannel==""))
     {  var mychannel="data/cwdeals.html"
     }
     if (mychannel.indexOf("http")<0)
     {  mychannel="http://craigsworld.org/"+mychannel
     }  else
     {  var mychannel=test
     }
     var cwtv="<iframe src=\""+mychannel+"\" height=595 width=695></iframe>"
     setTimeout("cktv()",3000)
     return cwtv
  }
  var test=getCookie("gotv")
  if (test)
  {  if (myref.indexOf(test)<0)
     {  setCookie("gotv","",killit)
        var gothere="http://craigsworld.org/cwtv.html?"+test
        window.location=gothere
     }
  }
  setTimeout("cktv()",3000)
}


function cwcity()
{ settv("cwcity.html")
}

function goedu()
{ window.open("http://www.eduhosting.org")
}

function setad()
{ var test2=document.bform.myregion.value
  var test3=document.bform.mycat.value
  var asep="[}"
  for (i=0; i<items.length; i++)
  {   var anitem=items[i]
      isplit=anitem.split(asep)
      var ades=isplit[4]
      var acode=isplit[5]
      if (acode.substr(0,2)=="cw")
      {  sides=ades.split("Ad")
         lside=sides[0]+"Ad "+test2+" "+test3
         isplit[4]=lside
         var newline=""
         for (j=0; j<6; j++)
         {   var newseg=isplit[j]
             newline+=newseg
             if (newline!=5)
             {  newline+=asep
             }
         }
         items[i]=newline
      }
  }
}

function loaded()
{ if (myref.indexOf("index.html")>0)
  {  ckcook()
  }
  var loadtime = new Date( )
  var loadsecs=loadtime.getSeconds()
  if (loadsecs<startsecs)
  {  loadsecs=loadsecs+60
  }
  loadsecs=loadsecs-startsecs
  var newpic=""
  if (loadsecs>2)
  {  newpic="http://craigsworld.org/pics/cwus.jpg"
     var test=today.getTimezoneOffset()
     test=test/60
     if ((test<4) && (test>-4))
     {  newpic="http://craigsworld.org/pics/cweuro.jpg"
     }
     if (test<-3)
     {  newpic="http://craigsworld.org/pics/cwasia.jpg"
     }
     if (test<-7)
     {  newpic="http://craigsworld.org/pics/cwoz.jpg"
     }
  }
  if (newpic.length>0)
  {  document.cworld.src=newpic
  }
}

function find(pass)
{ if (pass=="?")
  {  pass=document.sform.type.value
  }
  var areg=document.rform.region.value
  if ((areg=="") || (areg=="0"))
  {  alert("Please select a region.")
     document.location="#seeregion"
     document.rform.region.focus()
     return
  }
  var gothere=areg+"+"+pass
  var test=document.sform.search.value
  if (test!="")
  {  gothere=areg+"+"+pass+"+"+test
  }
  gothere=escape(gothere)
  gothere="cwfind.html?"+gothere
  settv(gothere)
}


