/*
 * SimpleModal IRcontact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: IRcontact.js 212 2009-09-03 05:33:44Z emartin24 $
 *
 */         
jQuery.noConflict();
jQuery(document).ready(function () {

//the below code will read the anchor tag from the url 
// and then perform an action based on that url
var myFile = document.location.toString();
if (myFile.match('#')) { // the URL contains an anchor
  // click the navigation item corresponding to the anchor
  var myAnchor = '#' + myFile.split('#')[1];
  //alert("'"+myAnchor+"'");
  if(myAnchor=='#dispInfo'){
    var cContent = jQuery("#careers-content").html();
    jQuery("#basic-modal-content").html(cContent);	
    
		jQuery("#basic-modal-content").modal({overlayClose:true});
		jQuery('#simplemodal-container').css('height','610px');	
  }  
  if(myAnchor=='#contact'){
    contactModal();
  }     
}    

//end click based on url
	jQuery('#IRcontact-form input.IRcontact, #IRcontact-form a.IRcontact').click(function (e) {
	 e.preventDefault();
    contactModal();
	});
  
	// preload images
	var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
	jQuery(img).each(function () {
		var i = new Image();
		i.src = 'img/contact/' + this;
	});
});
function countryChange() {
  var countrySelected = jQuery("#IRcontact-country").val();
  //update state selection list
  //ajax function to call findState.php to retrieve state info
  jQuery.ajax({
      type:     "GET",        
      url:      "/windRx/findState.php",
      beforeSend: function(x) {
                    if(x && x.overrideMimeType) {
                     x.overrideMimeType("application/j-son;charset=UTF-8");
                    }
                   },
      dataType: "json", 
      data: "cID="+countrySelected, 
      cache:    false,
      async:    false,
      success: function(response){
        jQuery('#IRcontact-state').children().remove().end().append(response.cCtrySelect) ;
      },
      error: function(response){
        alert('Error occured! '+jQuery("errorMessage",response).text());
  }
    }); 
}

function contactModal(){
		
		// load the IRcontact form using ajax
		jQuery.get("/data/contact.php", function(data){
			// create a modal dialog with the data
			jQuery(data).modal({
				closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
				position: ["5%",],
				overlayId: 'IRcontact-overlay',
				containerId: 'IRcontact-container',
				onOpen: IRcontact.open,
				onShow: IRcontact.show,
				overlayClose:['true'],				
				onClose: IRcontact.close,
				containerCss:{
      		position: 'absolute'
      	}  

			});
		});
}
var IRcontact = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if (jQuery.browser.mozilla) {
			jQuery('#IRcontact-container .IRcontact-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if (jQuery.browser.safari) {
			jQuery('#IRcontact-container .IRcontact-input').css({
				'font-size': '.9em'
			});
		}

		// dynamically determine height
		var h = 550;

		if (jQuery('#IRcontact-cc').length) {
			h += 22;
		}

		var title = jQuery('#IRcontact-container .IRcontact-title').html();
		jQuery('#IRcontact-container .IRcontact-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					jQuery('#IRcontact-container .IRcontact-content').animate({
						height: h
					}, function () {
						jQuery('#IRcontact-container .IRcontact-title').html(title);
						jQuery('#IRcontact-container form').fadeIn(200, function () {
							jQuery('#IRcontact-container #IRcontact-name').focus();

							jQuery('#IRcontact-container .IRcontact-cc').click(function () {
								var cc = jQuery('#IRcontact-container #IRcontact-cc');
								cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
							});

							// fix png's for IE 6
							if (jQuery.browser.msie && jQuery.browser.version < 7) {
								jQuery('#IRcontact-container .IRcontact-button').each(function () {
									if (jQuery(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										jQuery(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		jQuery('#IRcontact-container .IRcontact-send').click(function (e) {
			e.preventDefault();
			// validate form
			if (IRcontact.validate()) {
				var msg = jQuery('#IRcontact-container .IRcontact-message');
				msg.fadeOut(function () {
					msg.removeClass('IRcontact-error').empty();
				});
				jQuery('#IRcontact-container .IRcontact-title').html('Sending...');
				jQuery('#IRcontact-container form').fadeOut(200);
				jQuery('#IRcontact-container .IRcontact-content').animate({
					height: '80px'
				}, function () {
					jQuery('#IRcontact-container .IRcontact-loading').fadeIn(200, function () {
						jQuery.ajax({
							url: '/data/contact.php',
							data: jQuery('#IRcontact-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							success: function (data) {
								jQuery('#IRcontact-container .IRcontact-loading').fadeOut(200, function () {
									jQuery('#IRcontact-container .IRcontact-title').html('Thank you!');
									msg.html(data).fadeIn(200);
								});
							},
							error: IRcontact.error
						});
					});
				});
			}
			else {
				if (jQuery('#IRcontact-container .IRcontact-message:visible').length > 0) {
					var msg = jQuery('#IRcontact-container .IRcontact-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						IRcontact.showError();
						msg.fadeIn(200);
					});
				}
				else {
					jQuery('#IRcontact-container .IRcontact-message').animate({
						height: '65px'
					}, IRcontact.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		jQuery('#IRcontact-container .IRcontact-message').fadeOut();
		jQuery('#IRcontact-container .IRcontact-title').html('Goodbye...');
		jQuery('#IRcontact-container form').fadeOut(200);
		jQuery('#IRcontact-container .IRcontact-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						jQuery.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		IRcontact.message = '';
		if (!jQuery('#IRcontact-container #IRcontact-name').val()) {
			IRcontact.message += 'Name missing, ';
		}
		
    IRcontact.address = '';
		if (!jQuery('#IRcontact-container #IRcontact-address').val()) {
			IRcontact.message += 'Address missing,';
		}
		
		var email = jQuery('#IRcontact-container #IRcontact-email').val();
		if (!email) {
			IRcontact.message += 'Email missing, ';
		} else {
			if (!IRcontact.validateEmail(email)) {
				IRcontact.message += 'Email invalid, ';
			}
		}

		var verifyEmail = jQuery('#IRcontact-container #IRverifyEmail').val();
		if (!verifyEmail) {
			IRcontact.message += 'Verify Email missing,';
		} else {
			if (!IRcontact.validateEmail(verifyEmail)) {
				IRcontact.message += 'Verify Email invalid. ';
			}
		}
		
		if(email != verifyEmail){
		  IRcontact.message += 'Entered Emails do not match, ';
    }
		if (!jQuery('#IRcontact-container #IRcontact-message').val()||
         jQuery('#IRcontact-container #IRcontact-message').val()=='How can we help?') {
			IRcontact.message += 'Message is required,';
		}

		if (IRcontact.message.length > 0) {
			IRcontact.message = 'Please correct the following issues: '+IRcontact.message;
      return false;
		}
		else {
			return true;
		}
	},
	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;	

		return true;
	},
	showError: function () {
		jQuery('#IRcontact-container .IRcontact-message')
			.html(jQuery('<div class="IRcontact-error"></div>').append(IRcontact.message))
			.fadeIn(200);
	}
};

       

         
        
