// JavaScript Document

function openWin(url, name, width, height, left, top) {
	myWindow = window.open(url, name, 'width='+width+',height='+height+',left='+left+',top='+top); 
}



function validateAddContact(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].address.value)){
    valid_form = false;
	valid_msg += "Address \n";
  }
  if(!trim(document.forms[form_name].phone.value)){
    valid_form = false;
	valid_msg += "Phone \n";
  }
 
  if(!validateEmail(trim(document.forms[form_name].email.value),1,1)){
    valid_form = false;
	valid_msg += "Email \n";
  }
  
  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}



function validateUserEditForm(form_name){

  var valid_form = true;
  var valid_msg = "Fields requiring a value:       \n\n";

    if(!trim(document.forms[form_name].password.value)){
    valid_form = false;
	valid_msg += "Password     \n";
  }
  if(!trim(document.forms[form_name].repassword.value)){
    valid_form = false;
	valid_msg += "Confirmation Password    \n";
  }
  if(document.forms[form_name].repassword.value !== document.forms[form_name].password.value){
    valid_form = false;
	valid_msg += "Passwords don't match    \n";
  }

  if(valid_form){
    //window.alert(valid_form);
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}

function validateAddYachtForm(form_name){
	//window.alert('here here!');
	//document.forms[form_name].submit();

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].dayrate.value)){
    valid_form = false;
	valid_msg += "Day Rate \n";
  }
  if(!trim(document.forms[form_name].guests.value)){
    valid_form = false;
	valid_msg += "Guests \n";
  }
  if(!trim(document.forms[form_name].cabins.value)){
    valid_form = false;
	valid_msg += "Cabins \n";
  } 
  if(!trim(document.forms[form_name].length.value)){
    valid_form = false;
	valid_msg += "Length (*short) \n";
  }
  if(valid_form){
    //window.alert('you got it!');
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}

function validateEditYachtForm(form_name){
	//window.alert('here here!');
	//document.forms[form_name].submit();

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].name.value)){
    valid_form = false;
	valid_msg += "Name \n";
  }
  if(!trim(document.forms[form_name].dayrate.value)){
    valid_form = false;
	valid_msg += "Day Rate \n";
  }
  if(!trim(document.forms[form_name].guests.value)){
    valid_form = false;
	valid_msg += "Guests \n";
  }
  if(!trim(document.forms[form_name].cabins.value)){
    valid_form = false;
	valid_msg += "Cabins \n";
  } 
  if(!trim(document.forms[form_name].length.value)){
    valid_form = false;
	valid_msg += "Length (*short) \n";
  }
  if(valid_form){
    //window.alert('you got it!');
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}



function validateAddVillaForm(form_name){
	//window.alert('here here!');
	//document.forms[form_name].submit();

  var valid_form = true;
  var valid_msg = "Fields requiring attention:       \n\n";
  
  if(!trim(document.forms[form_name].location.value)){
    valid_form = false;
	valid_msg += "Location \n";
  }
  if(!trim(document.forms[form_name].description.value)){
    valid_form = false;
	valid_msg += "Description \n";
  }
  if(valid_form){
    //window.alert('you got it!');
    document.forms[form_name].submit();
  }else{
  //document.forms.signup.submit();
    window.alert(valid_msg);
  }

}

function validateSendToFriendForm(form_name){
  var valid_form = true;
  var valid_msg = "Email addresses must be valid.\n\nFields requiring attention:       \n";
  
  if(!validateEmail(document.forms[form_name].to_email.value)){
    valid_form = false;
	valid_msg += "\tTo: \n";
  }
  if(!validateEmail(document.forms[form_name].from_email.value)){
    valid_form = false;
	valid_msg += "\tFrom: \n";
  }
  if(valid_form){
    document.forms[form_name].submit();
  }else{
    window.alert(valid_msg);
  }

}

function showTableInfo(section){
	
	if(document.getElementById){
	
		// turn all off
		document.getElementById('home_table').style.display='none';
		document.getElementById('aboutus_table').style.display='none';
		document.getElementById('yachts_table').style.display='none';
		document.getElementById('cruises_table').style.display='none';
		document.getElementById('itineraries_table').style.display='none';
		document.getElementById('aboutgreece_table').style.display='none';
		document.getElementById('chartering_table').style.display='none';
		document.getElementById('freeinfo_table').style.display='none';
		document.getElementById('services_table').style.display='none';
		document.getElementById('links_table').style.display='none';
		document.getElementById('contact_table').style.display='none';
		
		// turn selected table on
		document.getElementById(section + '_table').style.display='block';

	}
	
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function



function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   //if (db) window.alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) window.alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) window.alert("Email address contains non ascii characters.");
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) window.alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) window.alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) window.alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) window.alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) window.alert('Period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) window.alert('Period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) window.alert('Two periods must not be adjacent in email address');
	   return false;
	}
	//var suffix = addr.substring(addr.lastIndexOf('.')+1);
	//if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	//   if (db) alert('invalid primary domain in email address');
	//   return false;
	//}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length < 2 ) {
	  if (db) window.alert('Invalid primary domain in email address');
	  return false;
	}
	return true;
}





// this section deals with disabling the right-click on images


var specialcase = ((navigator.userAgent.indexOf('Mac') != -1) || document.all)
var flag = 0;
var msg = 'This image is protected by copyright.\nWe request you not to copy it.';
var x,y,x1,y1,copyAttempt;

function init()
{

	if (!(document.getElementById || document.all || document.layers)) return;
	if (specialcase && document.layers)
	{
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = special;
	}
	for (i=0;i<document.images.length;i++)
	{
		document.images[i].onmousedown = checkIt;
		document.images[i].onmouseup = function() {return false};
		if (specialcase)
		{
			document.images[i].onmousemove = special;
			document.images[i].onclick = clearIt;
		}
	}
}

function checkIt(e)
{
	copyAttempt = 0;
	if (window.Event)
	{
		x = e.screenX;
		y = e.screenY;
		theButt = (e.which == 3);
	}
	else
	{
		x = window.event.clientX;
		y = window.event.clientY;
		theButt = (window.event.button == 2);
	}
	if (theButt)
	{
		copyAttempt = 1;
		flag = 0;
		alert(msg);
		return false; // NN4 only
	}
	if (specialcase) flag = 1;
	return false;
}

function special(e)
{
	theObj = '';
	if (window.Event)
	{
		x1 = e.screenX;
		y1 = e.screenY;
		if (e.target.parentNode) theObj = e.target.parentNode.tagName;
	}
	else
	{
		x1 = window.event.clientX;
		y1 = window.event.clientY;
		theObj = window.event.srcElement.parentElement.tagName;
	}
	var isLink = (theObj == 'A');
	if (flag && (!isLink || ((Math.abs(x-x1) > 10) || (Math.abs(y-y1) > 10))))
	{
		copyAttempt = 1;
		flag = 0;
		alert(msg);
		return false;
	}
}

function clearIt()
{
	flag = 0;
	if (copyAttempt)
	{
		copyAttempt = 0;
		return false;
	}
}

 

function addBookmark(title, url) {
    if (window.sidebar) { // firefox
          window.sidebar.addPanel(title, url, "");
    } else if( document.all ) { //MSIE
            window.external.AddFavorite( url, title);
    } else {
           alert("Sorry, your browser doesn't support this");
    }
}


