/*function display_sub_popup(post_id) {
	
}
function display_sub_offer(post_id, is_home, wide) {
	
}
function display_ofie(post_id) {
	
}*/
jQuery(document).ready(function() {
	var ofie_container = jQuery("#ofie");
	var sub_offer_container = jQuery("#rail_subscribe");
	var sub_popup_container = jQuery("#sub_popup");
	
	var post_id = ofie_container.attr("post_id");
	if (jQuery("#ofie").size() < 1) {
		post_id = sub_offer_container.attr("post_id");
	}
	if (jQuery("#rail_subscribe").size() < 1) {
		post_id = sub_popup_container.attr("post_id");
	}
	var is_home = sub_offer_container.attr("is_home");
	var is_wide = sub_offer_container.attr("wide");

	var location = document.location.toString().replace('http://', '');
	var index = location.indexOf("/");
	if (index >= 0) {
		location = location.substring(index);
	}
	
	var hasOfie = false;
	var hasSubOffer = false;
	var hasSubPopup = false;
	
	setTimeout(function(){jQuery.get('/?sub_forms=1&post_id='+post_id+'&is_home='+is_home+'&is_wide='+is_wide+'&location='+escape(location),
			function(data) {
				var form_data = jQuery(data)

				if (form_data.filter("#ofie_offer").size() > 0) {
					hasOfie = true;
					ofie_container.replaceWith(form_data.filter("#ofie_offer"));
				}
				if (form_data.filter("#rr_mag_sub").size() > 0) {
					hasSubOffer = true;
					//setTimeout(function(){sub_offer_container.replaceWith(form_data.filter("#rr_mag_sub"))}, 15000);
					sub_offer_container.replaceWith(form_data.filter("#rr_mag_sub"));
				}
				if (form_data.filter("#popup_container").size() > 0) {
					hasSubPopup = true;
					sub_popup_container.replaceWith(form_data.filter("#popup_container"));
					jQuery("#popup_container").SubDialog({btnClose: '.subpopup_close', subs_mag_id: jQuery("#popup_container").attr("subs_mag_id")});
				}
			}
	)}, 120000);
});

function isNotAValidTextField(obj) {
    if (null != obj && obj != 'undefined' && null != obj.value && obj.value.length > 0) {
        return false;
    } else {
        return true;
    }
}
function validateMyCMSForm(formObj) {
    var errors = '';

    if (isNotAValidTextField(formObj.osc_address1)) {
        errors += '\nPlease fill the Address 1';
    }

    if (isNotAValidTextField(formObj.osc_city)) {
        errors += '\nPlease fill the City';
    }
    
    if (isNotAValidTextField(formObj.osc_state)){
	errors += '\nPlease choose a State';
    }

    if (isNotAValidTextField(formObj.osc_zip)) {
        errors += '\nPlease fill the Zip';
    }

    if (isNotAValidTextField(formObj.osc_email)) {
        errors += '\nPlease fill the email';
    }

    if (isNotAValidTextField(formObj.osc_last_name)) {
        errors += '\nPlease fill the Last Name';
    }

    if (isNotAValidTextField(formObj.osc_first_name)) {
        errors += '\nPlease fill the First Name';
    }

    if (null != errors && errors.length > 0) {
        alert('Please correct the following:' + errors);
        return false;
    } else {
        if (typeof (formObj.osc_submit_button) != "undefined" && formObj.osc_submit_button != null) {
            var Node = formObj.osc_submit_button;
            Node.parentNode.removeChild(Node);
            formObj.submit_button_div_message_osc.style.display = 'block';
        }
        updateIfBillMeExist(formObj);
        
        submitCircSourceForm(formObj, jQuery(formObj).attr('action'));
        
        return false;
    }
}
function submitCircSourceForm(clientfrm, url) {
	var theString = "";
	var f1 = clientfrm;
	if(url) {
		theString += url;
	}
	else {
		theString += "https://www.circsource.com/store/Subscribe.html?";
	}

	
	var magazineName = "";
	if(f1.iMagId.value == 151) {
		magazineName = escape("Horse&Rider");
	} else if(f1.iMagId.value == 28) {
		magazineName = "EQUUS";
	}
	
	if (theString.indexOf('circsource.com') != -1) {
		theString += "magazineId=" + f1.iMagId.value;
		//theString += "&i4Ky=" + f1.i4Ky.value;
		theString += "&osc_first_name=" + escape(f1.osc_first_name.value);
		theString += "&osc_last_name=" + escape(f1.osc_last_name.value);
		theString += "&osc_address1=" + escape(f1.osc_address1.value);
		theString += "&osc_address2=" + escape(f1.osc_address2.value);
		theString += "&osc_city=" + escape(f1.osc_city.value);
		theString += "&osc_state=" + f1.osc_state.options[f1.osc_state.selectedIndex].value;
		theString += "&osc_zip=" + escape(f1.osc_zip.value);
		theString += "&osc_email=" + escape(f1.osc_email.value);
		if (f1.offerId)
			theString += "&offerId=" + escape(f1.offerId.value);
		theString += "&countryCode=" + escape(f1.countryCode.value);
		theString += "&CMSOfferPlacement=" + escape(f1.CMSOfferPlacement.value);
		theString += "&frmHtml=" + escape(f1.frmHtml.value);
		if (f1.sourceCode)
			theString += "&sourceCode=" + escape(f1.sourceCode.value);
		if (f1.osc_term_value_radio)
			theString += "&osc_term_value_radio=" + escape(f1.osc_term_value_radio.value);
		if (f1.osc_cash_or_credit.value)
			theString += '&osc_cash_or_credit=1';
		else
			theString += '&osc_billme_later=Y';
	} else {
		theString += "iMagId=" + f1.iMagId.value;
		theString += "&i4Ky=" + f1.i4Ky.value;
		theString += "&iOrdBillName=" + escape(f1.osc_first_name.value + ' ' + f1.osc_last_name.value);
		theString += "&iOrdBillAddr1=" + escape(f1.osc_address1.value);
		theString += "&iOrdBillAddr2=" + escape(f1.osc_address2.value);
		theString += "&iOrdBillCity=" + escape(f1.osc_city.value);
		theString += "&iOrdBillState=" + f1.osc_state.options[f1.osc_state.selectedIndex].value;
		theString += "&iOrdBillPCode=" + escape(f1.osc_zip.value);
		theString += "&iOrdBillEmail=" + escape(f1.osc_email.value);
		//theString += "&offerId=" + escape(f1.offerId.value);
		theString += "&iOrdBillCountry=US";// + escape(f1.countryCode.value);
		//theString += "&CMSOfferPlacement=" + escape(f1.CMSOfferPlacement.value);
		//theString += "&frmHtml=" + escape(f1.frmHtml.value);
	}
	
	
	 AimTrackEvent("Circ DHTML Offer", "Submit", magazineName, escape((f1.offerId)?f1.offerId.value:''));
	 
	 window.open(theString, 'mywindow', '');
}
function updateIfBillMeExist(myForm) {
    var billMe = null;
    if (myForm.osc_billme_later && myForm.osc_cash_or_credit) {
        if (myForm.osc_billme_later.length) {
            for (var i = 0; i < myForm.osc_billme_later.length; i++) {
                if (myForm.osc_billme_later[i].checked) {
                    billMe = myForm.osc_billme_later[i].value;
                }
            }
            if (billMe == 'Y') {
                myForm.osc_cash_or_credit.value = null;
            } else {
		myForm.osc_cash_or_credit.value = '2';
	    }
        }
    }
}

function submitMyPCDForm(formObj) {
  if (validateMyPCDForm(formObj)) {
    var theString = jQuery(formObj).attr("action");
    var f1 = formObj;
    
    theString += "&iOrdBillName=" + f1.osc_first_name.value + " " + f1.osc_last_name.value;
    theString += "&iOrdBillAddr1=" + f1.osc_address1.value;
    theString += "&iOrdBillAddr2=" + f1.osc_address2.value;
    theString += "&iOrdBillCity=" + f1.osc_city.value;
    theString += "&iOrdBillState=" + f1.osc_state.options[f1.osc_state.selectedIndex].value;
    theString += "&iOrdBillPCode=" + f1.osc_zip.value;
    theString += "&iOrdBillCountry=US";
    theString += "&iOrdBillEmail=" + f1.osc_email.value;
    window.open(theString, 'mywindow', '');
  }
  return false;
}

function validateMyPCDForm(formObj){
  var errors='';

  if(isNotAValidTextField(formObj.osc_address1)) {
	  errors+='\nPlease fill the Address 1';
  }

  if(isNotAValidTextField(formObj.osc_city)) {
    errors+='\nPlease fill the City';
  }

  if(isNotAValidTextField(formObj.osc_zip)) {
    errors+='\nPlease fill the Zip';
  }

  if(isNotAValidTextField(formObj.osc_email)) {
    errors+='\nPlease fill the email';
  }

  if(isNotAValidTextField(formObj.osc_last_name)) {
	errors+='\nPlease fill the Last Name';
  }

  if(isNotAValidTextField(formObj.osc_first_name)){
    errors+='\nPlease fill the First Name';
  }
  
  if(null!=errors && errors.length>0) {
    alert('Please correct the following:'+errors);
    return false;
  }
  return true;
}
