// Code to Open the Lightbox/Model
// What is $(document).ready ? See: http://flowplayer.org/tools/using.html#document_ready

$(document).ready(function() {

	var triggers = $("a.modalInput").overlay({
		// some expose tweaks suitable for modal dialogs
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
			},
		closeOnClick: true,
		fixed: false
	});
});

function CheckFields(){
	var flag = 0;
	$.each($(".req_field"),function(i,e){
		if(e.value == ""){
			flag = 1;

		}
	});
	if(flag == 1){
		alert('Please fill all the fields');
	}
	else{
		flag = 0;
	}
	return flag;
}

// If the field got the red background, make sure to put remove and make it white again
function ClearFormElementsBackgrounds(form_element_id){
	$("#" + form_element_id).css({backgroundColor:"#ffffff"})
}

function fieldSelect(form_element_id){
	var fieldObj = $("#" + form_element_id);
	var fieldRow = fieldObj.parent().parent();
	var statusImage = fieldObj.next();
	var errorMessage = statusImage.next();
	fieldRow.attr("class", "formOnFocus");
	statusImage.attr("src", "/img/spacer.gif");
	errorMessage.hide();
}

function fieldValidate(form_element_id, validType){
	var fieldObj = $("#" + form_element_id);
	var fieldValue = fieldObj.val();
	var fieldRow = fieldObj.parent().parent();
	var statusImage = fieldObj.next();
	var errorMessage = statusImage.next();
	var valueError = (fieldValue == "");
	if(validType == 'email') valueError = !isValidEmailAddress(fieldValue);

	if (valueError) {
		fieldRow.attr("class", "formError");
		errorMessage.show();
		statusImage.attr("src", "/img/icons/formErrorRedX.png");
	} else {
		fieldRow.attr("class", "formConfirmed");
		errorMessage.hide();
		statusImage.attr("src", "/img/icons/formErrorGreenCheck.png");
	}
	statusImage.show();
}

function selectDemo(){
	$("#" + "demoList").css({border:"none"});
	$("#" + "demoList_message").css({color:"inherit"});
}

//TODO: Move to global and remove duplicate functions  //WHY?  this is global scope -ml
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function Rodrigo(cfc_path, form_method, form_div, div_reply_error, div_confirmation, submit_button){

	$.post(cfc_path + "?method=" + form_method, $("#" + form_div + " > form").serializeArray(),  function(reply){
		$("#" + div_confirmation).css({color:'#00aa00',backgroundColor:'#d8fbd8', fontSize:'110%'}).html('Thank you, your request is being processed.');
		//setTimeout("$('#closeOverlay').click();",2000);
	});
}

// This function handles 95% of all the forms, callign the cfc's and parsing the results
function ContactMe(cfc_path, form_method, form_div, div_reply_error, div_confirmation, submit_button, formSuffix){
	$("#" + submit_button).attr("disabled","disabled");
	$("#" + div_confirmation).css({backgroundColor:"#ffffff"});
	$(".formLoadingAnimation").show();
	var form_flag = 0;
	$.post(cfc_path + "?method=" + form_method, $("#" + form_div + " > form").serializeArray(),  function(reply){
		$(".formLoadingAnimation").hide();

		if (reply.indexOf("errors,") >= 0){
			form_flag = 1;
			$("#" + submit_button).removeAttr("disabled"); //Re-enable submit button so user can correct info and re-submit.

			var errorArray = reply.split(",");
			for (i=1; i < errorArray.length; i++) {
				if (errorArray[i] == 'no_demo') {
					$("#" + "demoList").css({border:"2px solid #ED1C24"});
					$("#" + "demoList_message").css({color:"#ED1C24"});
				} else {
					var fieldObj = $("#" + errorArray[i] + formSuffix);
					var fieldRow = fieldObj.parent().parent();
					var statusImage = fieldObj.next();
					var errorMessage = statusImage.next();

					fieldRow.attr("class", "formError");
					statusImage.attr("src", "/img/icons/formErrorRedX.png");
					errorMessage.show();
				}
			}
			alert('Please enter all the required fields');
		}
		else{
			omniFormEvents(form_div,'Submit');

			//TODO: Remove? Submit button already disabled.
			$("#" + submit_button).attr("disabled","disabled");
			if(reply.indexOf("|resourcelibrary") >=0){
				var tempAssetID = $('[name=asset_id]').val();
				var tempAssetType = $('[name=asset_type]').val();
				var tempAssetTitle = $('[name=asset_title]').val();
				omniRecordProductDownload(''+tempAssetType+'',''+tempAssetID+' : '+tempAssetTitle+'');

				if (tempAssetTitle == 'Discovery Promo Form') {
					var cookie_date = new Date(2012,01,01);
					document.cookie='discoveryPromoForm=1;expires=' + cookie_date.toGMTString() + ';path=/';
					location.reload();
					return;
				}

				var location_path = reply.split("|");
				var asset_path = location_path[0].replace(/^\s+|\s+$/g, '') ;
				if(asset_path.indexOf("http://") >= 0){
					window.open("" + location_path[0] + "","download","resizable=yes,dependent=yes,width=720,height=640,titlebar=yes,scrollbars=yes,location=yes");
				}
				else{
					window.open("http://www.ariba.com/" + asset_path + "","download","resizable=yes,dependent=yes,width=720,height=640,titlebar=yes,scrollbars=yes,location=yes");
				}

				$("#justLogged").fadeOut('fast');
				$("#hidden_button").fadeIn('fast');
				$("#hidden_button").click(function(){window.open("" + location_path[0] + "","download","resizable=yes,dependent=yes,width=720,height=640,titlebar=yes,scrollbars=yes,location=yes")});

			}
			else if(reply.indexOf("|collateral") >=0){
				var location_path = reply.split("|");
				window.open("" + location_path[0] + "","download","resizable=yes,dependent=yes,width=720,height=640,titlebar=yes,scrollbars=yes,location=yes");

				location.reload();

			}
			else if(reply.indexOf("supplier_workshop") >= 0){
				$("#" + form_div).html('');
			}
			else if(reply.indexOf("customer_nomination") >= 0){
				$("#" + form_div).html('');
			}
			else if(reply.indexOf("supplier_workshop") >= 0){
				$("#" + div_confirmation).css({color:'#00aa00',backgroundColor:'#d8fbd8', fontSize:'110%'}).html("Thank you for registering. You'll receive an email soon with additional information about this event.");
			}
			else if(reply.indexOf("sourcing_partner") >= 0){

				$("#confirmation_post").css({color:'#00aa00',backgroundColor:'#d8fbd8'}).html("<strong>Thank you for taking the time to share this information with us. We look forward to talking with you, and will be in touch soon.</strong>");

			}
			else if(reply.indexOf("partner_form") >= 0){
				$("#become_partner_div").slideUp('slow');
				$("#confirmation_post").css({color:'#00aa00',backgroundColor:'#d8fbd8'}).html("<h2><strong>Thank you for taking the time to share this information with us. We look forward to talking with you, and will be in touch soon.</strong></h2>");
			}
			else{
				$("#" + div_confirmation).css({color:'#00aa00',backgroundColor:'#d8fbd8', fontSize:'110%'}).html('Thank you, your request is being processed.');
			}
			$('#' + submit_button).removeAttr('disabled');
			setTimeout("$('img.close').click();",2500);
		    //$("#" + div_confirmation).html('');
			$("#" + form_div + " > form").get(0).reset();
			$("#" + form_div + " > form img").attr("src", "/img/spacer.gif");
		}
	});
}

function ContactMeFieldsJs(cfc_path, form_method, form_div){
	$.post(cfc_path + "?method=" + form_method, $("#contactMeDiv > form").serializeArray(),  function(reply){

		if (reply.indexOf("ERRORS,") >= 0) {
			var errorArray = reply.split(",");
			for (i = 1; i < errorArray.length; i++) {

				$("input[name=" + errorArray[i] + "]", "#contactMeDiv > form").css({backgroundColor:"#fbd7d7"});
				$("select[name=" + errorArray[i] + "]", "#contactMeDiv > form").css({backgroundColor:"#fbd7d7"});
				if(errorArray[i] == "solution_interest_00N30000000fZ9Q"){
					alert('Please select an option for your role');
					return false;
				}
			}
		}
		else{
			omniFormEvents(form_div,'Submit');
			$("#subcontact").css({color:'#00aa00',backgroundColor:'#d8fbd8', fontSize:'110%'}).html('Thank you, your request is being processed.');
			$("#contactMeConfirmation").css({color:'#00aa00',backgroundColor:'#d8fbd8', fontSize:'110%'}).html('Thank you, your request is being processed.');
			setTimeout("$('img.close').click();",2500);
		}
	});
}

function CreateSalesForceStruct(cfc_path, str_asset_id, str_email, form_div, campid, assettitle){

	$.post("http://localhost:8500" + cfc_path + "?method=CreateSalesForceStruct&asset_title=" + assettitle + "&asset_id=" + eval(str_asset_id) + "&campaign_id=" + campid, $("#" + form_div + " > form").serializeArray(),  function(reply){


	});

}




