/*
 * SimpleModal OSX Style Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: osx.js 238 2010-03-11 05:56:57Z emartin24 $
 */

jQuery(function ($) {
	var OSX = {
		container: null,
		init: function () {
			$(".mail-zakaz").click(function (e) {
				e.preventDefault();	
				
				$(".footer-flash-cont").hide('slow');
					
				$("#osx-modal-content").modal({
					overlayId: 'osx-overlay',
					containerId: 'osx-container',
					closeHTML: null,
					minHeight: 80,
					opacity: 65, 
					position: ['0',],
					overlayClose: true,
					onOpen: OSX.open,
					onClose: OSX.close
				});
			});
		},
		open: function (d) {
			var self = this;
			self.container = d.container[0];
			d.overlay.fadeIn('slow', function () {
				$("#osx-modal-content", self.container).show();
				var title = $("#osx-modal-title", self.container);
				title.show();
				d.container.slideDown('slow', function () {
					setTimeout(function () {
						var h = $("#osx-modal-data", self.container).height()
							+ title.height()
							+ 20; // padding
						d.container.animate(
							{height: h}, 
							200,
							function () {
								$("div.close", self.container).show();
								$("#osx-modal-data", self.container).show();
							}
						);
					}, 300);
				});
			})
		},
		close: function (d) {
			var self = this; // this = SimpleModal object
			d.container.animate(
				{top:"-" + (d.container.height() + 20)},
				500,
				function () {					
					self.close();
					$(".footer-flash-cont").show('slow');
					location.href = location.href;
					// or $.modal.close();
				}
			);
		}
	};

	OSX.init();
		
});


//------------------------------------------------------------------------------------------------------
var showIndex = 0;
function showOthers(check){
	
	if(check==0){
		if(showIndex ==0){
			$("#othersDiv").show();
			showIndex = 1;
		}
		else{
			$("#othersDiv").hide();
			$("#othersDiv").val("");
			showIndex = 0;
		}
	}
	else{
		$("#othersDiv").hide();
		$("#othersDiv").val("");
		showIndex = 0;
	}
}
//----------------------------------------------------------------------------------------------------------------------
function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}

function empty( mixed_var ) {  
    return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  ( is_array(mixed_var) && mixed_var.length === 0 ) );
}

function checkEmail(email) {
	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {	
		return false;
	}
	else{
		return true;
	}
}


function IsNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
} 
//----------------------------------------------------------------------------------------------------------------------
function sendMessage(){
	var cat = document.getElementsByName("cat[]"); 
	var text_production = $("#text_production").val();
	var list_sphere = $("#list_sphere").val();
	var user_name = $("#user_name").val();
	var user_phone = $("#user_phone").val();
	var user_email = $("#user_email").val();
	var user_place = $("#user_place").val();
	var message_text = $("#message_text").val();
	var user_firm_name = $("#user_firm_name").val();
	var ip = $("#ip").val();
	var user_firm_name = $("#user_firm_name").val();
	var hasError = new Array();
	var catArray = new Array();
	var j = 0;
	for(var i=1; i < 8; i++){
		
		if(IsNumeric($('#cat_'+i+':checked').val()) && $('#cat_'+i+':checked').val()!='undefined') {
			catArray[j]=$('#cat_'+i+':checked').val();
			j++;
		}
		else{
			
		}
	}
	
	//----------------------------------------------------------------------
	//form validation need
	
	//alert(catArray);
	if(catArray==''){
		hasError[0] = 1;
	}
	else{
		hasError[0] = 0;
	}
	
	
	if(list_sphere=="-1"){
		$("#list_sphere").addClass("errorMes");
		hasError[1] = 1;
	}
	else{
		$("#list_sphere").removeClass("errorMes");
		hasError[1] = 0;		
	}
	
	if(empty(user_name) && empty(user_firm_name)){
		$("#user_name").addClass("errorMes");
		hasError[2] = 1;
	}
	else{
		if(empty(user_name) && !empty(user_firm_name)){
			$("#user_name").removeClass("errorMes");
			hasError[2] = 0;	
		}
		else if(!empty(user_name) && empty(user_firm_name)){
			$("#user_name").removeClass("errorMes");
			hasError[2] = 0;	
		}
				
		$("#user_name").removeClass("errorMes");
		hasError[2] = 0;
	}
	
	if(empty(user_phone)){
		$("#user_phone").addClass("errorMes");
		hasError[3] = 1;
	}
	else{
		$("#user_phone").removeClass("errorMes");
		hasError[3] = 0;
	}
	
	if(empty(user_email)){
		$("#user_email").addClass("errorMes");
		hasError[4] = 1;
	}
	else{
		if(!checkEmail(user_email)){
			$("#user_email").addClass("errorMes");
			hasError[4] = 1;
		}
		else{
			$("#user_email").removeClass("errorMes");
			hasError[4] = 0;
		}
	}	
	
	if(empty(user_place)){
		$("#user_place").addClass("errorMes");
		hasError[5] = 1;
	}
	else{
		$("#user_place").removeClass("errorMes");
		hasError[5] = 0;
	}
	
	var re = /<A(.*)>/i;
	
	if(empty(message_text)){
		$("#message_text").addClass("errorMes");
		hasError[6] = 1;
	}
	else{
		//alert(message_text.search(re));
		if (message_text.search(re) != -1){
			$("#message_text").addClass("errorMes");
			hasError[6] = 1;
		}
		else{
		   $("#message_text").removeClass("errorMes");
		   hasError[6] = 0;	
		}
	}
	
	var isError = 0;
	for (var key in hasError) {
	    var val = hasError[key];
	    if(val==1){
		isError = 1;
	    }
	}
	//alert(hasError);
	if(isError ==0){
	
	document.forms.managerForm.submit();
	
	
	}
	
}
//------------------------------------------------------------------------------

