// EMAIL OBFUSCATOR

function printEmail(user,domain,disptext) {
  if (!domain) {
	  domain = 'awd.com.au';
  }
  if (!disptext) {
	  disptext = user + '@' + domain;
  }
  myLink = '<a href="mailto:'+user+'@'+domain+'">'+disptext+'</a>';
  
  return myLink;
}



// TOP SEARCH BAR FUNCTIONS

function searchFocus() {

  $('#top_search').removeClass();
		$('#top_search').addClass('focus');
		
}

function searchBlur() {
  var myValue = document.getElementById('top_search_field').value;
		
		if (myValue == ''){
			 $('#top_search').removeClass();
		}

}


// CONTENT SLIDER

function doSlide(showThisDiv,showThisHash) {
	  if (!$('#'+showThisDiv).length) {
					showThisDiv = $("#body_content > h2 + div:first").attr('id');
					myIndex = showThisDiv.indexOf('_') + 1;
					showThisHash = $('#'+showThisDiv).attr('id').substr(myIndex);
			}
	  $("div.slider").slideUp('normal', function() {
																																											  if ($(this).attr('id') == showThisDiv){
																																											    $('#'+showThisDiv).slideDown('normal');;
																																											  }
																																											});
			window.location.hash = showThisHash;
}


// PORTFOLIO FADER

function doFade(showThisDiv,showThisHash) {
	  if (!$('#'+showThisDiv).length) {
					showThisDiv = $("#portfolio_container div:first").attr('id');
					myIndex = showThisDiv.indexOf('_') + 1;
					showThisHash = $('#'+showThisDiv).attr('id').substr(myIndex);
			}
	  $("div#portfolio_container div:visible").fadeOut(500,function() {
																																																																		  $('#'+showThisDiv).fadeIn(500);
																																																																		});
			window.location.hash = showThisHash;
}


if (typeof(thisPage) != 'undefined') {
		$(document).ready(function () {
																														  if (window.location.hash.length > 1) {
																																  var myParent = $("#hr_"+window.location.hash.substr(1)).parent().attr("id");
																																  doSlide(myParent,window.location.hash)
																																} else {
																																	 $("#portfolio_container div:first").fadeIn();
																																}
																														});
} else if ($("body").attr('class') != 'home') {
		$(document).ready(function () {
																														  if (window.location.hash.length > 1) {
																																  var myParent = $("#hr_"+window.location.hash.substr(1)).parent().attr("id");
																																  doSlide(myParent,window.location.hash)
																																} else {
																																	 $("#body_content > h2 + div:first").slideDown('normal');
																																}
																														});
}




// SITE MAP LISTENERS
$(".footer_links").hover(function(){
																																		  $(this).addClass("hover");
																																				alert('Hover!');
																																		},
																																		function(){
																																			 $(this).removeClass();
																																		});



// INTERNET EXPLORER < v7 PNG TRANSPARENCY FIX
if (document.all && /MSIE (5\.5|6)/.test(navigator.userAgent) &&
  document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule)
{
  document.styleSheets[0].addRule('a', 'behavior: url(/library/ie_png/iepngfix.htc)');
		document.styleSheets[0].addRule('div', 'behavior: url(/library/ie_png/iepngfix.htc)');
  document.styleSheets[0].addRule('img', 'behavior: url(/library/ie_png/iepngfix.htc)');
  document.styleSheets[0].addRule('span', 'behavior: url(/library/ie_png/iepngfix.htc)');
}




// CUSTOM CSS DEFINITIONS
document.getElementById('boxy_css').href = '/library/styles/boxy.css';



// PROPOSAL REQUEST FORM SUBMISSION
function doProposalRequest() {
		
		var notNullFields =     new Array('proposal_name','proposal_email','proposal_phone','proposal_business_industry','proposal_description');
		var notNullFieldNames = new Array('Your name',    'E-mail address','Telephone',     'Business industry',         'Proposal description');
		var notNullFails  =     new Array();
		var submitFields =      new Array('proposal_name','proposal_company','proposal_email','proposal_phone','proposal_business_industry','proposal_service_webdesign','proposal_service_graphicdesign','proposal_service_webhosting','proposal_service_ecommerce','proposal_cms_self','proposal_cms_awd','proposal_timeline','proposal_description','proposal_contact_method','proposal_contact_time');
		
		// Validate not-null fields
		noFails = 0;
		for (i=0; i<notNullFields.length; i++){
				if (document.getElementById(notNullFields[i]).value == ''){
				  notNullFails[noFails] = notNullFieldNames[i];
						noFails++;
				}
		}
		
		if (notNullFails.length > 0){
			 failString = '';
		  for (i=0; i<notNullFails.length; i++){
					 failString = failString + '&nbsp;&bull;' + notNullFails[i] + '<br />';
				}
				Boxy.alert("<p>Not all of the required fields have been filled out. You still need to fill out the following fields:</p><p>" + failString + "</p>", null, {title: "Request a proposal", draggable: false});
				
				return false;
		}


		for (i=0; i<submitFields.length; i++){
			 if (i==0){
				formContent = '{' 	+ submitFields[i] + ': "' + document.getElementById(submitFields[i]).value + '"';
				} else {
			 formContent = formContent + ',' + submitFields[i] + ': "' + document.getElementById(submitFields[i]).value + '"';
				}
		}
		formContent = formContent + '}';
		
		// Submit form
		$("div#contactus_request_a_proposal p:first").slideUp("fast");
		$("div#proposal_compulsory_message").slideUp("fast");
		$("form#proposal_form").slideUp("fast");
		$("div#form_submit_progress").slideDown("fast");
		$.post("/library/ajax/request_proposal.php", formContent, function(data){
																																																																					  if(data.status != 'success') {
																																																																								 $("div#form_submit_success p:first").html('There was an error submitting your request. Please try again later.');
																																																																							}
																																																																							$("div#form_submit_progress").slideUp("fast");
																																																																							$("div#form_submit_success").slideDown("fast");
																																																																					}, "json");
		
}



// AJAX FUNCTIONS

function validateCredentials(myForm) {
	 if (myForm == 'modal_login_form') {
			 var username = document.getElementById('modal_username').value;
		  var password = document.getElementById('modal_password').value;
			 loginBox.resize(230,45,
																		  function() {
																					 loginBox.setContent("<div class=\"loader\" id=\"message_box\">Checking your credentials...</div>");
																						loginBox.center();
																				}
																		);
		}
		
		jQuery.post("https://secure.awd.com.au/library/ajax/login.php", { u: username, p: password },
															function(data) {
																	if (data.status == 'change_pass') {
																		 loginBox.hide();
																			myUsername = data.username;
																			oldPassword = data.oldPassword;
																		 displayChangePassword();
																	} else if (data.status == 'success') {
																			window.location = 'https://secure.awd.com.au/my_account/';
																	} else {
																			displayLoginError(data.message);
																	}
															}, "json");
		
		return false;
		
}

function createModalLoginBox() {
  loginBox = new Boxy("<div class=\"login_box\" id=\"login_box\"><span id=\"my_message\">Please enter your e-mail address and password.</span><form id=\"modal_login_form\"><label for=\"modal_username\">E-mail</label><input type\"text\" id=\"modal_username\" name=\"u\" title=\"E-mail\" /><label for=\"modal_password\">Password</label><input type=\"password\" id=\"modal_password\" name=\"p\" title=\"Password\" /><a href=\"#view\" onclick=\"Boxy.get(this).hide();\" id=\"cancel\">Cancel</a><a href=\"#login\" onclick=\"validateCredentials('modal_login_form'); return false;\" id=\"login\">Login</a></form></div>", {modal: true, closeable: false, draggable: false} );
		loginBox.show();
		document.getElementById('modal_username').focus();
		return false;
}

function displayLoginError(myError) {
	 jQuery("#message_box").fadeOut(100);
		loginBox.resize(400,235);
		loginBox.setContent("<div class=\"login_box\" id=\"login_box\"><span id=\"my_error\">"+myError+"</span><form id=\"modal_login_form\"><label for=\"modal_username\">E-mail</label><input type\"text\" id=\"modal_username\" name=\"u\" title=\"E-mail\" /><label for=\"modal_password\">Password</label><input type=\"password\" id=\"modal_password\" name=\"p\" title=\"Password\" /><a href=\"#view\" onclick=\"Boxy.get(this).hide();\" id=\"cancel\">Cancel</a><a href=\"#login\" onclick=\"validateCredentials('modal_login_form'); return false;\" id=\"login\">Login</a></form></div>");
		loginBox.center();
		jQuery("#login_box").fadeIn(100);
		document.getElementById('modal_username').value = document.getElementById('login_username').value;
		document.getElementById('modal_username').focus();
		return true;
}

function confirmLogout() {
  Boxy.confirm("Are you sure you want to log out?",
																																																	function() {
																																																			 window.location = '/logout/';
																																																		 }, {modal: true, closeable: false, draggable: false});
}