/**
 * @author pversai
 */

$include("/js/lib/offer/validateForm.js");
$include("/js/lib/rd/QueryString.js");
$include("/js/lib/rd/net.js");

var SubmitForm = function (success, failure){
	if (success == undefined || success == ""){
		alert("SubmitForm - Please set a success function");
	}
	if (failure == undefined || failure == ""){
		alert("SubmitForm - Please set a Fail function");
	}
	this.success = success;
	this.failure = failure;
}

/**
 * Function creates a asyinc request to the process order host
 * set in the form.
 * @param {Object} form
 */
rdOnloadManager.addListener(function(){
	ValidateForm = function (form){
		return validateForm(form);
	}
});

function validateForm(form){
	clearErrors();
	FieldListManager.createFieldList(form);
	FieldListManager.setDefaultProperties(validationFields);
	errorList = FieldListManager.createErrorList();	
	if(errorList.length > 0){
		displayAlertMessage(errorList);
		return false;
	}
	var queryString = new QueryString(form);
    var request = new net.ContentLoader(form.action,displayThankyou,ajaxError,"POST",queryString.getQueryString());
	disableButtons();
	return false;
}

/**
 * @name inc short for include 
 * @description Allows to include other files.
 * @param {String} filename
 */
function $include(filename){
	
	var scripts = document.getElementsByTagName("script");
	for (var i=0; i< scripts.length; i++){
		if (scripts.item(i).src == (getHost() + filename)){
			return false;
		}
	}
	var head = document.getElementsByTagName('head').item(0);
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	head.appendChild(script);
}
/**
 * @return String
 */
function getHost(){
	var location = new String(document.location.href);
	var matches = location.split("/");
	return matches[0]+"//"+matches[2];
}

/**
 *  Implements the thankyou page omniture
 */
function TFHAjaxOmnitureThankyou(){
	
	var pageName = document.getElementById("page_name").innerHTML;
	var strEpid = document.getElementById("strEpid").innerHTML;
	var orderId = document.getElementById("orderId").innerHTML;
	var tid = document.getElementById("tid").innerHTML;
	var promoKey = document.getElementById("promokey").innerHTML;
	var price = document.getElementById("price").innerHTML;
	var optStatTransId = document.getElementById("optStatTransId").innerHTML;
	var optInOut = document.getElementById("optInOut").innerHTML;
	
	s.eVar5 = "offerForm";	
	s.channel = "rd: offer";
	s.prop1 = "";
	s.prop2 = "";
	s.prop5 = "";
	s.hier1 = "rd|offer";
	s.eVar19 = strEpid;
	s.eVar11 = tid;
	s.purchaseID = orderId;
	s.eVar20=s.purchaseID;
	
	// Newsletter signin and signout	
    if (optInOut.length > 0) {
        s.prop9 = optInOut.substring(0, optInOut.indexOf("|"));
        s.prop10 = optInOut.substring(optInOut.indexOf("|") + 1);
    }
	
	// not sure this can be done			

	if ( promoKey != null )
	{		
		s.products = ";" + promoKey + ";1;" + price;
	}
	
	s.pageName = "offer: The Family Handyman FREE Project Plans!: " + pageName;
	s.events = "event1,event5,event12,purchase";
	s.eVar1=s.pageName;
	void(s.t());
}