// JavaScript Document
function elem(el) {
return document.getElementById(el);
}

function submitField(field) {
	var whichField = elem(field);
		whichField.submit();
}


function submitName(name) {
	var styleInd = elem('dock').style.selectedIndex;
	elem('mainForm').styleIndex.value = elem('dock').style[styleInd].value;
	document.mainForm.occasion.value = document.dock.occasion.value;
	if (name == 1) {
		document.mainForm.nameExists.value = 'yes';
	} else {
		document.mainForm.nameExists.value = 'no';
	}
	elem('mainForm').submit();
}

function submitDock(name) {
	if (document.dock.nameExists) {
		if (name == 1) {
			document.dock.nameExists.value = 'yes';
		} else {
			document.dock.nameExists.value = 'no';
		}
	}
	elem('dock').submit();
}

function submitWhat(formName,name) {
	if (elem(formName).nameExists) {
		if (name == 1) {
			elem(formName).nameExists.value = 'yes';
		} else {
			elem(formName).nameExists.value = 'no';
		}
	}
	elem(formName).submit();
}

function hoverLyrics(module) {
	switch(module.className) {
		case 'occasionButton':
			module.className = 'occasionButtonHover';
			break;
		case 'occasionButtonHover':
			module.className = 'occasionButton';
			break;
		case 'altLyrics':
			module.className = 'altLyricsHover';
			break;
		case 'altLyricsHover':
			module.className = 'altLyrics';
			break;
	}
}

function hoverButton(module) {
	switch(module.className) {
		case 'button_asterisk':
			module.className = 'button_asterisk_hover';
			break;
		case 'button_asterisk_hover':
			module.className = 'button_asterisk';
			break;
		case 'button_heart':
			module.className = 'button_heart_hover';
			break;
		case 'button_heart_hover':
			module.className = 'button_heart';
			break;
		case 'button_laugh':
			module.className = 'button_laugh_hover';
			break;
		case 'button_laugh_hover':
			module.className = 'button_laugh';
			break;
		case 'button_card':
			module.className = 'button_card_hover';
			break;
		case 'button_card_hover':
			module.className = 'button_card';
			break;
		case 'button_skip':
			module.className = 'button_skip_hover';
			break;
		case 'button_skip_hover':
			module.className = 'button_skip';
			break;
		case 'button_kiss':
			module.className = 'button_kiss_hover';
			break;
		case 'button_kiss_hover':
			module.className = 'button_kiss';
			break;
		case 'next':
			module.className = 'next_hover';
			break;
		case 'next_hover':
			module.className = 'next';
			break;
		case 'back':
			module.className = 'back_hover';
			break;
		case 'back_hover':
			module.className = 'back';
			break;
	}
}

function selectButton(module) {
	var selectedclass = null;
	var mc = module.className;
	if ( mc == 'button_asterisk_hover' || mc == 'button_asterisk' ) {
		selectedclass = 'button_asterisk_selected';
	} else if ( mc == 'button_heart_hover' || mc == 'button_heart' ) {
		selectedclass = 'button_heart_selected';
	} else if ( mc == 'button_laugh_hover' || mc == 'button_laugh' ) {
		selectedclass = 'button_laugh_selected';
	} else if ( mc == 'button_kiss_hover' || mc == 'button_kiss' ) {
		selectedclass = 'button_kiss_selected';
	}
	module.className = selectedclass;
}

function revertSelectedButton(module) {
	var revertedclass = null;
	switch (module.className) {
		case 'button_asterisk_selected':
			revertedclass = 'button_asterisk';
			break;
		case 'button_heart_selected':
			revertedclass = 'button_heart';
			break;
		case 'button_laugh_selected':
			revertedclass = 'button_laugh';
			break;
		case 'button_kiss_selected':
			revertedclass = 'button_kiss';
			break;
	}
	module.className = revertedclass;
}


function getCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{ begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return "";
}



function setCookie(NameOfCookie, value, expiredays)
{
	var ExpireDate = new Date ();
	var cookieDate ='';
	if (expiredays != 0) {
		ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
		cookieDate = "expires=" + ExpireDate.toGMTString() + ";";
	}
	document.cookie = NameOfCookie + "=" + escape(value) +";"+ cookieDate;
}



function delCookie (NameOfCookie)
{ if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

}


function setNav(navSection) {
	if (elem(navSection) != null) {
		elem(navSection).id = 'current';
	}
}

function PlayerOpen(soundfiledesc, soundfilepath) 
{
    var UniqueID = 314 // Make each link open in a new window
    var newWinOffset = 0 // Position of first pop-up

    PlayWin = window.open('',UniqueID,'width=350,height=330,top=' + newWinOffset +',left=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
    PlayWin.focus();

    var winContent = "<HTML>\n<HEAD>\n<TITLE>Preview: " + soundfiledesc + "</TITLE>"
	
					+ "\n<style type='text/css'>"
					
					+ "\n.button {border-left: 5px solid #e80000;padding: 4px 10px 5px 5px;text-align: center;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;}"
					
					+ "\n.buttonWrapper {	background: #ff9900 url(./images/button_normal_orange.gif) top right no-repeat;width: 140px;	height: 22px;overflow: hidden;}"
					
					+ "\n.buttonWrapper a, .buttonWrapperNext a, .buttonWrapperBack a {color: #fff;font-weight: bold;text-decoration: none;cursor: pointer;}"
					
					+ "\n.buttonWrapper a:hover, .buttonWrapperNext a:hover, .buttonWrapperBack a:hover {color: #e80000;text-decoration: none;}"
					
					+ "\n.buttonAlign {width: 140px;position: relative;}"
					
					+ "\nbody {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 11px;text-align: center;background: #fff;}"
					+ "\n</style>";
					+ "\n</HEAD>\n<BODY>";
    
    winContent += "\n\n<div style='font-size:18px; margin: 20px; font-weight: bold; color: #e80000'>Preview: <span style='color: #ff9900;'>" + soundfiledesc + "</span></div>";

    winContent += "\n<div style='width: 300px; background: #ffdd97 url(./images/corner_top_left.gif) top left no-repeat; margin: 0 auto;'><div style='width: 300px; background:  url(./images/corner_top_right.gif) top right no-repeat;'><div style='font-size:11px; padding: 10px;'><div style='margin: 0 0 10px;'>This audio clip may take a moment to download.</div><em>Preview quality &mdash; Lyricized songs are produced as high quality MP3s.</em></div>";
    
    winContent += "\n\n<OBJECT width='300' height='42' style='margin: 0 auto;'>";
    winContent += "\n<param name='SRC' value='" + soundfilepath + "'>";
    winContent += "\n<param name='AUTOPLAY' VALUE='true'>";
    winContent += "\n<param name='CONTROLLER' VALUE='true'>";
    winContent += "\n<param name='BGCOLOR' VALUE='#ffffff'>";    
    winContent += "\n<EMBED SRC='" + soundfilepath + "' AUTOSTART='TRUE' LOOP='FALSE' WIDTH='300' HEIGHT='42' CONTROLLER='TRUE' BGCOLOR='#ffffff' ";
    
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf("Firefox") != -1)
    {
  	winContent += " type='application/x-mplayer2' ";
    }
    
    winContent += ">\n</EMBED>\n</OBJECT>";

    winContent += "\n</div>\n</div>\n\n<div class='pad_0_5_10' style='margin: 20px auto;text-align: center'> "
				+ "\n<div class='buttonWrapper' style='margin: 0 auto;'>"
				+ "\n<a href='javascript:window.close();' name='submit' value='submit' type='submit'>"
				+ "\n<div class='button'>CLOSE WINDOW</div>\n</a>\n</div>\n</div>";
    winContent += "\n\n</BODY>\n</HTML>";

    PlayWin.document.write(winContent);
    PlayWin.document.close(); // "Finalizes" new window
    //UniqueID = UniqueID + 1 // newWinOffset = newWinOffset + 20 // subsequent pop-ups will be this many pixels lower
 }


function previewName(name)
{
	
	
	PlayerOpen(name, "http://www.lyricize.com/audio_files/audio_files/N_" + name + "_117.wav");	
}

function handleEnter (field, event, stay) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		if (stay == 1) {
		} else { field.form.elements[i].focus(); }
		return false;
	} 
	else
	return true;
}

function disableEnterKey()
{
     if (window.event.keyCode == 13) window.event.keyCode = 0;
}
 
function trimAll(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimLeadingZero(sString) {
	while (sString.substring(0,1) == '0') {
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function errorNotice(items) {
	var errorHTML = "<h2>Oops!</h2><ul>" + items + "</ul>";
	var errorDiv = document.getElementById('errorNotice');
	errorDiv.className = "displaySome";
	errorDiv.innerHTML = errorHTML;
	window.location = '#errorNotice';
}
