function jsCSS(action,obj,class1,class2){
// author: Christian Heilmann - http://onlinetools.org
 switch (action){
 case 'swap':
 obj.className=!jsCSS('check',obj,class1)?obj.className.replace(class2,class1): obj.className.replace(class1,class2)
 break
 case 'add':
 if(!jsCSS('check',obj,class1)){obj.className+=obj.className?' '+class1:class1}
 break
 case 'remove':
 var rep=obj.className.match(' '+class1)?' '+class1:class1
 obj.className=obj.className.replace(rep,'')
 break
 case 'check':
 return new RegExp('\\b'+class1+'\\b').test(obj.className)
 break
 }
 return false
}

function replaceAll(str,from,to){
 var idx=str.indexOf(from)
 while (idx> -1){
 str=str.replace(from,to)
 idx=str.indexOf(from)
 }
 return str
}

function flashSlides(){
 var width=600
 var height=675
 var str=''
 str+='<object align="center" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'">'
 str+='<param name="movie" value="../viewer.swf" />'
 str+='<param name="quality" value="high" />'
 str+='<param name="bgcolor" value="#ffffff" />'
 str+='<param name="flashvars" value="" />'
 str+='<!--[if !IE]> <-->'
 str+='<object align="center" data="../viewer.swf" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash">'
 str+='<param name="quality" value="high" />'
 str+='<param name="bgcolor" value="#ffffff" />'
 str+='<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />'
 str+='<param name="flashvars" value="xmlDataPath=gallery.xml" />'
 str+='</object>'
 str+='<!--> <![endif]-->'
 str+='</object>'
 return (str)
}

function flashVideo(appWidth,appHeight,playList){
 // flvplayer.swf v3.99 - Jeroen Wijering - http://jeroenwijering.com/
 var player="flvplayer.swf"
 var flvVars='file='+playList
 flvVars+='&displayheight='+appHeight
 flvVars+='&displaywidth='+appWidth
 flvVars+='&autostart=true'
 flvVars+='&screencolor=0xffffff'
 flvVars+='&bwfile=http://www.becta.org.uk/videomedia/v5/100k.jpg&bwstreams=[100,496,828]&backcolor=0x115566&frontcolor=0x000000&lightcolor=0xffffff&showdigits=false&usefullscreen=false'
 var str=""
 str+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+appWidth+'" height="'+appHeight+'">'
 str+='<param name="movie" value="'+player+'" />'
 str+='<param name="flvQuality" value="high" />'
 str+='<param name="bgcolor" value="#FFFFFF" />'
 str+='<param name="wmode" value="transparent" />'
 str+='<param name="flashvars" value='+flvVars+' />'
 str+='<!--[if !IE]> <-->'
 str+='<object data="'+player+'" width="'+appWidth+'" height="'+appHeight+'" type="application/x-shockwave-flash">'
 str+='<param name="flvQuality" value="high" />'
 str+='<param name="bgcolor" value="#FFFFFF" />'
 str+='<param name="wmode" value="transparent" />'
 str+='<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />'
 str+='<param name="flashvars" value="'+flvVars+'" />'
 str+='</object>'
 str+='<!--> <![endif]-->'
 str+='</object>'
 return (str)
}

// this function is for a work-around in IE related to element attributes
function hasClass(obj) {
 var result=false
 if (obj.getAttributeNode("class")!=null)result=obj.getAttributeNode("class").value
 return result
}

function stripe() {
 var evenColor =arguments[1]?arguments[1]:"#f7fff7"
 var oddColor =arguments[2]?arguments[2]:"#f0f7f0"

 var tables=document.getElementById('content').getElementsByTagName("table")
 for (var f=0;f<tables.length;f++){
 var even=false
 var table = tables[f]
 var tbodies = table.getElementsByTagName("tbody")
 for (var h=0;h<tbodies.length;h++){
 var trs=tbodies[h].getElementsByTagName("tr")
 for (var i=0;i<trs.length;i++){
 if (! hasClass(trs[i]) && ! trs[i].style.backgroundColor){
 trs[i].style.backgroundColor=even?evenColor:oddColor
 var tds = trs[i].getElementsByTagName("td")
 for (var j=0;j<tds.length;j++){
 var mytd = tds[j]
 if (! mytd.style.backgroundColor){mytd.style.backgroundColor = even?evenColor:oddColor}
 } }
 even= !even
} } } }

function replaceImageVideo(id,playList){
 if (idExists(id)){
 replaceContent(id,'<a href="#">'+document.getElementById(id).innerHTML+'<img class="playvideo" src="play_video.png" width="100" height="17" title="" alt="play video" /></a>')
 document.getElementById(id).getElementsByTagName("img")[0].alt="play video"
 document.getElementById(id).getElementsByTagName("a")[0].title="play video"
 document.getElementById(id).getElementsByTagName("a")[0].onclick=function(){
 if (isFlash(minimumFlashVersion)){
 jsCSS('add',document.getElementById(id),'play')
 if(idExists('dance1'))
 replaceContent(id,flashVideo('320','165',playList))
 else
 replaceContent(id,flashVideo('320','240',playList))
 }
 else jsCSS('remove','html','hasJS')
 return false
} } }

function setup(){
 if (document.getElementById('content').getElementsByTagName("form").length==0) stripe()
 if (idExists('booking')) stripe()
 if (idExists('contact')) stripe()
 if (idExists('vacancy')) stripe()

 // anti-spam email address replacement.
 var E="&#64;"
 E+="zorbasdancerestaurant"
 E+=".co.uk"
 replaceContent('replaceEmail2','Email: <a href="mailto:info'+E+'?subject=Website enquiry">info'+E+'</a>')
 replaceContent('replaceEmail3','Email: <a href="mailto:vacancies'+E+'?subject=Vacancy enquiry">vacancies'+E+'</a>')

 if (idExists('galleryPlayer')){
 //replaceContent('slides',document.getElementById('imagelist').innerHTML)
 if (isFlash(minimumFlashVersion)) replaceContent('images',flashSlides())
 else jsCSS('remove','html','hasJS')
 }

 replaceImageVideo('belly1','playlist1.xml')
 replaceImageVideo('belly2','playlist2.xml')
 replaceImageVideo('dance1','playlist1.xml')
}

var minimumFlashVersion=8
addEvent(setup)












/* Form functions; setup and validation of vacancy, enquiry and reservation forms */

function isInteger(str){
 if (str.length){
 for (var i=0;i<str.length;i++) if ((str.substring(i,i+1)<'0') || (str.substring(i,i+1)>'9')) return false
 return true
 }
 return false
}

function isEmpty(str){return ((str==null)||(str.length==0))}

function isValidEmail(str){
 var r1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/
 var r2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/
 return !r1.test(str) && r2.test(str)
}

function isTelno(telNo){
 // Convert into a string and check exists
 var telnum=telNo+" "
 if (telnum.length==1) {telErr="Please enter a UK phone number."+alternate; return false}
 telnum.length=telnum.length-1

 // Don't allow country codes (assumes a leading "+")
 var exp = /^(\+)[\s]*(.*)$/;
 if (exp.test(telnum)==true) {telErr="Please enter a UK phone number without the country code."; return false}

 // Remove spaces
 while (telnum.indexOf(" ")!= -1) {telnum=telnum.slice(0,telnum.indexOf(" "))+telnum.slice(telnum.indexOf(" ")+1)}

 // Remove hyphens
 while (telnum.indexOf("-")!= -1) {telnum=telnum.slice(0,telnum.indexOf("-"))+telnum.slice(telnum.indexOf("-")+1)}

 //check all characters are digits
 exp = /^[0-9]{10,11}$/
 if (exp.test(telnum)!=true) {telErr="Please enter a UK phone number. \n It should contain 10 or 11 digits."; return false}

 // check first digit is 0
 exp = /^0[0-9]{9,10}$/
 if (exp.test(telnum)!=true) {telErr="Please enter a UK phone number. \n It should start with a 0."; return false}

 // Finally check that the telephone number is appropriate.
 exp = /^(01|02|05|070|077|078|079)[0-9]+$/;
 if (exp.test(telnum)!=true) {telErr="Please enter a valid UK phone number."; return false}

 return telnum
}

function errorAction(obj,msg){
 if (msg!=""){
 alert (msg)
 obj.style.backgroundColor="#fee"
 obj.onblur = function() {this.style.backgroundColor="#eee"}
 obj.focus(obj.select())
 return false
 }
 return true
}

function progressMeter(){
 if (idExists('vacancies') && idExists('container1')){
 var obj=document.getElementById("container1")
 intWidth=parseInt(obj.style.width)+10
 obj.style.width=(intWidth<=400)?intWidth+"px":"0px"
 setTimeout("progressMeter()",300);
} }

function validationsetup(){
 // reservation form
 if (idExists('reservations') && idExists('booking')){
 document.getElementById('booking').onsubmit=function(){
 var obj=document.getElementById('name')
 if (isEmpty(obj.value)) return errorAction(obj,'Please enter you name.'+alternate)
 var obj=document.getElementById('telephone')
 if (! isTelno(obj.value)) return errorAction(obj,telErr)
 var obj=document.getElementById('email')
 if (! isValidEmail(obj.value)) return errorAction(obj,'Please enter your email address.'+alternate)
 var obj=document.getElementById('guests')
 if (! isInteger(obj.value) || (obj.value>140) || (obj.value=="0")) return errorAction(obj,'Please enter the approximate number of guests in your party (maximum 140).'+alternate)
 var obj=document.getElementById('reservationDate')
 if (isEmpty(obj.value)) return errorAction(obj,'Please enter the reservation date you require.'+alternate)
 var obj=document.getElementById('message')
 if (obj.value===obj.defaultValue) obj.value=""
 return true
 }
 document.getElementById('arrival').defaultValue="eg: 8:00pm"
 document.getElementById('arrival').onfocus=function(){if (this.value==this.defaultValue) this.value=""}
 document.getElementById('arrival').onblur=function(){if (this.value=="") this.value=this.defaultValue}

 document.getElementById('message').defaultValue="eg: office party, christmas do, birthday, engagement, hen night, wedding. Please supply any details you can."
 document.getElementById('message').onfocus=function(){if (this.value==this.defaultValue) this.value=""}
 document.getElementById('message').onblur=function(){if (this.value=="") this.value=this.defaultValue}
 }
 // contact form
 if (idExists('contact') && idExists('enquiry')){
 document.getElementById('enquiry').onsubmit=function(){
 var obj=document.getElementById('name')
 if (isEmpty(obj.value)) return errorAction(obj,'Please enter you name.'+alternate)
 var obj=document.getElementById('telephone')
 if (! isTelno(obj.value)) return errorAction(obj,telErr)
 var obj=document.getElementById('email')
 if (! isValidEmail(obj.value)) return errorAction(obj,'Please enter your email address.'+alternate)
 return true
 } }
 // vacancy form
 if (idExists('vacancies') && idExists('vacancy')){
 document.getElementById('vacancy').onsubmit=function(){
 var obj=document.getElementById('name')
 if (isEmpty(obj.value)) return errorAction(obj,'Please enter you name.'+alternate)
 var obj=document.getElementById('telephone')
 if (! isTelno(obj.value)) return errorAction(obj,telErr)
 var obj=document.getElementById('email')
 if (! isValidEmail(obj.value)) return errorAction(obj,'Please enter your email address.'+alternate)
 var obj=document.getElementById('age')
 if (isEmpty(obj.value)) return errorAction(obj,'Please enter your age.'+alternate)

 var progressContent='<p>Loading...</p><div id="container1" style="width:0; height:10px; background:#156; margin:0; text-align: left;"></div><p>Please be patient while your CV is attached.</p>'
 replaceContent('attachmentMeter','<p>Attaching your CV. Please wait...</p><div id="container1" style="width:0px; height:5px; background:#156; margin-top:0px; text-align: left;"></div>')
 progressMeter()

 return true
 } }
}

var telErr="Please enter a UK telephone number."
var alternate=" \n Alternatively call us on the number provided."

addEvent(validationsetup)

/* End of form functions; setup and validation of vacancy, enquiry and reservation forms */
