var prospectus_id = 0;
jQuery(function (jQuery) {
	var contact1 = {
		message: null,
		init: function () {
			jQuery('#contact1-form input.contact1, #contact1-form a.contact1').click(function (e) {
				e.preventDefault();

				// load the contact1 form using ajax
				jQuery.get(siteVirtualPath+"prospectus.php", function(data){
					// create a modal dialog with the data
					jQuery(data).modal({
						closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
						position: ["15%",],
						overlayId: 'contact1-overlay',
						containerId: 'contact1-container',
						onOpen: contact1.open,
						onShow: contact1.show,
						onClose: contact1.close
					});

					inputs = $('#prospectus_form').find('input[type=text], button');
					$(inputs).unbind('keydown');
					$(inputs).bind('keydown', function(e) {
						var max = $(inputs).size() - 1;
						var index = $(inputs).index($(this));
						var next = index + 1;
						next = (next > max) ? 0 : next;
						if (e.keyCode == 9) {
							$(inputs).eq(next).focus();
							return false;
						}

					});
				});
			});
		},
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if (jQuery.browser.mozilla) {
				jQuery('#contact1-container .contact1-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if (jQuery.browser.safari) {
				jQuery('#contact1-container .contact1-input').css({
					'font-size': '.9em'
				});
			}

			// dynamically determine height
			var h = 210;
			if (jQuery('#contact1-number').length) {
				h += 26;
			}
			if (jQuery('#contact1-cc').length) {
				h += 22;
			}

			var title = jQuery('#contact1-container .contact1-title').html();
			jQuery('#contact1-container .contact1-title').html('Loading...');
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						jQuery('#contact1-container .contact1-content').animate({
							height: h
						}, function () {
							jQuery('#contact1-container .contact1-title').html(title);
							jQuery('#contact1-container form').fadeIn(200, function () {
								jQuery('#contact1-container #contact1-name').focus();

								jQuery('#contact1-container .contact1-cc').click(function () {
									var cc = jQuery('#contact1-container #contact1-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('#contact1-container .contact1-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('#contact1-container .contact1-send').click(function (e) {
				e.preventDefault();
				// validate form
				if (contact1.validate()) {
					var msg = jQuery('#contact1-container .contact1-message');
					msg.fadeOut(function () {
						msg.removeClass('contact1-error').empty();
					});
					jQuery('#contact1-container .contact1-title').html('Sending...');
					jQuery('#contact1-container form').fadeOut(200);
					jQuery('#contact1-container .contact1-content').animate({
						height: '150px'
					}, function () {
						jQuery('#contact1-container .contact1-loading').fadeIn(200, function () {
							jQuery.ajax({
								url: siteVirtualPath+'prospectus.php',
								data: jQuery('#contact1-container form').serialize() + '&action=send',
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {
									jQuery('#contact1-container .contact1-loading').fadeOut(200, function () {
										//jQuery('#contact1-container .contact1-title').html('Thank You');
										jQuery('#contact1-container .contact1-title').html('Thank You<br/><br/><span style="color: #F46B22;font-size:12px;font-weight:normal;">Prospectus & Application are also available at select outlets, <a href="prospectus-application-available-at-53" style="color: #F46B22;font-size:12px;font-weight:normal;text-decoration:underline;">click here</a> to know more.</span>');
									
										msg.html(data).fadeIn(200);
									});
								},
								error: contact1.error
							});
						});
					});
				}
				else {
					
					if (jQuery('#contact1-container .contact1-message:visible').length > 0) {
						var msg = jQuery('#contact1-container .contact1-message div');
						msg.fadeOut(200, function () {
							msg.empty();
							contact1.showError();
							msg.fadeIn(200);
						});
					}
					else {
						jQuery('#contact1-container .contact1-message').animate({
							height: '30px'
						}, contact1.showError);
					}
					
				}
				
			});
		},
		close: function (dialog) {
			jQuery('#contact1-container .contact1-message').fadeOut();
			//jQuery('#contact1-container .contact1-title').html('Closing...');
			jQuery('#contact1-container .contact1-title').html('');
			jQuery('#contact1-container form').fadeOut(200);
			jQuery('#contact1-container .contact1-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							jQuery.modal.close();
							if (prospectus_id !== 0 && prospectus == 1) {									
								prospectus_link(prospectus_id);
							}
						});
					});
				});
			});
			return false;
		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		validate: function () {
			contact1.message = '';
			if (!jQuery('#contact1-container #contact1-name').val()) {
				contact1.message += 'Name is required. ';
			}
			if (jQuery('#contact1-container #contact1-name').val()) {			
					if(containsAlphabets(jQuery('#contact1-container #contact1-name').val()) == false)
					{
						contact1.message += 'Please enter a valid Name. ';
					}
			}
			
			

			var email = jQuery('#contact1-container #contact1-email').val();
			if (!email) {
				contact1.message += 'Email Address is required. ';
			}
			else {
				if (!contact1.validateEmail(email)) {
					contact1.message += 'Invalid Email Address. ';
				}
			}

			if (!jQuery('#contact1-container #contact1-number').val()) {
				contact1.message += 'Contact Number is required.';
			}
			
			if (jQuery('#contact1-container #contact1-number').val()) 
			{		
					if(IsNumeric(jQuery('#contact1-container #contact1-number').val()) == false)
					{
						contact1.message += 'Please enter a valid Contact Number.';
					}
			}	
			
			if (!jQuery('#contact1-container #contact1-city').val()) {
				contact1.message += 'State / City is required.';
			}			
			
			if (contact1.message.length > 0) {
				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('#contact1-container .contact1-message')
				.html(jQuery('<div class="contact1-error"></div>').append(contact1.message))
				.fadeIn(200);
		}
	};

	contact1.init();

});


function prospectus_link(id)
{	
	prospectus_id = 0;
	prospectus = 0;
	window.location.href= site_url + "prospectus/"+id;						
}

function containsAlphabets(checkString) {
	
		var checkString = trim(checkString,' ')
        var tempString="";
        var regExp = /^[A-Za-z \']$/;
        if(checkString != null && checkString != "")
        {
        	
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
              return false;
            }
            if(i == (checkString.length-1) && checkString.charAt(i) == "'")
            {
              return false;
            }
            if(checkString.charAt(i) == "'" && (checkString.charAt(i-1) == " " || checkString.charAt(i-1) == ""  ) )
            {
              return false;
            }
            
            if(checkString.charAt(i) == "'" && (checkString.charAt(i+1) == " " || checkString.charAt(i+1) == ""  ) )
            {
              return false;
            }            
            
          }
        }
        else
        {
          return false;
        }
        return true;
}

function IsNumeric(strTxtVal) // check for valid numeric strings
	{
	var strTxtVal = trim(strTxtVal,' ')
	var strValidChars = "0123456789+ ";
	var strChar;
	
	if (strTxtVal.length == 0) return false;
	
	// test strTxtVal consists of valid characters listed above
		for (i = 0; i < strTxtVal.length ; i++)
		{
		strChar = strTxtVal.charAt(i);
				if (strChar == "+" && i > 0)
				{				
					return false;
				}
				if (strValidChars.indexOf(strChar) == -1)
				{
					return false;
				}
		}
	return true;
}
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function google_code_prospectus()
{	
	prospectus = 1;
	var pageTracker = _gat._getTracker("UA-16773829-1");
	pageTracker._trackPageview('/downloads/map');		
	$.modal.close();	
	return false;
}



