// 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	
	});
});

// 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"})
}

// 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){
	$("#" + div_reply_error).css({backgroundColor:"#ffffff"});
	$("#" + div_reply_error).html('');
	$("#" + div_confirmation).css({backgroundColor:"#ffffff"});
	$("#" + div_confirmation).html('');
	$.post(cfc_path + "?method=" + form_method, $("#" + form_div + " > form").serializeArray(),  function(reply){
	
		if (reply.indexOf("errors") >= 0){
			if (reply.indexOf("errors,no_demo") >= 0){
				$("#" + div_reply_error).css({color:"#dd0000",backgroundColor:"#fbd7d7"}).html('<strong>No demo has been selected.</strong>');		
			}
			else if(reply.indexOf("errors,emailexits") >= 0){
				$("#" + div_reply_error).css({color:"#dd0000",backgroundColor:"#fbd7d7"}).html('<strong>You are already registered with this email address.</strong>');		
			}					
			else{
				$("#" + div_reply_error).css({color:"#dd0000",backgroundColor:"#fbd7d7",width:"550px"}).html('<strong>Please fill all the required fields.</strong>');		
				var errorArray = reply.split(",");
				
				for (i=0; i < errorArray.length; i++) {
					if(errorArray[i] == 'solution_interest_00n30000000fz9q'){
						$("select[name=solution_interest_00N30000000fZ9Q]", "#" + form_div + " > form").css({backgroundColor:"#fbd7d7"});	
					}
					$("input[name=" + errorArray[i] + "]", "#" + form_div + " > form").css({backgroundColor:"#fbd7d7"});	
					$("select[name=" + errorArray[i] + "]", "#" + form_div + " > form").css({backgroundColor:"#fbd7d7"});	
					$("textarea[name=" + errorArray[i] + "]", "#" + form_div + " > form").css({backgroundColor:"#fbd7d7"});	
				}
			}		
		} 
		else{
			omniFormEvents(form_div,'Submit');
			
			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+'');
				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('');
			}
			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{
				$("#" + div_confirmation).css({color:'#00aa00',backgroundColor:'#d8fbd8', fontSize:'110%'}).html('Thank you, your request is being processed.');
			}
			setTimeout("$('#closeOverlay').click();",3000);
		    //$("#" + div_confirmation).html('');  
			$("#" + form_div + " > form").get(0).reset();
						
			
		}
	});
}

function CreateSalesForceStruct(cfc_path, str_asset_id, str_email, form_div, campid, assettitle){

	$.post(cfc_path + "?method=CreateSalesForceStruct&asset_title=" + assettitle + "&asset_id=" + eval(str_asset_id) + "&campaign_id=" + campid, $("#" + form_div + " > form").serializeArray(),  function(reply){

		if (reply.indexOf("already_member") >= 0){
			$("#" + form_second_part).fadeOut();
		}
		else if(reply.indexOf("bad format") >= 0){
			$("#error_reply").css({color:"#dd0000",backgroundColor:"#fbd7d7"}).html('<strong>Invalid email.</strong>');
		}
		else{
			
		}
	});
}

function ClearErrorMessages(id){
	$("#" + id).css({color:"#ffffff",backgroundColor:"#ffffff"}).html('');
}