// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
							  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function FaqCollapseAll() {
	
	//Expand all Answers
	var ids=new Array();
	var tids=document.getElementsByTagName('div');
	for(i=0;i<tids.length;i++)if(tids[i].className=="faqAnswer")ids[ids.length]=tids[i];
	for(i=0;i<ids.length;i++)ids[i].style.display='none';
	
	//Change Style of All Questions
	var ids2=new Array();
	var tids2=document.getElementsByTagName('div');
	for(i=0;i<tids2.length;i++)if(tids2[i].className=="faqQuestionSelected")ids2[ids2.length]=tids[i];
	for(i=0;i<ids2.length;i++)ids2[i].className='faqQuestion';
	
	//Change Expand to Collapse
	var ids3=new Array();
	var tids3=document.getElementsByTagName('div');
	for(i=0;i<tids3.length;i++)if(tids3[i].className=="faqExpand")ids3[ids3.length]=tids[i];
	for(i=0;i<ids3.length;i++)ids3[i].style.display='block';
	
	var ids4=new Array();
	var tids4=document.getElementsByTagName('div');
	for(i=0;i<tids4.length;i++)if(tids4[i].className=="faqCollapse")ids4[ids4.length]=tids[i];
	for(i=0;i<ids4.length;i++)ids4[i].style.display='none';
	
	var trigger = document.getElementById('expandAllTrigger');
	trigger.style.display = 'block';
	
	var trigger = document.getElementById('collapseAllTrigger');
	trigger.style.display = 'none';
	
}

function FaqExpandAll() {
	
	//Expand all Answers
	var ids=new Array();
	var tids=document.getElementsByTagName('div');
	for(i=0;i<tids.length;i++)if(tids[i].className=="faqAnswer")ids[ids.length]=tids[i];
	for(i=0;i<ids.length;i++)ids[i].style.display='block';
	
	//Change Style of All Questions
	var ids2=new Array();
	var tids2=document.getElementsByTagName('div');
	for(i=0;i<tids2.length;i++)if(tids2[i].className=="faqQuestion")ids2[ids2.length]=tids[i];
	for(i=0;i<ids2.length;i++)ids2[i].className='faqQuestionSelected';
	
	//Change Expand to Collapse
	var ids3=new Array();
	var tids3=document.getElementsByTagName('div');
	for(i=0;i<tids3.length;i++)if(tids3[i].className=="faqExpand")ids3[ids3.length]=tids[i];
	for(i=0;i<ids3.length;i++)ids3[i].style.display='none';
	
	var ids4=new Array();
	var tids4=document.getElementsByTagName('div');
	for(i=0;i<tids4.length;i++)if(tids4[i].className=="faqCollapse")ids4[ids4.length]=tids[i];
	for(i=0;i<ids4.length;i++)ids4[i].style.display='block';
	
	var trigger = document.getElementById('expandAllTrigger');
	trigger.style.display = 'none';
	
	var trigger = document.getElementById('collapseAllTrigger');
	trigger.style.display = 'block';
}



function getElementsByStyleClass (className) {
  var all = document.all ? document.all :
    document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];
  return elements;
}


function FaqToggle(qid) {
	var question  = 'faq_q'+qid
	var answer = 'faq_a'+qid
	var expand = 'q'+qid+'_expand'
	var collapse = 'q'+qid+'_collapse'
	
	var q_container = document.getElementById(question)
	var q_answer = document.getElementById(answer)
	var q_expand = document.getElementById(expand)
	var q_collapse = document.getElementById(collapse)

if (q_container.className == 'faqQuestion') {
		q_container.className = 'faqQuestionSelected';
		q_answer.style.display = 'block';
		q_expand.style.display = 'none';
		q_collapse.style.display = 'block';
	} else { 
		q_container.className = 'faqQuestion'; 
		q_answer.style.display = 'none';
		q_expand.style.display = 'block';
		q_collapse.style.display = 'none';
	}
}


function expandContent(menuoption) {
var obj = document.getElementById (menuoption)
obj.style.display = 'block';
}
// Collapse Contest for DIV Menus
function collapseContent(menuoption) {
var obj = document.getElementById (menuoption)
obj.style.display = 'none';
}

function FaqSelectQuestion(question) {
var obj = document.getElementById (question)
obj.className = 'faqQuestionSelected';
}

function FaqUnselectQuestion(question) {
var obj = document.getElementById (question)
obj.className = 'faqQuestion';
}

function FaqExpand(param) {
		param.style.display=(param.style.display=="none")?"":"none";
}

// Follow a link with optional variables
function FollowLink (sUrl, sUrlVariables) {
  if (sUrlVariables != null) {
    var sAddVar  = (sUrl.match(/\.html?\?/i) == null) ? "?" : "&";
    sUrl += sAddVar + sUrlVariables;
  }
  window.location = sUrl;
}

function EmailLink (sUrl, sTitle) {
  popUpWindow("/email_share.htm?url=" + sUrl + "&title=" + sTitle,
	      500, 480, 0, 0);
}

function EmailColleague (sUrl, sTitle) {
  popUpWindow("/contact/popup_tellcolleague.htm?url=" + sUrl + "&title=" + sTitle,
	      500, 480, 0, 0);
}

function EmailFriend (sUrl, sTitle) {
  popUpWindow("/contact/popup_tellfriend.htm?url=" + sUrl + "&title=" + sTitle,
	      500, 480, 0, 0);
}

function TakeSurvey () {	
  popUpWindow("/contact/popup_survey.htm", 519, 520, 1, 0);
}

function StdInputValue (oInput) {
  var sValue = null;
  if (typeof(oInput) == "object") {
    switch(oInput.type) {
    case "select-multiple": case "select-one":
      sValue = oInput.options[oInput.selectedIndex].value; break;
    case "hidden": case "text": case "textarea": case "password":
    case "checkbox": 
      sValue = oInput.value; break;
    default:
      for (var i = 0; i < oInput.length && sValue == null; i++) {
        if (oInput[i].checked) { sValue = oInput[i].value; }
      }
      break;
    }
  }
  return sValue;
}

function FormValidate (oForm, sRequired) {
  if (!oForm || !ValidStr(sRequired)) { return false; }

  // Array of variables: variable name, description
  var aArgs = sRequired.split(",");

  // Loop through variable/test set, stopping if an error occurs
  for (var n = 0; n < aArgs.length; n += 2) {

    // Set variable name, test type and get the input object from the form
    sVarName = aArgs[n].replace(/^ */, "");
    sDesc    = aArgs[n+1].replace(/^ */, "");
    oInput   = eval("oForm." + sVarName);

    // Input object is defined - continue
    if (oInput) {
      var sValue = StdInputValue(oInput);

      // No state but there is a country input
      if (sVarName == "state" && oForm.country && !ValidStr(sValue)) {

	// No state defined
	if (StdInputValue(oForm.country) == "United States") {
	  alert("Please select a US state");
	  oInput.focus(); return false;
	}

	// No province defined
	else if (StdInputValue(oForm.country) == "Canada") {
	  alert("Please select a Canadian province");
	  oInput.focus(); return false;
	}
      }
      

      // Phone or fax check
      else if (sVarName == "phone" || sVarName == "fax") {
	if (!ValidStr(sValue) || sValue.search(/[^0-9\(\)\+\. -]/) >= 0 ||
	    sValue.length < 7) {
	  alert("Invalid number for " + sDesc);
	  oInput.value = ""; oInput.focus(); return false;
	}
      }

      // Zip code check - only verifying US codes for now
      else if (sVarName == "zip") {
	if (StdInputValue(oForm.country) == "United States" &&
	    (sValue.length < 5 || sValue.search(/[^0-9\-]/) >= 0)) {
	  alert("Please enter a valid US zip code");
	  oInput.focus(); return false;
	}
      }

      // Email check
      else if (sVarName == "email") {
	var reFilter = /^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]+$/i;
	//	var filter=/^.+@.+\..{2,3}$/;
	if (!reFilter.test(sValue)) {
	  alert("Please input a valid email address");
	  oInput.focus(); return false;
	}
      }

      // Checkbox value - assume description is complete
      else if (oInput.type == "checkbox" && !oInput.checked) {
	alert(sDesc);
	if (oInput.focus) { oInput.focus(); }
	return false;
      }

      // Standard value check - may be a radio button, so check for focus()
      else if (!ValidStr(sValue)) {
	alert("Please fill in " + sDesc);
	if (oInput.focus) { oInput.focus(); }
        else if (oInput.length > 0 && oInput[0].focus) { oInput[0].focus(); }
	return false;
      }

      // Password check
      if (sDesc.toLowerCase() == "password") {
	var sMsg = "";

	if (sValue.length < iPASSWD_MIN) {
	  sMsg = "Please use at least " + iPASSWD_MIN +
	    " characters or numbers in the password.";
	} else if (sValue.search(/[^A-Za-z0-9]/) >= 0) {
	  sMsg = "Your password should contain only characters or numbers.";
	  alert(sValue);
	} else if (oForm.sPasswordCheck &&
		   oForm.sPasswordCheck.value != sValue) {
	  sMsg = "Your passwords do not match.";
	  oForm.sPasswordCheck.value = "";
	}

	if (sMsg != "") {
	  alert(sMsg + " Please enter the password again.");
	  oInput.value = ""; oInput.focus(); return false;
	}
      }
    }
  }
  return true;
}

function ValidStr (sStr) {
  return (sStr != null && sStr.search(/[a-z0-9]/i) != -1);
}

function getUniqueName () {
  var oDate = new Date(); return("" + oDate.getTime());
}

function popUpWindow (url, width, height, scrollbar, verify) {

  if (verify && !ppdOutsideConfirm()) { return; }

  var str = "toolbar=0,directories=0,status=0,menubar=0,resizable=1";
  var x = Math.round((screen.width - width) / 2);
  var y = Math.round((screen.height - height) / 2);

  str += ",left=" + x + ",width="  + width;
  str += ",top="  + y + ",height=" + height;

  if (scrollbar == true) { str += ",scrollbars=1"; }
  else                   { str += ",scrollbars=0"; }

  subwindow = window.open(url, getUniqueName(), str);
  subwindow.focus();
}

function ppdOutsideLink (url) {
  if (!ppdOutsideConfirm()) { return; }
  subwindow = window.open(url, getUniqueName());
  subwindow.focus();
}

function ppdOutsideConfirm () {
  return confirm("You are leaving the PPD Web site. Linked Web sites\n" +
		 "are not under the control of PPD.  PPD does not\n" +
		 "endorse any Web site to which the PPD Web site is\n" +
		 "linked and PPD is not responsible for the content\n" +
		 "of any Web site linked to the PPD Web site.")
}

function BlankWindow (url) {
  var subwindow = window.open(url, getUniqueName());
  subwindow.focus();
}

// Follow a link with optional variables
function FollowLink (sUrl, sUrlVariables) {
  if (sUrlVariables != null) {
    var sAddVar  = (sUrl.match(/\.html?\?/i) == null) ? "?" : "&";
    sUrl += sAddVar + sUrlVariables;
  }
  window.location = sUrl;
}

// Redirection for select objects
function SelectRedirect (oSelect) {
  var sValue = StdInputValue(oSelect);
  if (ValidStr(sValue)) { window.location = sValue; }
}

// cGMP Podcast Player
function playPodcast(podcastid, audioid) {
	var obj = document.getElementById (audioid)
	var podcast_id = podcastid
	obj.style.display = 'none';
	var flash = document.createElement("div");
	flash.innerHTML = '<object height="19" width="226"><param value="true" name="allowFullScreen"><param value="http://natalie.feedroom.com/ppdi/audio/Player.swf?site=ppdi&skin=audio&fr_story='+podcast_id+'&rf=bm" name="movie"><embed height="19" width="226" src="http://natalie.feedroom.com/ppdi/audio/Player.swf?site=ppdi&skin=audio&fr_story='+podcast_id+'"></object>';
	obj.parentNode.insertBefore(flash,obj);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function EmailThisPage () {
  alert("Fall 2009 - at a theater near you!");
}
