////////////////////////////////////////////
// Persimmon Creek Javascript Functions.js //



/////////////////////////
// creates external links
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;


/////////////////////////////////////
// url encode
function urlencode(str) {
return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}
//////////////////////////////////////
// function for submitting form
function submit_form (theForm, serverPage, theDiv ) {
	var str = ''
//	alert(theForm+' '+serverPage+' '+theDiv);
	sub = document.getElementById('sub').value;
//	sub_label = document.getElementById('sub_label');

	name =  document.getElementById('name');
	name_value = document.getElementById('name').value;
//	name_label =  document.getElementById('name_label');
	
	pet = document.getElementById('name_pet');
	pet_value = document.getElementById('name_pet').value;
//	pet_label = document.getElementById('name_pet_label');
	
	phone = document.getElementById('phone');
	phone_value = document.getElementById('phone').value;
//	phone_label = document.getElementById('phone_label');

	cell = document.getElementById('cell');
	cell_value = document.getElementById('cell').value;
//	cell_label = document.getElementById('email_label');
	
	email = document.getElementById('email');
	email_value =  document.getElementById('email').value;
//	email_label =  document.getElementById('email_label');
	//alert (email);

	contact = document.getElementById('contactvia').value;
	remark = document.getElementById('remark').value;

	if (name_value == '') {
	  document.getElementById('name_label').style.color = '#ff0000';
	  
	  alert ('Please enter your name.');
	  name.focus();
	  return false; 
	  } else {
	    document.getElementById('name_label').style.color = '#666600';
	  //  if ((isPhoneNumber (phone)) ) {
	
	
	
	if ((phone_value==null)||(phone_value=="")){
		alert("Please Enter your Phone Number (555) 555-1234");
		document.getElementById('phone_label').style.color = '#ff0000';
	
		phone.focus();
		return false;
	}
	if (checkInternationalPhone(phone_value)==false){
		alert("Please Enter a Valid Phone Number (555) 555-1234");
		document.getElementById('phone_label').style.color = '#ff0000';
		phone.select();
		phone.focus();
		return false;
	}
	else { document.getElementById('phone_label').style.color = '#666600'; }
	
  	
	        if (echeck(email_value)) {
		  // a bit of clean up, I think echeck is changing the variable email from the value created earlier and the actual object reference
	       	  email = email_value;
		//	alert (email);
		  str = "_submit_ajax=1&_submit_check=true";
		  str += "&sub="+urlencode (sub);
		  str += "&name="+urlencode (name_value);
		  str += "&pet="+urlencode (pet_value);
		  str += "&phone="+urlencode( phone_value)+"&cell="+urlencode(cell_value);
		  str += "&email="+urlencode (email_value)+"&contactvia="+urlencode(contact)+"&remark="+ urlencode (remark);
//		alert(str);
		  processajax (serverPage, theDiv, 'post', str);
		
		} else { return false; }
	     }
	    
	

}



////////////////////////////
// validate phone number

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- .";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) {//alert(c+' '+i);
		return false;}
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3;
strPhone=trim(strPhone);
//alert (strPhone);
if(strPhone.indexOf("+")>1) {return false; }
if(strPhone.indexOf("-")!=-1){
  bracket=bracket+1; 
  theIndex = strPhone.indexOf("(");
  //alert("bracket: "+bracket+ ' theIndex: '+theIndex);
  }
if(strPhone.indexOf("(")!=-1) //{alert ("-1");} 
if ( strPhone.indexOf("(") > bracket)
 {//alert (strPhone.indexOf("(") +" > "+ bracket);
  return false;}
var brchr=strPhone.indexOf("(");
//alert ("brchr: "+brchr);
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+4)!=")")return false;
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false;
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		//email_label = document.getElementById('email_label');
		email = document.getElementById('email');
		if (str.indexOf(at)==-1){
		   document.getElementById('email_label').style.color = '#ff0000';

		   alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   document.getElementById('email_label').style.color = '#ff0000';

		   alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   document.getElementById('email_label').style.color = '#ff0000';

		    alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    document.getElementById('email_label').style.color = '#ff0000';

		    alert("Invalid Email address, please enter an email in the form of yourname@domain.com.");
		   email.focus();
		   email.select();
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   document.getElementById('email_label').style.color = '#ff0000';
		   email.focus();
		   email.select();
		    alert("Invalid Email address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    document.getElementById('email_label').style.color = '#ff0000';
		    alert("Invalid Email address");
		   email.focus();
		   email.select();
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    document.getElementById('email_label').style.color = '#ff0000';
		    alert("Invalid Email address");
		   email.focus();
		   email.select();
		    return false;
		 } else {
		email_label.style.color = '#666600';
 		 return true;
		}					
	}


//////////////////////////////
function updateImage (theURL, theFrame) {
	//alert ('here');
	send = 1;
	setStatus ("loading...", theFrame);
	//alert (theURL + theFrame);
	//theURL = '../'+theURL;	
	
	if (theFrame == 'thumbmenu') {

	  document.getElementById('minimage').innerHTML = 'Select a thumbnail to see a larger image.';
	//	minimage = document.getElementById('minimage');
	//	minimage.innerHTML = '';
	//"<p>Select a Thumbnail to see a larger image.</p>";
	//alert ('test');	
		
	}
	if (theFrame == 'floatContent') {
		document.getElementById('floatDiv').style.display = 'block';
		document.getElementById('transparentDiv').style.display = 'block';
                //setTimeout("fadeImage(1)", 8000);
		ajax_slideshow (theURL, theFrame, 'post', '');
		send = 0;
	}
	if (send == 1) {
             processajax (theURL, theFrame, "post", "");
	//document.all.galleryimage.src = theURL;
	}
}
function backTwoIt (theURL, theFrame) {
	//theURL = '../'+theURL;
	//alert (theURL + theFrame);
	//document.getElementById(theFrame).innerHTML = '<div style = "position: absolute; margin-top:0px; margin-left: 0px; background-color: #000000; width: 100%; height: 100%;">Choose the gallery on the left, then choose the image above. <br />Knikkolette accepts visa and mastercard payments through Propay. You can contact her at <a href = "mailto:art@knikkolette.com">art@knikkolette.com</a>.</div>';
	processajax (theURL, theFrame, "post", "");
}

function updateNav(theURL, theFrame) {
	theURL = '../'+theURL;
	//alert (theURL+' '+theFrame);
	processajax (theURL, theFrame, "post", "");
}


function setStatus(theStatus, theObj)
	{
	obj = document.getElementById(theObj);
	if (obj)
		{
		obj.innerHTML = "<div id = \"status\" class = \"status\"><img src = \"images/loading.gif\" alt = \""+theStatus+"\" width = \"16px\" height = \"16px\" />" + theStatus + "</div>";

		}
	return;
	}

////////////////////////////////////

/////////////////////////////////////
// slide show 
// hideFloat
function hideFloat () {
    document.getElementById('transparentDiv').style.display = "none";
    content = document.getElementById('floatDiv');
    document.getElementById('floatContent').innerHTML = '';
    content.style.display = "none";
    
}


/////////////////////////////////////////////////////////////////////
// calendar functions
	//A variable used to distinguish whether to open or close the calendar.
	var showOrHide = true;
	
	function showHideCalendar() {
		//alert ('here');
		//The location we are loading the page into.
		var objID = "calendar";
		
		//Change the current image of the minus or plus.
		if (showOrHide == true){
			//Show the calendar.
			document.getElementById('cal_link').innerHTML = "Hide Calendar";
                        document.getElementById(objID).style.backgroundColor = "#ffffff";
			//The page we are loading.
			var serverPage = "calendar.php";
			//Set the open close tracker variable.
			showOrHide = false;
			var obj = objID;
			processajax (serverPage, obj, "get", "");
		} else {
			//Hide the calendar.
			document.getElementById(objID).innerHTML = "";
		        document.getElementById(objID).style.backgroundColor = "#778899";
			document.getElementById('cal_link').innerHTML = "Show Calendar";
			showOrHide = true;
			//Reset the content.
			
		}
		
		
	}

	function changemonth (month, year) {
		// advance or reverse month on calendar
		//alert ('year: '+year+' month: '+month);
		var objID = "calendar";
		var serverPage = "calendar.php?year="+year+"&&month="+month;
		var obj = objID;
		processajax (serverPage, obj, "get", "");
		}
	


///////////////////////
//  set_date from calendar
  	function set_date (theDate) {
          document.getElementById('pbday').value = theDate;
          showHideCalendar();
 	}
// end calendar functions
///////////////////////////////////////////////////////////////////////////////
///////////////////////
// toggles row highlighting on the items form
function togglerow (theID1, theID2, on_off) { 
           if (on_off == 'on') {
	     onmouseover = theID1.style.backgroundColor = '#EBECE4';
	     document.getElementById(theID2).style.backgroundColor = '#EBECE4';
          } 
	  if (on_off == 'off') {
	    onmouseout = theID1.style.backgroundColor = '';
	    document.getElementById(theID2).style.backgroundColor = '';
 	  }
	}	