/**
 * popup code
 */
// When you click on a link with class of poplight and the href starts with a #
function loadPopup() {
	$('a.poplight[href^=#]')
			.click(
					function() {
						// alert("hi");
						var popID = $(this).attr('rel'); // Get Popup Name
						var popURL = $(this).attr('href'); // Get Popup href to
						// define size

						// Pull Query & Variables from href URL
						var query = popURL.split('?');
						var dim = query[1].split('&');
						var popWidth = dim[0].split('=')[1]; // Gets the
						// first query
						// string value

						// Fade in the Popup and add close button
						$('#' + popID)
								.fadeIn()
								.css({
									'width' : Number(popWidth)
								})
								.prepend(
										'<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

						// Define margin for center alignment (vertical
						// horizontal) - we add 80px to the height/width to
						// accomodate for the padding and border width defined
						// in the css
						var popMargTop = ($('#' + popID).height() + 80) / 2;
						var popMargLeft = ($('#' + popID).width() + 80) / 2;

						// Apply Margin to Popup
						$('#' + popID).css({
							'margin-top' : -popMargTop,
							'margin-left' : -popMargLeft
						});

						// Fade in Background
						$('body').append('<div id="fade"></div>'); // Add the
						// fade
						// layer to
						// bottom of
						// the body
						// tag.
						$('#fade').css({
							'filter' : 'alpha(opacity=80)'
						}).fadeIn(); // Fade in the fade layer -
						// .css({'filter' :
						// 'alpha(opacity=80)'}) is used to fix
						// the IE Bug on fading transparencies

						return false;
					});

	// Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { // When clicking on the
		// close or fade layer...
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove(); // fade them both out
		});
		return false;
	});
}
function loaddatepicker() {
	$('#customer_proposed_time1').datepicker();
	$('#customer_proposed_time2').datepicker();
	$('#customer_proposed_time3').datepicker();
}
function clickBlockControlLink(elementid, displaytext, showname, hidename) {
	loadblock(elementid, displaytext, showname, hidename);
	$('#response_data').html("");
}
function loadblock(elementid, displaytext, showname, hidename) {
	// alert("here");
	if ($('#selectedRSP li').length > 0) {
		// alert("here-show:"+showname);
		// showname = "Send Email/SMS";
		showblock(elementid, displaytext, showname);
	} else {
		// alert("here-hide:"+hidename);
		// showname = "Select one or more vendors on the map and then click here
		// to submit your estimation/work request.";
		hideblock(elementid, displaytext, hidename);
	}
}
function showblock(elementid, displaytext, showname) {
	// alert(elementid);
	// var ele = document.getElementById(elementid);
	// var text = document.getElementById(displaytext);
	$(elementid).css('display', 'block'); // .style.display = "block";
	$(displaytext).html(showname);// .innerHTML = showname;
	// ele.style.display = "block";
	// text.innerHTML = showname;
}
function hideblock(elementid, displaytext, showname) {
	// alert(elementid);
	// var ele = document.getElementById(elementid);
	// var text = document.getElementById(displaytext);
	$(elementid).css('display', 'none'); // .style.display = "block";
	// $(elementid).style.display = "none";
	// alert("hideblock:"+$(displaytext).html());
	$(displaytext).html(showname);// .innerHTML = showname;
	// ele.style.display = "none";
	// text.innerHTML = showname;
}

/**
 * show default text in textbox
 */
function defaultText() {
	$(".defaultText").focus(function() {
		if ($(this).val() == $(this)[0].title) {
			$(this).removeClass("defaultTextActive");
			$(this).val("");
		}
		if ($(this)[0].type == "textarea")
			$(this).addClass("oe-textarea");
		else
			$(this).addClass("oe-textbox");
	});

	$(".defaultText").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("defaultTextActive");
			$(this).val($(this)[0].title);
		}

		if ($(this)[0].type == "textarea")
			$(this).addClass("oe-textarea");
		else
			$(this).addClass("oe-textbox");
	});

	$(".defaultText").blur();
}

function clearDefaultText() {
	$(".defaultText").each(function() {
		if ($(this).val() == this.title) {
			$(this).val("");
		}
	});
}

/**
function displayMessage(whichPanel) {
	var msg = $('#hiddenMessageText').val();
	if (msg != '') {
		if (whichPanel == 'customer') {
			$('#customerInput').css('display', 'none');
			$('#displayCustomerMessageText').css('display', 'block');
			$('#displayCustomerMessageText').val(msg)
			setTimeout(function() {
				$('#displayCustomerMessageText').val(msg).fadeOut();
			}, 1500);
			setTimeout(function() {
				hidePanel('customerPanel');
			}, 2000);
		} else if (whichPanel == 'SourcesSought') {
			// $('#vendorInput').css('display', 'none');
			setTimeout(function() {
				submitSourcesSought();
				hidePanel('vendorPanel');
			}, 3000);
		} else if (whichPanel == 'VendorShort') {
			// $('#vendorInput').css('display', 'none');
			setTimeout(function() {
				submitVendor();
				hidePanel('vendorPanel');
			}, 3000);
		}
	}
}
*/
function collapsePanel(whichPanel) {
	if (whichPanel == 'searchPanel') {
		showPanel('dummySearchPanel');
		hidePanel('searchPanel');
		$('div.oe-content').css('opacity', '1');
	} else if (whichPanel == 'submitRequestPanel') {
		showPanel('dummySubmitRequestPanel');
		hidePanel('submitRequestPanel');
		$('div.oe-content').css('opacity', '1');
	}
}

function expandPanel(whichPanel) {
	if (whichPanel == 'searchPanel') {
		hidePanel('dummySearchPanel');
		showPanel('searchPanel');
		$('div.oe-content').css('opacity', '1');
	} else if (whichPanel == 'submitRequestPanel') {
		hidePanel('dummySubmitRequestPanel');
		showPanel('submitRequestPanel');
		$('div.oe-content').css('opacity', '1');
	}
}

function isShowSubmitRequestPanel() {
	var len = $('#selectedRSP > li').size();
	// alert (len);
	if (len != 0) {
		showPanel("submitRequestPanel");
		$('div.oe-content').css('opacity', '1');
	} else {
		hidePanel("submitRequestPanel");
		hidePanel("dummySubmitRequestPanel");
	}
}

$(document).ready(function() {
	$("#searchPanel").mouseenter(function() {
		expandPanel('searchPanel');
	});
	$("#searchPanel").mouseleave(function() {
		collapsePanel('searchPanel');
	});
});

/* function to shade alternate rows of a table */

function shadeTable() {
	$('#shadeTable tr:nth-child(2n+1)').addClass('oe-shadedtable-even');
}


/*
 * jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
 * phone_number = phone_number.replace(/\s+/g, ""); return
 * this.optional(element) || phone_number.length > 9 &&
 * phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); },
 * "Please specify a valid phone number");
 * 
 * $("#myform").validate({ rules: { field: { required: true, phoneUS: true } }
 * });
 */

/**
* submit forgot password request
*/
function submitPasswordRecovery() {

	// alert("maps_url:"+ $('#myresourceurl').text());
	// alert($(maps_url));
	var PasswordRecovery_url = $('#PasswordRecovery_resource_url').attr('href');
	// alert("maps_url:"+ $('#myresourceurl').attr('href'));
	clearDefaultText();
	var customerEmailAddress = $('#SourcesSought_customerEmailAddress_Text')
			.val();

	// validate data

	var loc = window.location;

	var PasswordRecovery_xmlString = "<mechanismBean>"
			+ "  <email>" + escape(customerEmailAddress)
			+ "</email>" 
			+ "</mechanismBean>";
	alert("xmlString:" + PasswordRecovery_xmlString);

	// Open a connection to the server
	xmlHttp.open("POST", PasswordRecovery_url, true);

	// Tell the server you're sending it XML
	xmlHttp.setRequestHeader("Content-Type", "text/xml");

	// Set up a function for the server to run when it's done
	xmlHttp.onreadystatechange = passwordRecoveryHandler;

	// Send the request
	xmlHttp.send(PasswordRecovery_xmlString);

}
function passwordRecoveryHandler() {
	// alert("hi");
	if (xmlHttp.readyState == 4 /* complete */) {
		defaultText();
		displayPasswordRecoveryMessage();
	} else {
		$('#PasswordRecovery_response_data')
				.html(
						$('<span id="PasswordRecovery_request_processing" class="oe-processing"><img src="/oecrm/images/processing.gif"/>Processing....</span>'));
	}
}

function displayPasswordRecoveryMessage() {
	var message_success = "Successful";
	if (xmlHttp.responseText == message_success) {
		$('#PasswordRecovery_response_data').addClass('oe-message-info');
		$('#PasswordRecovery_response_data').html(xmlHttp.responseText);
		setTimeout(function() {
			$('#PasswordRecovery_response_data').html(xmlHttp.responseText)
					.fadeOut();
		}, 3000);
	} else {
		$('#PasswordRecovery_response_data').addClass('oe-message-error');
		$('#PasswordRecovery_response_data').html(xmlHttp.responseText);
	}
}

/**
 * load default text
 */
$(document).ready(defaultText);
$(document).ready(shadeTable);
$(document).ready(isShowSubmitRequestPanel);

$(document).ready(loaddatepicker);
$(document).ready(loadPopup);
// $(document).ready(showblock);
// $(document).ready(hideblock);
// $(document).ready(loadblock);

