var myPopupWindow = '';

var contactBoxMode = 'normal';
var contactBoxContents = '<h3>Call me Back</h3><p>Please enter you details in the form below and click &quot;send details&quot; to request a call back.</p><form action="<?=$_SERVER[PHP_SELF];?>" method="post" id="call-me-back-form"><div><label for="call_name">Name</label><input type="text" id="call_name" name="call_name" size="20" /></div><div><label for="call_tel">Phone</label><input type="text" id="call_tel" name="call_tel" size="20" /></div><div><label for="call_time">Time</label><input type="text" id="call_time" name="call_time" size="20" /></div>'+branchCombo+'</form><div class="feature-btn" style="margin-top: 5px;"><a href="#" title="Send Details" onclick="submitCallMeBackForm(); return false;">&gt; Send Details</a></div>';

$(document).ready(function(){

	$('img[@src$=.png]').ifixpng('/mark/aminsulations2/i/pixel.gif');

	$("input[@type=text], input[@type=password], select, textarea").focus(function(){
		$(this).parent().addClass('field-focused');
	});

	$("input[@type=text], input[@type=password], select, textarea").blur(function(){
		$(this).parent().removeClass('field-focused');
	});
	
	$(".confirm-link").click(function(){
		return confirm("Are you sure you wish to remove this "+$(this).attr("rel")+"?");
	});
	
	$(".back-btn").click(function(){
		if ($(this).attr("rel") != '') {
			window.location = $(this).attr("rel");
		}		
	});
		
});


function submitCallMeBackForm() {

		$.post("request_callback.php",{
    		call_name: $("#call_name").val(),
			call_tel: $("#call_tel").val(),
			call_time: $("#call_time").val(),
			contact_branch_callback: $("#contact_branch_callback").val()			
  		},function(xml){
			if (xml != 'SUCCESS') {
				var errors = xml.split("|");
				var error_string = '';

				for(property in errors) {
					if (errors[property] != 'ERROR') {
						error_string += errors[property] + '\n';
					}
				}

				alert(error_string);
			} else {
				callMeBack();
				alert('Your callback request has been sent.\nMany Thanks for your interest in A&M Insulations.');
			}
  		});	
}

function callMeBack() {

		(contactBoxMode == 'normal') ? contactBoxMode = 'form' : contactBoxMode = 'normal';			

		var tempContactBoxContents = $("#contact-me-box").html();
		$("#contact-me-box").html(contactBoxContents);
		contactBoxContents = tempContactBoxContents;
	
}


function openPopupWindow(url, name, width, height)
{
    //Remove special characters from name
    name = name.replace(/\/|\-|\./gi, "");

    //Remove whitespaces from name
    var whitespace = new RegExp("\\s","g");
    name = name.replace(whitespace,"");

    //If it is already open
    if (!myPopupWindow.closed && myPopupWindow.location)
    {
        myPopupWindow.location.href = url;
    }
    else
    {
        myPopupWindow= window.open(url,name, "location=no, scrollbars=yes, resizable=yes, toolbar=no, menubar=no, width=" + width + ", height=" + height );
        if (!myPopupWindow.opener) myPopupWindow.opener = self;
    }

     //If my main window has focus - set it to the popup
    if (window.focus) {myPopupWindow.focus()}
}



sfHover = function() {
	var sfEls = document.getElementById("main-navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
