var g_bDataChanged = false;

function dataChanged()
{
  g_bDataChanged = true;
}

function resetChanges()
{
  g_bDataChanged = false;
}

function checkChanges()
{
  if (g_bDataChanged)
    return confirm("You have changed information on this page.  You will lose you changes if you select 'OK'.  Select 'Cancel' to remain on this page.");
  else
    return true;
}

function submitEvent(form, event, args)
{
  form.action.value = event;
	try
	{
  	for (key in args)
	  {
  	  form[key].value = args[key];
	  }
	} catch (ex) { }
	form.submit();
	return false;
}


//Added by Joe
function updatePulldown(elm, list) {
	//alert("elm"+elm+"-list"+list);	
if (elm && list) {
		var mylist = String(list);
		//alert (mylist);
		var elements = mylist.split(",");
		for (var i=0;i<elm.options.length;i++) {
			//alert (elm.options[i].value);
			if (in_array(elm.options[i].value,elements)) {
				//alert('Gotit');
				elm.options[i].selected = "1";
			}
		}
	}
}


function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict; 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}



//From Joe:  Stuff to assist submission of forms through ajax.
var http_form_request = false;
function makeRequest(url, parameters) {
      http_form_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_form_request = new XMLHttpRequest();
         if (http_form_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_form_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_form_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_form_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_form_request.onreadystatechange = alertContents;
      http_form_request.open('GET', url + parameters, true);
      http_form_request.send(null);
   }

   function alertContents() {
      if (http_form_request.readyState == 4) {
         if (http_form_request.status == 200) {
            //alert(http_request.responseText);
            result = http_form_request.responseText;
            document.getElementById('myspan').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function getstr(obj) {
      var getstr = ""; //was "?"
	 // alert(obj);
  for (i=0; i<obj.getElementsByTagName("input").length; i++) {
        if ((obj.getElementsByTagName("input")[i].type == "text")||(obj.getElementsByTagName("input")[i].type == "password")) {
           getstr += obj.getElementsByTagName("input")[i].name + "=" + 
                   obj.getElementsByTagName("input")[i].value + "&";
        }
        if (obj.getElementsByTagName("input")[i].type == "checkbox") {
           if (obj.getElementsByTagName("input")[i].checked) {
              getstr += obj.getElementsByTagName("input")[i].name + "=" + 
                   obj.getElementsByTagName("input")[i].value + "&";
           } else {
              getstr += obj.getElementsByTagName("input")[i].name + "=&";
           }
        }
        if (obj.getElementsByTagName("input")[i].type == "radio") {
           if (obj.getElementsByTagName("input")[i].checked) {
              getstr += obj.getElementsByTagName("input")[i].name + "=" + 
                   obj.getElementsByTagName("input")[i].value + "&";
           }
     }  
     if (obj.getElementsByTagName("input")[i].tagName == "SELECT") {
        var sel = obj.getElementsByTagName("input")[i];
        getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
     }
     
  }
	  //alert (getstr);
	  return getstr;
      //makeRequest('get.php', getstr);
   }

/* Added by Joe */
 function submitAjaxForm(divname, obj, url) {
 //alert (obj);
      var getstr = "?";
	  var els = obj.getElementsByTagName("input");
	  
  for (i=0; i<els.length; i++) {
        if ((els[i].type == "text")||
        	(els[i].type == "password")||
            (els[i].type == "hidden")) {
           getstr += els[i].name + "=" + 
                   els[i].value + "&";
        }
        if (els[i].type == "checkbox") {
           if (els[i].checked) {
              getstr += els[i].name + "=" + 
                   els[i].value + "&";
           } else {
              getstr += els[i].name + "=&";
           }
        }
        if (els[i].type == "radio") {
           if (els[i].checked) {
              getstr += els[i].name + "=" + 
                   els[i].value + "&";
           }
     }  
     
  }
  els = obj.getElementsByTagName("SELECT");
  for (i=0; i<els.length; i++) {
        var sel = els[i];
		//alert(sel.name sel.selectedIndex);
        getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
     }
      var newurl=url + getstr;
// 	 alert(newurl); alert(divname);
          sndReq(divname, url + getstr);
}


function verifyLoginForm()
{ //alert ("verifyLogin2");

 // if (document.loginForm.action == 'login')
//	{
    if (document.loginForm.userName.value == "")
    {
      alert("User Name must be provided.");
      document.loginForm.userName.focus();
      return false;
    }
    if (document.loginForm.password.value == "")
    {
      alert("Password must be provided.");
      document.loginForm.password.focus();
      return false;
    }
//  }
  return true;
}

function verifyLoginStatusForm()
{
    if (document.loginStatusForm.userName.value == "")
    {
      alert("User Name must be provided.");
      document.loginStatusForm.userName.focus();
      return false;
    }
    if (document.loginStatusForm.password.value == "")
    {
      alert("Password must be provided.");
      document.loginStatusForm.password.focus();
      return false;
    }
  return true;
}

function sndLoginReq () {
	var user = this.userName.value;
	var pass = this.password.value;
	var loginString = 'login.php?action=login&userName=' + user + '&password=' + pass;
	sndReq('content',loginString);
}


function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} 

function verifyDownloadForm () {
	//alert("foo");
	theform = document.getElementById('choosetrack');
	//If nothing is selected, warn dialog and return false.
	if (getSelectedRadio(theform.filechoice) == -1) {
		alert("Choose which file format and track type to download.");
		return false;
	}
	//If checkbox is unchecked, warn and return false.
	if (theform.terms.checked == false) {
		alert("You must accept the 5.1 music terms before you can download the file.");
		return false;
	}
	//Otherwise, hide the one box and show the other.
	hm('box');
	//sm('dlreminder',200,200);
	alert ("REMEMBER! After you have permanently synchronized your music, you must either call 866-544-5151 to discuss licensing options or return to purchase a royalty free or needledrop license to report usage or non-usage.");
	return true;
alert ('oops');
}

function verifyNewPass (username) {
	//alert (username);
	theform = document.getElementById('resetPassForm');
	//alert (theform.username.value);
	if (theform.username.value != username) { alert("User name does not match activation code?  Try again."); return false;}
	if (theform.pass.value != theform.repeatPass.value) { alert("Passwords do not match."); return false;}
	return true;
}
function verifyPassChange () {
	theform = document.getElementById('newpassform');
	password = document.getElementsByName('user[password]');
	repeat = document.getElementsByName('user[passwordRepeat]');
	//alert(password[0].value);
	//alert (repeat[0].value);
	//return false
	if (password[0].value != repeat[0].value) { alert("Passwords do not match."); return false;}
	if (password[0].value == "") { alert("Password field must not be blank."); return false;}
	return true;
}

function settracktype(myFiletype, myTracktype) {
	theform = document.getElementById('choosetrack');
	//alert(myFiletype); alert(myTracktype);
	theform.filetype.value = myFiletype;
	theform.tracktype.value = myTracktype;
}



function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

function prepChecked(myForm) {
	var checkBoxArr = getSelectedCheckboxValue(document.getElementsByName('downloads'));
	var txt = "Error";
	if (checkBoxArr.length == 0) { alert("No check boxes selected"); return false;}
    //alert(checkBoxArr.join(","));
	myForm.checkedDLs.value = checkBoxArr.join(",");
	var reason=myForm.removereason;
	var license=myForm.lictype;

if (myForm.dlaction.value == 'remove') {txt = "Do you agree to remove ALL selected tracks from all computers?";}

	if (myForm.dlaction.value == 'remove') {
		txt = "Do you agree to remove ALL selected tracks from all computers?";
		if (reason.selectedIndex==0) {
			alert("Please indicate why you have chosen not to purchase these tracks.");
			return false;
		}
		if ((reason.options[reason.selectedIndex].value=='6')&&(myForm.note.value=="")) {
			alert("Please use the note field to indicate a reason for 'other'.");
			return false;
		}
	}
	if (myForm.dlaction.value == 'purchase') {
		txt = "Do you wish to purchase the selected license for all these tracks?";
		if (license.selectedIndex==0) {
			alert("Please indicate why you have chosen not to purchase these tracks.");
			return false;
		}
		if ((license.options[license.selectedIndex].value=='7')&&(myForm.note.value=="")) {
			alert("Please use the note field to indicate a reason for 'other'.");
			return false;
		}
	}
    var confirmed = confirm(txt);
	return confirmed;
}

function getfieldvalue(name) {
	mylist=document.getElementsByName(name);
	return mylist[0].value;
}

function getfieldselection(name) {
	mylist=document.getElementsByName(name);
	val= mylist[0].selectedIndex;
	return mylist[0].options[val].value;
}

function validateUserForm(myForm) {
//stub function
//alert (myForm.user);
org=getfieldselection("user[orgType]");
lic=getfieldselection("user[licenseType]");
st=getfieldselection("user[state]");
ph=getfieldvalue("user[phone]");
zip=getfieldvalue("user[zipCode]");
//alert (org);
//alert (getfieldvalue("user[orgTypeOther]"));
//alert (lic);
if (getfieldvalue("user[firstName]") ==""){alert ("First name is a required field"); return false;};
if (getfieldvalue("user[lastName]") ==""){alert ("Last name is a required field"); return false;};
if (getfieldvalue("user[address1]") ==""){alert ("Please enter your address"); return false;};
if (getfieldvalue("user[city]") ==""){alert ("Please enter your city"); return false;};
if (st == 'NULL'){alert ("Please choose your state"); return false;};
if (zip ==""){alert ("Please enter your zip code"); return false;};
if (zip.length > 9){alert ("If entering a zip+4 code, enter it as a single 9-digit number"); return false;};
if ((zip.length != 5)&&(zip.length < 9)){alert ("Please enter a valid zip code"); return false;};
if (ph ==""){alert ("Please enter a phone number"); return false;};
if (ph.length != 10) {alert("Please enter a full phone number with area code - no dashes or spaces"); return false;}; 
if (getfieldvalue("user[emailAddress]") ==""){alert ("Email Address required for registration"); return false;};
if (getfieldvalue("user[company]") ==""){alert ("Please enter your business affiliation"); return false;};
if (getfieldvalue("user[title]") ==""){alert ("Please enter your title"); return false;};
if (org==""){alert ("Choose your organization type"); return false;};
if (org=='7'&&getfieldvalue("user[orgTypeOther]") ==""){alert ("Please indicate type of organization for 'other'"); return false;};
if (lic ==""){alert ("Choose your preferred license type"); return false;};
if (lic==7&&getfieldvalue("user[licenseTypeOther]")==""){alert ("Please indicate your preferred license type for 'other'"); return false;};
//if (getfieldvalue("user[licenseType]") ==""){alert ("First name is a required field"); return false;};

//alert (myForm.user['firstName'].value);

	//if (myForm.user['firstName'].value=="") 
	return true;
}
