//Returns everything right of the last instance of the subString to the end of the fullString
function rightFromSubStringToEndOfFullString(fullString, subString, defaultValue) {
	if (fullString.lastIndexOf(subString) == -1) {
	return defaultValue;
	} else {
	return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);
	}
}

function showStep(index) {

	for (i = 1; i <= 3 ; i++) {

		div = document.getElementById("step_" + i);

		if (i == index)

			div.style.display = "block";

		else

			div.style.display = "none";

	}

}

function show(divID) {

	document.getElementById(divID).style.display='block';

}

function hide(divID) {

	document.getElementById(divID).style.display='none';

}

function loadProductType() {

	findObj("ctrl_to_focus").value = "sProductType";

	document.getElementById('sProductType').selectedIndex = 0;

	document.f_app.action = rightFromSubStringToEndOfFullString(location.href,"/","online_application.php");

	document.f_app.submit();

}

function loadProductPlan() {

	findObj("ctrl_to_focus").value = "sProductPlan";

	document.f_app.action = rightFromSubStringToEndOfFullString(location.href,"/","online_application.php");

	document.f_app.submit();

}

function findObj(id) {

  return document.getElementById(id);

}

function SetValue(controlID, value) {

	var ctrl = findObj(controlID);

	if (ctrl)

		ctrl.value = value.replace(/<br>/g, "\r\n");

}

function SetRadioValue(controlID, value) {

	var index = 1;

	var ctrl = document.getElementById(controlID + index);

	while(ctrl)

	{

		if(ctrl.value == value)

		{

			ctrl.checked = true;

			break;

		}

		index++;

		ctrl = document.getElementById(controlID + index);

	}

}

function SetCheckBoxValue(controlID, value) {

	var ctrl = document.getElementById(controlID);

	if (ctrl) { ctrl.checked = (value != ""); }

}



function validateStep1() {

	if(trim(document.f_app.tFirstname.value)=="")

	{

		alert('Please fill in your First name.');

		document.f_app.tFirstname.focus();

		return false;

	}	

	if(trim(document.f_app.tLastname.value)=="")

	{

		alert('Please fill in your Last name.');

		document.f_app.tLastname.focus();

		return false;

	}

	if(trim(document.f_app.tMiddlename.value)=="")

	{

		alert('Please fill in your Middle name.');

		document.f_app.tMiddlename.focus();

		return false;

	}

	if(trim(document.f_app.tMothername.value)=="")

	{

		alert('Please fill in your Mother\'s Full Maiden Name.');

		document.f_app.tMothername.focus();

		return false;

	}

	var strDate = document.f_app.txtDobMm.value + "/" + document.f_app.txtDobDd.value + "/" + document.f_app.txtDobYyyy.value;

	

	if (isDate(strDate)==false){

		alert('Please fill in your Date of Birth properly.');

		document.f_app.txtDobDd.focus();

		return false;

	}

	if(trim(document.f_app.tHomeaddress.value)=="")

	{

		alert('Please fill in your Home Address.');

		document.f_app.tHomeaddress.focus();

		return false;

	}

	if(trim(document.f_app.tBarangay.value)=="")

	{

		alert('Please fill in your Municipality/Barangay.');

		document.f_app.tBarangay.focus();

		return false;

	}

	if(trim(document.f_app.tCity.value)=="")

	{

		alert('Please fill in your City.');

		document.f_app.tCity.focus();

		return false;

	}

	if(document.f_app.rBilling[0].checked==false && document.f_app.rBilling[1].checked==false)

	{

		alert('Please choose your Billing Address.');

		document.f_app.rBilling[0].focus();

		return false;

	}

	if(trim(document.f_app.tHomePhonePrefix.value)=="")

	{

		alert('Please fill in your Home Phone properly.');

		document.f_app.tHomePhonePrefix.focus();

		return false;

	}

	if(trim(document.f_app.tHomePhonePrefix.value)!="" && isNaN(document.f_app.tHomePhonePrefix.value))

	{

		alert('Please fill in your Home Phone properly.');

		document.f_app.tHomePhonePrefix.focus();

		return false;

	}

	if(trim(document.f_app.tHomePhone.value)=="")

	{

		alert('Please fill in your Home Phone properly.');

		document.f_app.tHomePhone.focus();

		return false;

	}

	if(trim(document.f_app.tHomePhone.value)!="" && isNaN(document.f_app.tHomePhone.value))

	{

		alert('Please fill in your Home Phone properly.');

		document.f_app.tHomePhone.focus();

		return false;

	}

	if (document.f_app.tEmail.value == "" || !check_email(document.f_app.tEmail.value)){

		alert("Please fill in your Email properly.");

		document.f_app.tEmail.focus();

		return false;

	}

	if (document.f_app.tConfirmEmail.value == "" || !check_email(document.f_app.tConfirmEmail.value)){

		alert("Please fill in your Email properly.");

		document.f_app.tConfirmEmail.focus();

		return false;

	}

	if (document.f_app.tConfirmEmail.value != document.f_app.tEmail.value){

		alert("Confirm Email is not the same.");

		document.f_app.tConfirmEmail.focus();

		return false;

	}

	if(trim(document.f_app.tTIN.value)=="")

	{

		alert('Please fill in your TIN properly.');

		document.f_app.tTIN.focus();

		return false;

	}

	if(trim(document.f_app.tSSS.value)=="")

	{

		alert('Please fill in your SSS properly.');

		document.f_app.tSSS.focus();

		return false;

	}

	if(trim(document.f_app.tCitizen.value)=="")

	{

		alert('Please fill in your Citizenship properly.');

		document.f_app.tCitizen.focus();

		return false;

	}

	return true;

}



function validateStep2() {

	if(trim(document.f_app.tSecondContact.value)=="")

	{

		alert('Please fill in your Authorized Contact Person in absence.');

		document.f_app.tSecondContact.focus();

		return false;

	}		

	if(trim(document.f_app.tSecondContactPhonePrefix.value)=="")

	{

		alert('Please fill in your Authorized Contact Person Phone Prefix properly.');

		document.f_app.tSecondContactPhonePrefix.focus();

		return false;

	}

	if(trim(document.f_app.tSecondContactPhonePrefix.value)!="" && isNaN(document.f_app.tSecondContactPhonePrefix.value))

	{

		alert('Please fill in your Authorized Contact Person Phone Prefix properly.');

		document.f_app.tSecondContactPhonePrefix.focus();

		return false;

	}

	if(trim(document.f_app.tSecondContactPhone.value)=="")

	{

		alert('Please fill in your Authorized Contact Person Phone properly.');

		document.f_app.tSecondContactPhone.focus();

		return false;

	}

	if(trim(document.f_app.tSecondContactPhone.value)!="" && isNaN(document.f_app.tSecondContactPhone.value))

	{

		alert('Please fill in your Authorized Contact Person Phone properly.');

		document.f_app.tSecondContactPhone.focus();

		return false;

	}

	if(trim(document.f_app.tEmployer.value)=="")

	{

		alert('Please fill in your Employer/Business Name properly.');

		document.f_app.tEmployer.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessAddress.value)=="")

	{

		alert('Please fill in your Complete Business Address properly.');

		document.f_app.tBusinessAddress.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessPhonePrefix.value)=="")

	{

		alert('Please fill in your Business Phone Prefix properly.');

		document.f_app.tBusinessPhonePrefix.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessPhonePrefix.value)!="" && isNaN(document.f_app.tBusinessPhonePrefix.value))

	{

		alert('Please fill in your Business Phone Prefix properly.');

		document.f_app.tBusinessPhonePrefix.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessPhone.value)=="")

	{

		alert('Please fill in your Business Phone properly.');

		document.f_app.tBusinessPhone.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessPhone.value)!="" && isNaN(document.f_app.tBusinessPhone.value))

	{

		alert('Please fill in your Business Phone properly.');

		document.f_app.tBusinessPhone.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessFaxPrefix.value)!="" && isNaN(document.f_app.tBusinessFaxPrefix.value))

	{

		alert('Please fill in your Business Fax Prefix properly.');

		document.f_app.tBusinessFaxPrefix.focus();

		return false;

	}

	if(trim(document.f_app.tBusinessFax.value)!="" && isNaN(document.f_app.tBusinessFax.value))

	{

		alert('Please fill in your Business Fax properly.');

		document.f_app.tBusinessFax.focus();

		return false;

	}

	if(trim(document.f_app.tPosition.value)=="")

	{

		alert('Please fill in your Position Title properly.');

		document.f_app.tPosition.focus();

		return false;

	}

	if(trim(document.f_app.tYears.value)=="")

	{

		alert('Please fill in your Years in Company properly.');

		document.f_app.tYears.focus();

		return false;

	}

	if(trim(document.f_app.tYears.value)!="" && isNaN(document.f_app.tYears.value))

	{

		alert('Please fill in your Years in Company properly (only numbers).');

		document.f_app.tYears.focus();

		return false;

	}

	/*

	if(trim(document.f_app.tIncome.value)=="")

	{

		alert('Please fill in your Gross Monthly Income properly.');

		document.f_app.tIncome.focus();

		return false;

	}

	if(trim(document.f_app.tIncome.value)!="" && isNaN(document.f_app.tIncome.value))

	{

		alert('Please fill in your Gross Monthly Income properly (only put the number without any separator).');

		document.f_app.tIncome.focus();

		return false;

	}

	*/

	return true;

}



function validateStepFinal() {

	if (document.f_app.sProduct.value == "")

	{

		alert('Please choose your Sun Cellular Product.');

		document.f_app.sProduct.focus();

		return false;

	}

	if (document.f_app.sProductType.value == "")

	{

		alert('Please choose your Sun Cellular Product Type.');

		document.f_app.sProductType.focus();

		return false;

	}

	if (document.f_app.sProductPlan.value == "")

	{

		alert('Please choose your Sun Cellular Product Type Plan.');

		document.f_app.sProductPlan.focus();

		return false;

	}

	if(document.f_app.fID.value=="")

	{

		alert('Please upload your ID documents.');

		document.f_app.fID.focus();

		return false;

	}

	else 

	{

		fIDValue = document.f_app.fID.value.toLowerCase();

		if (fIDValue.indexOf('jpeg') == -1 && fIDValue.indexOf('jpg') == -1 && fIDValue.indexOf('gif') == -1 && fIDValue.indexOf('png') == -1 && fIDValue.indexOf('pdf') == -1) {

			alert('Please upload ID documents with supported format gif/jpg/png/pdf.');

			document.f_app.fID.focus();

			return false;

		}

	}
	
	if($("#rd4").is(":hidden")){
		
	}else{ 
	
	if(document.f_app.fCompetitorBilling.value=="")
	{
		if(document.f_app.fIncome.value=="")
	
		{
	
			alert('Please upload your Proof of Income documents.');
	
			document.f_app.fIncome.focus();
	
			return false;
	
		}
	
		else 
	
		{
	
			fIncomeValue = document.f_app.fIncome.value.toLowerCase();
	
			if (fIncomeValue.indexOf('jpeg') == -1 && fIncomeValue.indexOf('gif') == -1 && fIncomeValue.indexOf('jpg') == -1 && fIncomeValue.indexOf('png') == -1 && fIncomeValue.indexOf('pdf') == -1) {
	
				alert('Please upload Proof of Income documents with supported format gif/jpg/png/pdf.');
	
				document.f_app.fIncome.focus();
	
				return false;
	
			}
	
		}

		if(document.f_app.fBilling.value=="")
	
		{
	
			alert('Please upload your Proof of Billing documents.');
	
			document.f_app.fBilling.focus();
	
			return false;
	
		}
	
		else 
	
		{
	
			fBillingValue = document.f_app.fBilling.value.toLowerCase();
	
			if (fBillingValue.indexOf('jpeg') == -1 && fBillingValue.indexOf('gif') == -1 && fBillingValue.indexOf('jpg') == -1 && fBillingValue.indexOf('png') == -1 && fBillingValue.indexOf('pdf') == -1) {
	
				alert('Please upload Proof of Billing documents with supported format gif/jpg/png/pdf.');
	
				document.f_app.fBilling.focus();
	
				return false;
	
			}
	
		}
	}

	if(document.f_app.fIncome.value=="" && document.f_app.fBilling.value=="")

	{
		if(document.f_app.fCompetitorBilling.value=="")
	
		{
	
			alert('Please upload your Proof of Competitor Billing documents.');
	
			document.f_app.fCompetitorBilling.focus();
	
			return false;
	
		}
	
		else {
	
			fCompetitorBillingValue = document.f_app.fCompetitorBilling.value.toLowerCase();
	
			if (fCompetitorBillingValue.indexOf('jpeg') == -1 && fCompetitorBillingValue.indexOf('gif') == -1 && fCompetitorBillingValue.indexOf('jpg') == -1 && fCompetitorBillingValue.indexOf('png') == -1 && fCompetitorBillingValue.indexOf('pdf') == -1) {
	
				alert('Please upload Proof of Competitor Billing documents with supported format gif/jpg/png/pdf.');
	
				document.f_app.fCompetitorBilling.focus();
	
				return false;
	
			}
	
		}
	}
	
	}
	
	

	/*if(trim(document.f_app.tRemarks.value)=="")

	{

		alert('Please fill in your Remarks properly.');

		document.f_app.tRemarks.focus();

		return false;

	}*/
	

  /**

	if(document.f_app.cAgree.checked==false)

	{

		alert('Please confirm that you agree with the Terms and Conditions.');

		document.f_app.cAgree.focus();

		return false;

	}**/

	return true;

}



function validateStepUpload() {

	if (document.f_app.fID) {

		if (document.f_app.fID.value=="")

		{

			alert('Please upload your ID documents.');

			document.f_app.fID.focus();

			return false;

		}

		else 

		{

			fIDValue = document.f_app.fID.value.toLowerCase();

			if (fIDValue.indexOf('jpeg') == -1 && fIDValue.indexOf('jpg') == -1 && fIDValue.indexOf('gif') == -1 && fIDValue.indexOf('png') == -1 && fIDValue.indexOf('pdf') == -1) {

				alert('Please upload ID documents with supported format gif/jpg/png/pdf.');

				document.f_app.fID.focus();

				return false;

			}

		}

	}

	if (document.f_app.fIncome) {

		if(document.f_app.fIncome.value=="")

		{

			alert('Please upload your Proof of Income documents.');

			document.f_app.fIncome.focus();

			return false;

		}

		else 

		{

			fIncomeValue = document.f_app.fIncome.value.toLowerCase();

			if (fIncomeValue.indexOf('jpeg') == -1 && fIncomeValue.indexOf('gif') == -1 && fIncomeValue.indexOf('jpg') == -1 && fIncomeValue.indexOf('png') == -1 && fIncomeValue.indexOf('pdf') == -1) {

				alert('Please upload Proof of Income documents with supported format gif/jpg/png/pdf.');

				document.f_app.fIncome.focus();

				return false;

			}

		}

	}

	if (document.f_app.fBilling) {

		if(document.f_app.fBilling.value=="")

		{

			alert('Please upload your Proof of Billing documents.');

			document.f_app.fBilling.focus();

			return false;

		}

		else 

		{

			fBillingValue = document.f_app.fBilling.value.toLowerCase();

			if (fBillingValue.indexOf('jpeg') == -1 && fBillingValue.indexOf('gif') == -1 && fBillingValue.indexOf('jpg') == -1 && fBillingValue.indexOf('png') == -1 && fBillingValue.indexOf('pdf') == -1) {

				alert('Please upload Proof of Billing documents with supported format gif/jpg/png/pdf.');

				document.f_app.fBilling.focus();

				return false;

			}

		}

	}

	if (document.f_app.fCompetitorBilling) {

		if(document.f_app.fCompetitorBilling.value=="")

		{

			alert('Please upload your Proof of Competitor Billing documents.');

			document.f_app.fCompetitorBilling.focus();

			return false;

		}

		else {

			fCompetitorBillingValue = document.f_app.fCompetitorBilling.value.toLowerCase();

			if (fCompetitorBillingValue.indexOf('jpeg') == -1 && fCompetitorBillingValue.indexOf('gif') == -1 && fCompetitorBillingValue.indexOf('jpg') == -1 && fCompetitorBillingValue.indexOf('png') == -1 && fCompetitorBillingValue.indexOf('pdf') == -1) {

				alert('Please upload Proof of Competitor Billing documents with supported format gif/jpg/png/pdf.');

				document.f_app.fCompetitorBilling.focus();

				return false;

			}

		}

	}

	document.f_app.action = "online_application_validate.php";

	document.f_app.submit();

}

function validateForm() {

	if (validateStep1() && validateStep2() && validateStepFinal()) {

		document.f_app.action = "online_application_validate.php";

		document.f_app.submit();

	}

}

function submitForm() {

	document.f_app.action = "online_application_process.php";

	document.f_app.submit();

}

function submitForm1() {

	document.f_app.action = "online_application_edit_process.php";

	document.f_app.submit();

}

function downloadFile(session_id, file) {

	document.location = "download_file.php?sub_dir=" + session_id + "&file=" + file;

}

function removeFile(session_id, file) {

	findObj("sub_dir").value = session_id;

	findObj("file").value = file;

	findObj("returnurl").value = "online_application.php";

	document.f_app.action = "remove_file.php";

	document.f_app.submit();

}

function removeFile1(session_id, file) {

	findObj("sub_dir").value = session_id;

	findObj("file").value = file;

	findObj("returnurl").value = "online_application_edit.php";

	document.f_app.action = "remove_file.php";

	document.f_app.submit();

}

function amendForm() {

	document.f_app.action = "online_application.php";

	document.f_app.submit();

}

function amendForm1() {

	document.f_app.action = "online_application_edit.php";

	document.f_app.submit();

}

function showTransaction() {

	if(trim(document.f_app.tTransactionID.value)=="")

	{

		alert('Please fill in your Transaction ID.');

		document.f_app.tTransactionID.focus();

		return false;

	}	

	document.f_app.action = "online_application_get_transaction.php";

	document.f_app.submit();

}

function validateForgotIDForm() {

  if(trim(document.f_app.forgotFormEmail.value)=="")

	{

		alert('Please fill in your Email.');

		document.f_app.forgotFormEmail.focus();

		return false;

	}

	document.f_app.action = "online_application_forgot_id.php";

	document.f_app.submit();

}

function showProductTNC() {

  

  var product = $("#sProduct").val();

  

  if (product != "") {

    $(".detailform").hide();

    if (product == "Sun Postpaid") {

      $("#tncContent").load("online_application_tnc_mobile.php", null, function(){$("#tncCheckbox").show();}).parent().show();  

    }

    else if (product == "Sun Broadband Wireless") {

      $("#tncContent").load("online_application_tnc_broadband.php", null, function(){$("#tncCheckbox").show();}).parent().show();  

    }

  }

  else {

    alert("Please choose a product.");

  }

}



$(function() {

  

  var product = $("#sProduct").val();

  

  if (product != "") {

    $(".detailform").show();

    $("#selectproductform").hide();

  }

  else {

    $(".detailform").hide();

    $("#selectproductform").show();

  }

  /*

  $("#tEmploymentType").change(function() {

    if ($(this).val() == "") {

      $(this).attr("id", "tEmploymentTypeBak").attr("name", "tEmploymentTypeBak");

      $("#tEmploymentTypeOther").attr("id", "tEmploymentType").attr("name", "tEmploymentType").show().focus();

    }

    else if ($(this).attr("id") != "tEmploymentType") {

      $("#tEmploymentType").attr("id", "tEmploymentTypeOther").attr("name", "tEmploymentTypeOther").hide();

      $("#tEmploymentTypeBak").attr("id", "tEmploymentType").attr("name", "tEmploymentType").show();

    }

  });

  

  $("#tPosition").change(function() {

    if ($(this).val() == "") {

      $(this).attr("id", "tPositionBak").attr("name", "tPositionBak");

      $("#tPositionOther").attr("id", "tPosition").attr("name", "tPosition").show().focus();

    }

    else if ($(this).attr("id") != "tPosition") {

      $("#tPosition").attr("id", "tPositionOther").attr("name", "tPositionOther").hide();

      $("#tPositionBak").attr("id", "tPosition").attr("name", "tPosition").show();

    }

  });

  */

  $("#btnSubmitForm").click(function() {

    if ($("#selectproductform").is(":visible"))

      showProductTNC();

    else if ($("#tncWrapper").is(":visible")) {

      if (document.f_app.cAgree.checked==false) {

        alert('Please confirm that you agree with the Terms and Conditions.');

        document.f_app.cAgree.focus();

        return false;

      }

      loadProductType();

    }

    else {

      validateForm();

    }

  });
  
  /*$("#sProductPlan").change(function(){
	$("#sProductPlan option:selected").each(function(){
		if(this.title=="1"){
			$("#rd2").hide()			
			$("#rd3").hide()
			$("#rd4").hide()
		}else{
			$("#rd1").show()
			$("#rd2").show()
			$("#rd3").show()
			$("#rd4").show()
		}
	});
  });*/  
  /*$("#sProductPlan").bind({
	change: function(){
		$("#sProductPlan option:selected").each(function(){
			if(this.title=="1"){
				$("#rd2").hide()			
				$("#rd3").hide()
				$("#rd4").hide()
			}else{
				$("#rd1").show()
				$("#rd2").show()
				$("#rd3").show()
				$("#rd4").show()
			}
		});
	}
  });*/
  
  /*function toggleRequireDocument(){
		$("#sProductPlan option:selected").each(function(){
			if(this.title=="1"){
				$("#rd2").hide()			
				$("#rd3").hide()
				$("#rd4").hide()
			}else{
				$("#rd1").show()
				$("#rd2").show()
				$("#rd3").show()
				$("#rd4").show()
			}
		});
  }
  
  $('#sProductPlan').bind('change',function(){	
	toggleRequireDocument();
  });*/
  
  if($("#sProductType").val() == "Easy Line"){
	  $("#sProductPlan").change(function(){									 
		 alert("Please proceed to the nearest Sun Shop and Bring a Valid ID to apply for this Plan.");					
		});
  }  
    
  if($("#sProductType").val() == "SBW Postpaid"){
	  $("#sProductPlan").change(function(){										 
		if($("#sProductPlan :selected").text() == "Easy Broadband**" || $("#sProductPlan :selected").text() == "Easy Broadband SIM Only**"){										 
		 alert("Please proceed to the nearest Sun Shop and Bring a Valid ID to apply for this Plan.");					
		}
		});
  }

});
