/*
you need to escape() any "&variable=" + something so that you have "&variable=" +  escape(something)
to prevent any & issues in their data.
*/

var DOWNLOADHOST_new = "http://cdn.gracewaymedia.net";
var DOWNLOADHOST = "http://72.47.254.89";
var PPT_DOWNLOADHOST = "http://cdn.gracewaymedia.net";

function new_download(sermon_id, ppsuid) {
	var input = document.getElementById("download_select_"+ sermon_id);
	var PPSUID = ppsuid;

	if (!input.value) {
		input.style.background = "#ff5544";
		return;
	}

	if(input.value == 'Download') {
		input.style.background = "#ff5544";
		return;
	}


	if(PPSUID < 1) {

		alert("Oops! You must be logged in to download.");
		return false;
		/*
		$confirm_yes_no = confirm("You must be logged in to download files.\r\nWould you like to go to the login page?");
		if($confirm_yes_no == true)
			document.location="/?page=login";
		else
			return false;
		*/
	}

	document.location = "/?page=download&sermon_id="+ sermon_id +"&text="+ input.value + "&ppsuid=" + escape(input.value) + "&ck=" + escape(input.value) + ":" + escape(input.value),'download';
}

function retitle(sermon_id, ppsuid) {
	var PPSUID = ppsuid;

	if(PPSUID < 1) {

		alert("Oops! You must be logged in to retitle.");
		return false;
		/*
		$confirm_yes_no = confirm("You must be logged in to download files.\r\nWould you like to go to the login page?");
		if($confirm_yes_no == true)
			document.location="/?page=login";
		else
			return false;
		*/
	}

}

function resetDLWarn() {
                          alert("Please select a download option to continue.");

              fid = "downloadWarn";
              FID = document.getElementById(fid);
              FID.style.display = "none";
}

function download_select(sermon_id, ppsuid) {

              afid = "c_i";
              aFID = document.getElementById(afid);

              bfid = "u_i";
              bFID = document.getElementById(bfid);

  var PPSUID = ppsuid;
              cfid = "u_ui";
              cFID = document.getElementById(cfid);
              ds1 = "option1";
              DS1 = document.getElementById(ds1);
              ds2 = "option2";
              DS2 = document.getElementById(ds2);
              ds3 = "option3";
              DS3 = document.getElementById(ds3);
			  ds4 = "option4";
              DS4 = document.getElementById(ds4);
			  ds9 = "option9";
              DS9 = document.getElementById(ds9);
              fid = "downloadWarn";
              FID = document.getElementById(fid);

       if (DS1.checked == true) {

           document.location = "/?page=download&sermon_id="+ sermon_id +"&text=text" + "&ppsuid=" + cFID.value,'download';
        }

	   else if (DS2.checked == true) {

            document.location = "/?page=download&sermon_id="+ sermon_id +"&text=notext" + "&ppsuid=" + cFID.value,'download';
        }

	   else if (DS3.checked == true) {

            document.location = "/?page=download&sermon_id="+ sermon_id +"&text=blank" + "&ppsuid=" + cFID.value,'download';
        }

	   else if (DS4.checked == true) {

		    document.location = "/?page=download&sermon_id="+ sermon_id +"&text=zip" + "&ppsuid=" + cFID.value,'download';
        }

	   else if (DS9.checked == true) {
            //document.location = "http://ppt.powerpointsermons.com/?page=download&sermon_id="+ sermon_id +"&text=zip" + "&ppsuid=" + cFID.value  + "&ck=" + bFID.value + ":" + aFID.value,'download';
            document.location = PPT_DOWNLOADHOST + "/stills/"+ input.value + NewName + "_Wide.zip";
        }

        else {
		                           alert("Please select a download option to continue.");

          FID.style.display = "";
          return;
        }

}


function download_select_motion(sermon_id, ppsuid) {

              cfid = "u_ui";
              cFID = document.getElementById(cfid);

              afid = "c_i";
              aFID = document.getElementById(afid);

              bfid = "u_i";
              bFID = document.getElementById(bfid);


              ds1 = "option1";
              DS1 = document.getElementById(ds1);
              ds2 = "option2";
              DS2 = document.getElementById(ds2);
              ds3 = "option3";
              DS3 = document.getElementById(ds3);
			  ds4 = "option4";
              DS4 = document.getElementById(ds4);
              fid = "downloadWarn";
              FID = document.getElementById(fid);

        if (DS1.checked == true) {
            //document.location = "http://video.powerpointsermons.com/?page=download&sermon_id="+ sermon_id + "&ppsuid=" + cFID.value +"&text=mpg"  + "&ck=" + bFID.value + ":" + aFID.value,'download';
            document.location = "/?page=download&sermon_id="+ sermon_id + "&ppsuid=" + cFID.value +"&text=mpg",'download';
        }

        else if (DS2.checked == true) {
            //document.location = "http://video.powerpointsermons.com/?page=download&sermon_id="+ sermon_id + "&ppsuid=" + cFID.value +"&text=mp4" + "&ck=" + bFID.value + ":" + aFID.value,'download';
            document.location = "/?page=download&sermon_id="+ sermon_id + "&ppsuid=" + cFID.value +"&text=mp4",'download';
        }

        else if (DS3.checked == true) {
            //document.location = "http://video.powerpointsermons.com/?page=download&sermon_id="+ sermon_id + "&ppsuid=" + cFID.value +"&text=wmv" + "&ck=" + bFID.value + ":" + aFID.value,'download';
            document.location = DOWNLOADHOST + "/video/HD/MPEG1"+ NewName + ".zip";
        }

		else if (DS4.checked == true) {
            //document.location = "http://video.powerpointsermons.com/?page=download&sermon_id="+ sermon_id + "&ppsuid=" + cFID.value +"&text=wmv" + "&ck=" + bFID.value + ":" + aFID.value,'download';
          document.location = DOWNLOADHOST + "/video/HD/MOV" + NewName + ".zip";
        }


        else {
          FID.style.display = "";
          return;
        }

}


function download_sample() {




              ds1 = "dl_1";
              DS1 = document.getElementById(ds1);
              ds2 = "dl_2";
              DS2 = document.getElementById(ds2);
              ds3 = "dl_3";
              DS3 = document.getElementById(ds3);
              ds4 = "dl_4";
              DS4 = document.getElementById(ds4);
              ds5 = "dl_5";
              DS5 = document.getElementById(ds5);
              ds6 = "dl_6";
              DS6 = document.getElementById(ds6);
              ds7 = "dl_7";
              DS7 = document.getElementById(ds7);
			  ds8 = "dl_8";
              DS8 = document.getElementById(ds8);
			  ds9 = "dl_9";
              DS9 = document.getElementById(ds9);
              fnid = "dlname";
              FNID = document.getElementById(fnid);
              emid = "dlemail";
              EMID = document.getElementById(emid);
              phid = "dlphone";
              PHID = document.getElementById(phid);
              hid = "reg_user_how";
              HID = document.getElementById(hid);
              hoid = "reg_how_other";
              HOID = document.getElementById(hoid);
              sid = "sample_id_ppt";
              SID = document.getElementById(sid);
              sid2 = "sample_id_motion";
              SID2 = document.getElementById(sid2);



              sample_id1 = SID.value;
              sample_id2 = SID2.value;

              goodSubmit = true;
              error = "Please complete the following and click submit again:";

              var inputname = eval(FNID);
              if (!inputname.value) {
                error = error + "\r\n- Please enter your name";
                goodSubmit = false;
              }
              var inputemail = eval(EMID);
              if (!inputemail.value) {
                error = error + "\r\n- Please enter your email address";
                goodSubmit = false;
              }
              var inputhow = eval(HID);
              if (!inputhow.value) {
                error = error + "\r\n- Please let us know how you heard about us";
                goodSubmit = false;

              }
              else {
                otherQuery = "&how=" + inputhow.value;
              }
              if (inputhow.value == "Other" || inputhow.value == "other") {
                var inputhowother = eval(HOID);
                if (!inputhowother.value) {
                  error = error + "\r\n- Please let us know the other way you heard about us";
                  goodSubmit = false;
                }
                else {
                  otherQuery = "&how=" + inputhow.value + "&how_other=" + inputhowother.value;
                }
              }

              if (goodSubmit) {
                      if (DS1.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id1 +"&text=text"  + "&name=" + inputname.value + "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS2.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id1 +"&text=notext"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS3.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id1 +"&text=blank"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS4.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id1 +"&text=zip"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS5.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id2 +"&text=SD_mpg"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS6.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id2 +"&text=SD_mov"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS7.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id2 +"&text=HD_mpg"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS9.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id2 +"&text=HD_mov"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }
                        else if (DS8.checked == true) {
                            document.location = "/?page=sample_download&sample_id="+ sample_id1 +"&text=wide"  + "&name=" + inputname.value +  "&email=" + inputemail.value + otherQuery ,'download';
                        }



                        else {
                          alert("Please select a download option to continue.");
                          return;
                        }
              }
              else {
                alert(error);
                return;
              }

}


function download_cpartner(sample_id) {

              fid = "download_select_sample";
              FID = document.getElementById(fid);
              var input = eval(FID);
              if (!input.value) {
                input.style.background = "#ff5544";
                return;
              }
              document.location = "/?page=cpartner_download&sample_id="+ sample_id +"&text="+ input.value;
            }

function two_free_download() {
  var input1 = eval("document.forms.download_form.name");
  var input2 = eval("document.forms.download_form.email");


  if (!input1.value) {
    input1.style.background = "#ff5544";
    var alert1 = true;
    var input_er = true;
  }

  if (!input2.value) {
    input2.style.background = "#ff5544";
    var alert2 = true;
    var input_er = true;
  }
  else {
    var email_check = emailCheck(input2.value);
    if (!email_check) {
      input2.style.background = "#ff5544";
      var input_er = true;
    }
  }

  if (alert1 && alert2) {
    alert("Please provide us your name and email address to continue.");
  }
  else if (alert1) {
    alert("Please provide us your name to continue.");
  }
  else if (alert2) {
    alert("Please provide us a valid email address to continue.");
  }

  if (input_er) {
    return;
  }

  document.location = "/?page=two_free_get&action=submit&name="+ input1.value +"&email="+ input2.value;
}



function emailCheck (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) {

          alert("Please enter a valid email address to continue. Email address seems incorrect (check @ and .'s)");

          return false
  }
  var user=matchArray[1]
  var domain=matchArray[2]

  if (user.match(userPat)==null) {
      alert("Please enter a valid email address to continue. The username doesn't seem to be valid.")
      return false
  }

  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
            for (var i=1;i<=4;i++) {
              if (IPArray[i]>255) {
                  alert("Please enter a valid email address to continue. Destination IP address is invalid!")
                  return false
              }
      }
      return true
  }
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
          alert("Please enter a valid email address to continue. The domain name doesn't seem to be valid.")
      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) {
     // the address must end in a two letter or three letter word.
     alert("Please enter a valid email address to continue. The address must end in a three-letter domain, or two letter country.")
     return false
  }

  if (len<2) {
     var errStr="Please enter a valid email address to continue. This address is missing a hostname!"
     alert(errStr)
     return false
  }
  return true;
}

function StillDownloader (sermon_id, ppsuid){



  var PPSUID = ppsuid;
              cfid = "u_ui";
              cFID = document.getElementById(cfid);

              afid = "c_i";
              aFID = document.getElementById(afid);

              bfid = "u_i";
              bFID = document.getElementById(bfid);


$('.Still').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=Still&ppsuid=" + cFID.value,'download';}); // Title Slide With Verse
$('.StillC').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=StillC&ppsuid=" + cFID.value,'download';}); // Title Slide W/O Verse
$('.StillTNT').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=StillTNT&ppsuid=" + cFID.value,'download';}); // No Verse No Title
$('.StillNV').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=StillNV&ppsuid=" + cFID.value,'download';}); // No Verse No Title
$('.StillNV1').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=StillNV1&ppsuid=" + cFID.value,'download';}); // No Verse No Title2
$('.StillNVB').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=StillNVB&ppsuid=" + cFID.value,'download';}); // No Verse No Title2
$('.StillNVB2').click(function() {document.location = "/?page=download&sermon_id="+ sermon_id +"&text=StillNVB2&ppsuid=" + cFID.value,'download';}); // No Verse No Title2

}

