var hommegaPlayer;

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
}

function checkEmail(str) {
///// function for validating email address
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){
		    return false
		} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		    return false
		} else 	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		} else  if (str.indexOf(at,(lat+1))!=-1){
		    return false
		} else 	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   return false
		} else  if (str.indexOf(dot,(lat+2))==-1){
		    return false
		} else if (str.indexOf(" ")!=-1){
		     return false
		} else {
 		 	return true
 		}
}

function checkContactSub(){
	if (document.contactForm.contact_fullName.value==""){
		alert ("Please Enter Your Full Name");
		document.contactForm.contact_fullName.select();
		document.contactForm.contact_fullName.focus();
		return false;
	} else if (document.contactForm.contact_email.value==""){
		alert ("Please Enter Your Email Address");
		document.contactForm.contact_email.select();
		document.contactForm.contact_email.focus();
		return false;
	 } else if (!checkEmail(document.contactForm.contact_email.value)){
	 	alert ("Email is not valid");
	 	document.contactForm.contact_email.select();
		document.contactForm.contact_email.focus();
	 	return false;
	 } else if (document.contactForm.contact_content.value==""){
		alert ("No Email Content!");
		document.contactForm.contact_content.focus();
		return false;
	} else {
		return confirm ("OK to send email to HOMmega Productions?");
	}
}

function tagOut(tagID){
	document.getElementById(tagID+"_td").className="menu_off";	
	document.getElementById(tagID+"_pic").src="images/artistTag_"+tagID+".jpg";
}
function tagOver(tagID){
	document.getElementById(tagID+"_td").className="menu_on";	
	document.getElementById(tagID+"_pic").src="images/artistTag_"+tagID+"_over.jpg";
}

function openPlayer(){
	if (hommegaPlayer){
		hommegaPlayer.close();
	}
	attributes="height=187, width=483, scrollbars=no,status=no, toolbar=no,titlebar=no,location=no,resizable=no,menubar=no,alwaysRaised=yes,directories=no,hotkeys=no,dependent=yes";
	hommegaPlayer=window.open("wimpy/myWimpy.html","hommegaPlayer", attributes);
}

function playAlbum(albumID){
	if (hommegaPlayer){
		hommegaPlayer.close();
	}
	attributes="height=187, width=483, scrollbars=no,status=no, toolbar=no,titlebar=no,location=no,resizable=no,menubar=no,alwaysRaised=yes,directories=no,hotkeys=no,dependent=yes";
	hommegaPlayer=window.open("albumsWimpy/myWimpy.php?queryWhere=album&queryValue="+albumID,"hommegaPlayer", attributes);
}

function playRingtone(ringName){
	if (hommegaPlayer){
		hommegaPlayer.close();
	}
	attributes="height=187, width=483, scrollbars=no,status=no, toolbar=no,titlebar=no,location=no,resizable=no,menubar=no,alwaysRaised=yes,directories=no,hotkeys=no,dependent=yes";
	hommegaPlayer=window.open("mobile/ringtones/myWimpy.php?ringName="+ringName,"hommegaPlayer", attributes);
}

function changeBookingForm(){
	curArtist=document.bookingForm.artistSelect.value;
	curType=document.bookingForm.showType.value;
	
	if (curArtist!=""){
		for (i=0; i < artists.length; i++){
			if (artists[i]['artistID']==curArtist){
				document.bookingForm.numPeople.value=artists[i]['flightNum'];
				var curArtistNum=i;
			}
		}
	} else {
		document.bookingForm.numPeople.value="";
	}
	if (curType!="" && curArtist!=""){
		curType="showTime_"+curType;
		document.bookingForm.showDuration.value=artists[curArtistNum][curType];
	} else {
		document.bookingForm.showDuration.value="";
	}
}

function subBookingForm(){
	// check booking form before submitting
	subError=false;
	if (document.bookingForm.artistSelect.value==""){
		alert ("Please select artist");
		document.bookingForm.artistSelect.focus();
		return false;
	} else if (document.bookingForm.dateDay.value=="" ||	 document.bookingForm.dateMonth.value=="" ||	document.bookingForm.dateYear.value==""){
		alert ("Please enter valid date for event");
		document.bookingForm.dateDay.focus();
		return false;
	} else if (document.bookingForm.showType.value==""){
		alert ("Please choose desired show type");
		document.bookingForm.showType.focus();
		return false;
	} else if (document.bookingForm.eventCountry.value==""){
		alert ("Please choose event country");
		document.bookingForm.eventCountry.focus();
		return false;
	} else if (document.bookingForm.eventCity.value==""){
		alert ("Please choose event city");
		document.bookingForm.eventCity.focus();
		return false;
	} else if (document.bookingForm.yourName.value==""){
		alert ("Please enter your full name");
		document.bookingForm.yourName.focus();
		return false;
	} else if (document.bookingForm.telephone.value=="" && document.bookingForm.mobilePhone.value==""){
		alert ("Please enter telephone or mobile number");
		document.bookingForm.telephone.focus();
		return false;
	} else if (document.bookingForm.yourEmail.value==""){
		alert ("Please enter your email");
		document.bookingForm.yourEmail.focus();
		return false;
	} else if (!checkEmail(document.bookingForm.yourEmail.value)){
		alert ("Email is not a valid email");
		document.bookingForm.yourEmail.focus();
		return false;
	} else {
		return confirm("OK to submit booking request to HOMMEGA?");
	}
}

function showPic(artistID, picID, picType){
	attributes="height=500, width=750, scrollbars=no, toolbar=no, titlebar=no, location=no, resizable=no, menubar=no, alwaysRaised=yes, directories=no, hotkeys=no, dependent=yes";
	window.open("picDisplay.php?artistID="+artistID+"&picID="+picID+"&picType="+picType, "PictureDisplay", attributes);
}

function viewCover(picPath){
	attributes="height=500, width=500, scrollbars=no, toolbar=no, titlebar=no, location=no, resizable=no, menubar=no, alwaysRaised=yes, directories=no, hotkeys=no, dependent=yes";
	window.open("coverDisplay.php?coverPath="+picPath, "CoverDisplay", attributes);
}




function submit_joinML(f){
	
	var email_field = f.email_field;
	var confirm_field = f.confirm_field;
	var country_field = f.country_field;
	
	var _joinML_empty = "Please enter your email address";
	var _joinML_invalid = "The email address you provided is invalid";
	var _joinML_country = "Please select your country";
	var _joinML_check = "Please check the confirmation box for receiving updates from HOMmege.";
	var _joinML_confirm = "Thank you for joining our newsletters group!";
	
	if (email_field.value==""){
	 	alert (_joinML_empty);
	 	email_field.focus();
	} else if (!checkEmail(email_field.value)){
	 	alert (_joinML_invalid);
	 	email_field.focus();
	} else if (country_field.value == ""){
	 	alert (_joinML_country);
	 	country_field.focus();
	} else if (confirm_field.checked == false) {
		alert (_joinML_check);
	 	email_field.focus();
	} else {
		var url = "tyco_joinML.ajax.php?joinML_email="+email_field.value+"&joinML_country="+country_field.value;
		var xml = LoadXML(url);
		if(xml != null)
		{
			var response = xml.getElementsByTagName('rsp_stat')[0].firstChild.data;
			if (response=="ok")
			{
				email_field.value="";
				country_field.selectedIndex = 0;
				alert (_joinML_confirm);
			}
		}
	}
	return false;
}


function getHTTPObject()
{
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
 }

function LoadXML(url)
{
	var xmlHttp = getHTTPObject();
	xmlHttp.open("GET",url, false);
	xmlHttp.onreadystatechange = function()
	{
		   if (xmlHttp.readyState != 4)  { return; }
		   var serverResponse = xmlHttp.responseText;
	};
	xmlHttp.send(null);
	return xmlHttp.responseXML.documentElement;
}