// JavaScript Document

var url;
var imageName;

var index; 


var imageDetails = new Object();
imageDetails.imageArray = new Array();
imageDetails.imageUrlArray = new Array();


var xmlHttp = createXmlHttpRequestObject();

imageDetails.imageArray[0] = "bandFunkClubHouse.jpg";
imageDetails.imageArray[1] = "bandJunkadelic.jpg";
imageDetails.imageArray[2] = "bandJunkaEarlierYears.jpg";
imageDetails.imageArray[3] = "bandJunkaFloats.jpg";
imageDetails.imageArray[4] = "bandJunkaHedgeBandits.jpg";
imageDetails.imageArray[5] = "bandJunkaKids.jpg";
imageDetails.imageArray[6] = "bandJunkaLocalEvents.jpg";
imageDetails.imageArray[7] = "bandJunkaTouring.jpg";
imageDetails.imageArray[8] = "bandOdetteMercy.jpg";
imageDetails.imageArray[9] = "bandRandaSoul.jpg";
imageDetails.imageArray[10] = "bandRumskull.jpg";
imageDetails.imageArray[11] = "bandSpecialBrew.jpg";
imageDetails.imageArray[12] = "servBandBookings.jpg";
imageDetails.imageArray[13] = "servCarnivalFunk.jpg";
imageDetails.imageArray[14] = "servCorporateEvents.jpg";
imageDetails.imageArray[15] = "servEducation.jpg";
imageDetails.imageArray[16] = "servPromotionsArt.jpg";
imageDetails.imageArray[17] = "servTouringActs.jpg.jpg";


imageDetails.imageUrlArray[0] = "bandPages/funkClubHouseBand.htm";
imageDetails.imageUrlArray[1] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[2] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[3] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[4] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[5] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[6] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[7] = "bandPages/junkadelic.htm";
imageDetails.imageUrlArray[8] = "bandPages/odetteMercy.htm";
imageDetails.imageUrlArray[9] = "bandPages/randaSoulKingdom.htm";
imageDetails.imageUrlArray[10] = "bandPages/rumskull.htm";
imageDetails.imageUrlArray[11] = "bandPages/specialBrew.htm";
imageDetails.imageUrlArray[12] = "servicePages/bandBookings.htm";
imageDetails.imageUrlArray[13] = "servicePages/carnivaleDeFunk.htm";
imageDetails.imageUrlArray[14] = "servicePages/corporateEvents.htm";
imageDetails.imageUrlArray[15] = "servicePages/education.htm";
imageDetails.imageUrlArray[16] = "servicePages/promotionsArt.htm";
imageDetails.imageUrlArray[17] = "servicePages/touringActs.htm";

// creates the XMLHttpRequest object 
function createXmlHttpRequestObject()  
{  
  // will store the reference to the XMLHttpRequest object 
  var xmlHttp; 
  // if running Internet Explorer 
  if(window.ActiveXObject) 
  { 
    try 
    { 
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (e)  
    { 
      xmlHttp = false; 
    } 
  } 
  // if running Mozilla or other browsers 
  else 
  { 
    try  
    { 
      xmlHttp = new XMLHttpRequest(); 
    } 
    catch (e)  
    { 
      xmlHttp = false; 
    } 
  } 
// return the created object or display an error message 
  if (!xmlHttp) 
  {
  alert("Error!"); 
  }
  else  
  {
    return xmlHttp; 
  }
} 
 

function sendEmail() 
{ 

	   name = document.getElementById("name").value;
	   email = document.getElementById("email").value;
	   
	   checkEmail = validateEmail(email);
	   
	   
	   if(checkEmail)
	   {
		   	   url = 'sendmail.php';
	   
			   postDetails = "name="+name+"&email="+email;
			
			  try
			  {
				  document.getElementById("form").innerHTML = "Sending Mail";
				  
			  // proceed only if the xmlHttp object isn't busy 
				if(xmlHttp.readyState == 4 || xmlHttp.readyState ==0)
				{	    
					xmlHttp.open("POST",url,true);
					xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					xmlHttp.setRequestHeader("Content-length", postDetails.length);
					xmlHttp.setRequestHeader("Connection", "close");
					xmlHttp.onreadystatechange = handleSendEmailResult;	 
					xmlHttp.send(postDetails);
				}
				  
				  
			  }
			  catch(e)
			  {
				  
				 
			  }
		   
	   }
	   else
	   {
			alert("Email Not Valid");    
		   
	   }
	
	  
  return false;
  
} 


function handleSendEmailResult()
{
	
	if(xmlHttp.readyState == 4 && xmlHttp.status==200)
	{
	
		
		result = String(xmlHttp.responseText);
		
		result = trim(result);
		
	
		
		if(result == "success")
		{
			document.getElementById("form").innerHTML = "Your subscription to our mailing list has been sent.";
		}
		else
		{
			document.getElementById("form").innerHTML = "Your subscription to our mailing list has failed<br>";
			
		}
	}
	
}




function randomImageGenerator()
{
	
	index = Math.random();
	
	arrayLength = imageDetails.imageArray.length;
	
	index = Math.round(index * arrayLength);
	
	while(index > arrayLength)
	{
		index = Math.random();
	
		index = Math.round(index * arrayLength);
		
	}
	
		
	if(index <= arrayLength && index !=0)
	{
		
	   index= index-1;	
	   document.getElementById("bandUrl").href=imageDetails.imageUrlArray[index];
	   document.getElementById("bandImages").src="photos/home/"+imageDetails.imageArray[index];
	   //alert(imageDetails.imageArray[index]+ " "+imageDetails.imageArray[index]);
	}
	else if(index==0)
	{
		document.getElementById("bandUrl").href=imageDetails.imageUrlArray[0];
	    document.getElementById("bandImages").src="photos/home/"+imageDetails.imageArray[0];
		//alert(imageDetails.imageArray[0] + " "+imageDetails.imageUrlArray[0]);
		
	}
	
	
	
	
	
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function validateEmail(elementValue)
{
  //  elementValue = document.getElementById("email").value;
										  
	
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   result = emailPattern.test(elementValue); 
   
   
   
   return result;
   
}

