// JavaScript Document

//popup the preview window with the theUrl image preloaded
function popup(theUrl, imgwidth, textInfo, title, w, h) 
	{ var MyWin; 
  	   //if no image width is specified, use a default width
	   var imgw;
	   
	   if (imgwidth==null)
	   {	imgw=650;
	   
	   }	 
	   else
	   {	imgw=imgwidth;
	   }
	//if the no width specified, assume 700px
	  if (w==null)
  	  {	width=700;
	  }
	  else
	  {	width=w;
	  }
	  //if no height is specified assume 500px
  	 if (h==null)
  	  {	height=600;
	  }
	  else
	  {	height=h;
	  }
	  var text="";
	  if (textInfo == null)
	  {
	  }
	  else
	  {	text = textInfo;
	  }
	  //if not title is provided, use the "preview"
	  var t;
	  if (title==null)
	  	{	t = "preview";
			
	  	}
	  else
	  	{	t = title;
		}
	
	  MyWin = window.open("preview.asp?img="+theUrl+"&title="+t+"&info="+text+"&imgwidth="+imgw,"preview","width="+width+",height="+height+",top=0, left=50");
  	}


  
 //validate that the time is inserted in correct format
function checkTimeFormat(textbox)
  {	//if the field is empty return imidiatly
  	if (textbox.value=="")
		return;
  	var text, errorMSG, formatError;
    var allowedChars= "0123456789:"
  	errorMSG = "Wrong time format. Please insert time in this format: hh:mm, ex: 9:50. \nChoose Cancel if you don't wish to specify a time.";
	formatError = false;
  	text = textbox.value;
	
	//check the existance of ":"
 	if (text.substr(1,1)==":")
		{ text = "0" + text;
		}
	if (text.substr(text.length-2,1)==":")
		{ text = text.substr(0,text.length-1) + "0" + text.substr(text.length-1,1);
		}	
	//check the length of the time
	if (text.length!=5)
		{ formatError = true;
		}
	//check that there are only numbers
	for (i=0;i<text.length;i++)
	{	if (allowedChars.indexOf(text.substr(i,1)) == -1)
			{ formatError = true;	
			  break;
			}
	}
	if (formatError)
		{ if (confirm(errorMSG))
		  	{ textbox.focus();
		  	}
		  else
		  	{ textbox.value="";
			}
		}
	else
		{ textbox.value = text;
		}	
	
  }
  
  function checkEmailFormat(email)
  {
	  var errorExisted = false
	  //check for existance of @ and that there is at least one character before it
	  if (email.value.indexOf('@') < 1 ) errorExisted = true;
	  //check for existancer of . after @ and that . is not the last character
	  if (email.value.indexOf('.') < 0 || email.value.indexOf('.') <= email.value.indexOf("@")+1 || email.value.indexOf('.') == email.value.length - 1) errorExisted=true;	  
	  
	  if (errorExisted)
	  {	
	  	if (confirm("Wrong Email Format. If you do not wish to provide an email, please click on Cancel."))
			email.focus();
		else
			email.value="";
	  }
  }
  
  //validate the form field for appointments  
  function validateAppointmentForm(form)
  	{	var warning, valid;
		valid = true;
		warning = "Please fix the following:\n";
		//check that name is provided
		if (form.name.value == "")
			{	warning += "-- No Name is provided \n";
				valid = false;
			}
		//check that atleast one of telephone or email is provided
		if (form.telephone.value == "" & form.email.value == "")
			{	warning += "-- No Email or Telephone is provided \n";
				valid = false;
			}
		//check that month for the appointment is provided
		if (form.month.value == "" )
			{	warning += "-- No Month for appointment is specified \n";
				valid = false;
			}
		//check that day for appointment is provided
		if (form.day.value == "")
			{	warning += "-- No Day for appointment is specified \n";
				valid = false;
			}
		if (!valid) 
			{	alert(warning);
				return false;
			}
		else
			{	return true;
			}
	}
  
  
