/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2008 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 170 2008-12-04 19:03:12Z emartin24 $
 *
 */

$(document).ready(function () {
	$('.getquotepopup').click(function (e1) {
		e1.preventDefault();
		// load the contact form using ajax
		$.get("/templates/buildweb20/getquote_data/contact.php", function(data1){
			// create a modal dialog with the data
			$(data1).modal({
				close: false,
				position: ["2%",],
				overlayId: 'contact-overlay1',
				containerId: 'contact-container1',
				onOpen: contact1.open,
				onShow: contact1.show,
				onClose: contact1.close
			});
		});
	});

	// preload images
	var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'getquote_img/contact/' + this;
	});
});

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

		// dynamically determine height
		var h = 485;
		if ($('#contact-subject').length) {
			h += 26;
		}
		if ($('#contact-cc').length) {
			h += 22;
		}

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

							$('#contact-container1 .contact-cc').click(function () {
								var cc = $('#contact-container1 #contact-cc');
								cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
							});

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

		/* Validation for email Starts */
		var email = $('#contact-container1 #contact-email').val();
		if (!email) {
			contact1.message += 'Email is required. ';
		}
		else {
			if (!contact1.validateEmail(email)) {
				contact1.message += 'Email is invalid. ';
			}
		}		
		/* Validation for email End */
		
		/* Validation for phone no Starts */
		var phone = $('#contact-container1 #contact-phone').val();
			if(!phone){
				contact1.message += 'Phone is required. ';
			}
		/* Validation for phone no Ends */
		
		/* Validation for Company name */
		if (!$('#contact-container1 #contact-company').val()) {
			contact1.message += 'Company name is required. ';
		}
		
		/* Validation for Services Required Start */
		var count1 = 0;
		var others = $('#contact-container1 #contact-others').val();
		for(var i=0; i<document.buildweb.idd.length; i++) 
		{
			if(document.buildweb.idd[i].checked)
			{
			count1++;
			}
		}
			if(count1<1 && others=="")
			{
			contact1.message +='Please select atleast one service or type a one on others field. ';
			}
		/* Validation for Services Required Ends */
		
		/* Validation for Url Checking Starts */
		var url1 = $('#contact-container1 #contact-url-1').val();
		var url2 = $('#contact-container1 #contact-url-2').val();
				if(!url1){
					//contact.message += 'url is required. ';
				}
				else{
					tomatch= /www.[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
					if (tomatch.test(url1))
					 {}
					 else
					 {
					contact1.message += 'Invalid url 1(See Example). ';			
					 }
				}
				
				//second url 
				if(!url2){
					//contact.message += 'url is required. ';
				}
				else{
					tomatch= /www.[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
					if (tomatch.test(url2))
					 {}
					 else
					 {
					contact1.message += 'Invalid url 2 (See Example). ';			
					 }
				}
		/* Validation for Url Checking Ends */	
		
		/* Validation for message */
		if (!$('#contact-container1 #contact-message').val()) {
			contact1.message += 'Project Description 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 () {
		$('#contact-container1 .contact-message')
			.html($('<div class="contact-error">').append(contact1.message))
			.fadeIn(200);
	}
};
