var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function openNewsletterSignup() {
	var newWin;
	newWin = window.open('http://www.patronmail.com/pmailweb/PatronSetup?oid=1465','patron_signup','height=560,width=640,scrollBars=1,resizable=0');
	newWin.focus();
}

function openRightImage(file, w, h) {
	var newWin;
	var l = (screen.width-w)/2;
  	var t = (screen.height-h)/2;
  	if (l < 0) l = 0;
  	if (t < 0) t = 0;
	newWin = window.open('/showImage.asp?file='+file, '', 'height='+h+',width='+w+',top='+t+',left='+l);
}

function forgotPW() {
	var elHeader = document.getElementById('forgotHeader');
	var el = document.getElementById('forgotForm');
	
	if (el) {
		if (el.style.display == '') {
			el.style.display = 'none';
			elHeader.innerHTML = '<a href="javascript:forgotPW()">Forgot your password?</a>';
		} else {
			el.style.display = '';
			elHeader.innerHTML = '<strong>Forgot your password?</strong>';		
		}
	}
}
function sendPW() {
	var xmlObj = false;
	var elForm = document.getElementById('forgotForm');
	var elResults = document.getElementById('forgotResults');
	
	if (elForm) {
		elForm.style.display = 'none';
	}
	
	if (elResults) {
		elResults.style.display = '';
	}
		
	if (window.XMLHttpRequest) {
		xmlObj = new XMLHttpRequest();
	} else if (window.ActiveXObject) { 
		try {
			xmlObj = new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e){
			try{
				xmlObj = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e) {}
		}
	} else {
		return false;
	}
	
	xmlObj.onreadystatechange=function(){
		if (xmlObj.readyState == 4 && xmlObj.status == 200) {
			if (xmlObj.responseText == 'yes') {
				elResults.innerHTML = "The password has been sent to <strong>" + email + "</strong>.<p><a href='javascript:closePW()'>[Close]</a></p>";
			} else {
				elResults.innerHTML = "Sorry, but we could not find a record for <strong>" + email + "</strong>.<p><a href='javascript:closePW()'>[Close]</a></p>";			
			}
		}
	}
	
	var email = document.forms['frmForgot'].vEmail.value;
	
	if (email != '') {
		url = '/musicians/send.asp?email='+email+'&r='+Math.random();
		xmlObj.open('GET', url, true);
		xmlObj.send(null);
	} else {
		alert('Please enter your e-mail address');
	}
}	

function closePW() {
	var elHeader = document.getElementById('forgotHeader');
	var elForm = document.getElementById('forgotForm');
	var elResults = document.getElementById('forgotResults');
	
	if (elForm) {
		elForm.style.display = 'none';
	}
	if (elResults) {
		elResults.style.display = 'none';
	}
	elHeader.innerHTML = '<a href="javascript:forgotPW()">Forgot your password?</a>';

}

function submitHighlight(typeID) {
	document.forms['formType'].elements['laETypeID'].value = typeID;
	document.forms['formType'].submit();
}

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"))) return false;
	}
	// All characters are numbers.
	return true;
}

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++){   
	   var c = s.charAt(i);
	   if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this;
}

function isDate(dtStr) {
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
	return true;
}

function openCalendar(fieldToPopulate,valueToPrepopulate) {
	var cal = new calendar2(fieldToPopulate);
	cal.year_scroll = true;
	cal.time_comp = false;	
	cal.popup();
}

function confirmEventDelete(id) {
	if (confirm('Are you sure you want to remove this event?')) {
		window.location = 'delete-event.asp?id=' + id;	
	}
}

function showQuizAnswer(id, val) {
	var elA = document.getElementById('a'+id);
	
	if (elA) {
		elA.style.display = '';
		
		if (id == '1') {
			if (val == 'c') {
				elA.innerHTML = 'Correct!';
			} else {
				elA.innerHTML = 'Incorrect!  The correct answer is (c)';	
			}
		} else if (id == '2') {
			if (val == 'b') {
				elA.innerHTML = 'Correct!';
			} else {
				elA.innerHTML = 'Incorrect!  The correct answer is (b)';	
			}		
		} else if (id == '3') {
			if (val == 'c') {
				elA.innerHTML = 'Correct!';
			} else {
				elA.innerHTML = 'Incorrect!  The correct answer is (c)';	
			}			
		}
	}
}

function toggleMessageText(id) {
	var el = document.getElementById('message_' + id);
	
	if (el) {
		if (el.style.display == '') {
			el.style.display = 'none';
		} else {
			el.style.display = '';			
		}
	}
}

function resizeIframe() {
	i = parent.document.getElementById(window.name);
	iHeight = document.body.scrollHeight;
	i.style.height = iHeight + 5 + "px"
}

function submitToFlash(songName) {
	window.document.topSWF.SetVariable("mcPlayer.mcCalendarTrack.message", songName);	
}

function submitToParentFlash(songName) {
	parent.window.document.topSWF.SetVariable("mcPlayer.mcCalendarTrack.message", songName);	
}

//----------------------------------------------------
// form validation
//----------------------------------------------------

function calendarSetDefaults() {
	parent.window.document.forms['form1'].elements['d'].value = '';
	parent.window.document.forms['form1'].elements['laETypeID'].selectedIndex = 0;	
	parent.window.document.forms['form1'].elements['laMusicianID'].selectedIndex = 0;	
	parent.window.document.forms['form1'].elements['laVenueID'].selectedIndex = 0;	
	parent.window.document.forms['form1'].elements['iCityGroup'].selectedIndex = 0;		
}

function homeCalendarPrev() {
	parent.window.frames['iCalendarGrid'].document.location = '/calendar/grid.asp?type=prev';	
}

function homeCalendarNext() {
	parent.window.frames['iCalendarGrid'].document.location = '/calendar/grid.asp?type=next';		
}

function leftCalendarPrev() {
	parent.window.frames['iLeftCalendarGrid'].document.location = '/calendar/leftgrid.asp?type=prev';	
	parent.window.frames['iResults'].document.location = '/calendar/results.asp?type=prev';	
	calendarSetDefaults();	
}

function leftCalendarNext() {
	parent.window.frames['iLeftCalendarGrid'].document.location = '/calendar/leftgrid.asp?type=next';		
	parent.window.frames['iResults'].document.location = '/calendar/results.asp?type=next';	
	calendarSetDefaults();
}

function showDaysEvents(d) {
	calendarSetDefaults();
	parent.window.document.forms['form1'].elements['d'].value = d;		
	parent.window.frames['iResults'].document.location = '/calendar/results.asp?d='+d;		
}

function toggleEventDesc(id) {
	var eventID = document.getElementById('eventdesc_'+id);
	var eventLink = document.getElementById('eventlink_'+id);
	var eventVenue = document.getElementById('eventvenue_'+id);

	if (eventID) {
		if (eventID.style.display == '') {
			eventID.style.display = 'none';
			eventLink.innerHTML = 'Show details';
			resizeIframe();
		} else {
			eventID.style.display = '';	
			eventLink.innerHTML = 'Hide details';
			resizeIframe();
		}
	}
}

function Right(str, n) {
	if (n <= 0)     // Invalid bound, return blank string
	   return "";
	else if (n > String(str).length)   // Invalid bound, return
	   return str;                     // entire string
	else { // Valid bound, return appropriate substring
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
}

function empty(field) {
	if (field.type == 'text' || field.type == 'password' || field.type == 'textarea' || field.type == 'file') {
		if (field.value.replace(/\s*/g,'').length == 0) {
			return true;
		}
	} else if (field.type == 'select-one') {
		if (field.selectedIndex == 0) {
			return true;
		}
	} else if (field[0].type == 'radio') {
		var checked = false;
		for (i=0; i < field.length; i++) {
			if (field[i].checked) {
				checked = true;
			}
		}
		if (!checked) {
			return true;
		}
	}
	return false;
}

// based on script by Sandeep V. Tamhankar (stamhankar@hotmail.com) 
function verifyEmail(emailStr) {
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat= new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray = emailStr.match(emailPat);

	if (matchArray == null) { return false;}

	var user = matchArray[1]
	var domain = matchArray[2]
	if (user.match(userPat) == null) { return false; }

	var IPArray=domain.match(ipDomainPat)
	if (IPArray != null) {
		for (var i=1; i<=4; i++) {
	    		if (IPArray[i] > 255) {
	        		return false;
	    		}
    	}
   		return true;
	}

	var domainArray = domain.match(domainPat)
	if (domainArray == null) {
		return false;
	}

	var atomPat = new RegExp(atom,"g")
	var domArr = domain.match(atomPat)
	var len = domArr.length
	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) {
		return false;
	}

	if (len < 2) { return false; }
	
   	return true;
}

function confirmDeleteClip(clipID) {
	if (confirm("Are you sure you want to delete this clip?")) {
		window.location = '?type=clip&delete=' + clipID;
	}
}
function confirmDeletePhoto(photoID) {
	if (confirm("Are you sure you want to delete this clip?")) {
		window.location = '?type=photo&delete=' + photoID;
	}
}
function togglePhoto(photoID) {
	var linkEl = document.getElementById('link'+photoID);
	var imgEl = document.getElementById('img'+photoID);
	
	if (linkEl && imgEl) {
		if (imgEl.style.display == '') {
			imgEl.style.display = 'none';
			linkEl.innerHTML = 'Show photo';
		} else {
			imgEl.style.display = '';
			linkEl.innerHTML = 'Hide photo';		
		}
	}
}

//----------------------------------------------------
// preload images
//----------------------------------------------------

var imageRoot = '/images/';
var preloadFlag = false;

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && preloadFlag == true) {
		for (var i=0; i < changeImages.arguments.length; i += 2) {
			document[changeImages.arguments[i]].src = imageRoot + changeImages.arguments[i+1];
		}
	}
}

function preloadImages() {
	if (document.images) {
		home_venues_o = newImage(imageRoot + "bt_home_venues_o.gif");
		home_musicians_o = newImage(imageRoot + "bt_home_musicians_o.gif");
		home_organizations_o = newImage(imageRoot + "bt_home_organizations_o.gif");
		home_education_o = newImage(imageRoot + "bt_home_education_o.gif");
		home_radio_o = newImage(imageRoot + "bt_home_radio_o.gif");

		preloadFlag = true;
	}
}

//----------------------------------------------------
// function for Flash navigation
//----------------------------------------------------

function getLocation() {
	var loc = window.location.pathname;
	var arrURL = loc.split("/");
  
  	if (loc == '/') {
		return "home";	
	} else if (arrURL.length == 2) {
		if (arrURL[1] == 'default.asp') {
			return "home";
		} else {
			return "homesub";
		}
	} else { 
		return arrURL[1];
	}
}

//----------------------------------------------------
// function to set multiple DIVs to same height
// by Paul@YellowPencil.com and Scott@YellowPencil.com
//----------------------------------------------------

function setTall() {
	if (document.getElementById && document.getElementById('homeLeft') && document.getElementById('homeRight')) {
		var divs = new Array(document.getElementById('homeLeft'), document.getElementById('homeRight'));
		
		var maxHeight = 0;
		for (var i = 0; i < divs.length; i++) {
			if (divs[i]) {
				if (divs[i].offsetHeight > maxHeight) maxHeight = divs[i].offsetHeight;
			}
		}
		
		for (var i = 0; i < divs.length; i++) {
			if (divs[i]) {
				divs[i].style.height = maxHeight + 'px';
	
				if (divs[i].offsetHeight > maxHeight) {
					divs[i].style.height = (maxHeight - (divs[i].offsetHeight - maxHeight)) + 'px';
				}
			}
		}
	}
}

window.onresize = function() {
	setTall();
}

//----------------------------------------------------
// function to replace BODY onLoad()
//----------------------------------------------------

function addLoadEvent(func) {
	var oldonload = window.onload;

	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//----------------------------------------------------
// functions to call onLoad
//----------------------------------------------------

addLoadEvent(setTall);
addLoadEvent(preloadImages);
