/*
 * 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 132 2008-05-23 16:05:17Z emartin24 $
 *
 */

$(document).ready(function () {
	//$('#contactLink, #contactForm input:eq(0)').click(function (e) {
    $('#contactForm').click(function (e) {															  
		e.preventDefault();
		// load the contact form using ajax
		$.get("data/career.html", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});

	// preload images
	var img = ['cancel.png','form_bottom.gif','form_top.gif','form_top_ie.gif','loading.gif','send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'img/contact/' + this;
	});
});

var contact = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#contact-container .contact-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#contact-container .contact-input').css({
				'font-size': '.9em'
			});
		}

		var title = $('#contact-container .contact-title').html();
		$('#contact-container .contact-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contact-container .contact-content').animate({
						 height: 280 // This is Body height ... previously it was 260
					}, function () {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(200, function () {
							$('#contact-container #contact-name').focus();

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#contact-container .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 (dialog) {
		$('#contact-container .contact-send').click(function (e) {
			e.preventDefault();
			// validate form
			
			if (contact.validate()) {
				$('#contact-container .contact-message').fadeOut(function () {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('Sending...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '80px'
				}, function () {
					$('#contact-container .contact-loading').fadeIn(200, function () {
						$.ajax({
							url: 'data/success.jsp',
							data: $('#contact-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
								$('#contact-container .contact-loading').fadeOut(200, function () {
									$('#contact-container .contact-title').html('Thank you!');
									$('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contact-container .contact-message:visible').length > 0) {
					var msg = $('#contact-container .contact-message div');
					msg.fadeOut(200, function () {
						msg.empty();
						contact.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#contact-container .contact-message').animate({
						height: '30px' // This is Error Message height Previously It was 30px
					}, contact.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('byebye...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	
	validate: function () {
		contact.message = '';
	

// title starts frm here
	
	  if (document.getElementById('title').value == "")
	  {
		valerr = true;
		//contact.message += 'Title is required. ';
		alert("Please Enter Title" );
		 document.getElementById('title').focus();
		 
		 return false;
	  }
      else if (document.getElementById('title').value != "") 
      {
        if(contact.character(document.getElementById('title')) == false)
		{
		  valerr = true;	
		  alert(" Invalid Title");
          document.getElementById('title').value="";
		  document.getElementById('title').focus();
           return false;
        }
      }
	  
	  //validation of name field 
      if (document.getElementById('name').value == "") 
	  {    valerr = true;
		   alert("Please Enter Candidate Name");
		   document.getElementById('name').focus();
		   return false;
      }
      else if (document.getElementById('name').value != "")
	  {
		    if (contact.validateName(document.getElementById('name')) == false) 
		    {
			   valerr = true;	 
		       alert("Invalid Name!  Only characters  are accepted!");
               document.getElementById('name').value="";
		       document.getElementById('name').focus();
               return false;
            }
      }
	  
	  //validation of company field        
      if (document.getElementById('company').value == "") 
	  {    valerr = true;
		  alert("Please Enter Company Name");
		  document.getElementById('company').focus();
		  return false;
      }
      if (document.getElementById('company').value != "")
	  {
		 if(contact.validatecompany(document.getElementById('company'))==false)
         {
			 valerr = true;
             alert("Invalid Company Name");
	         document.getElementById('company').value="";
             document.getElementById('company').focus();
             return false;
         }
	  } 
	  
	  
	  //validation of address field
      if(document.getElementById('address').value== "")
     {
		 valerr = true;
         alert("Please Enter Address");
	     document.getElementById('address').focus();
	     return false;
	 }

//validation of mobile number
     if (document.getElementById('mobile').value == "") 
	 {
		 valerr = true;
		alert("Please Enter Mobile Number");
		document.getElementById('mobile').focus();		
		return false;
	 }
     else if(document.getElementById('mobile').value != "")
    {
		
          if(contact.IsNumeric(document.getElementById('mobile').value) == false)
          {
			  valerr = true;
              alert("Invalid Mobile Number");
              document.getElementById('mobile').focus();
	          document.getElementById('mobile').value="";
              return false;
          }
	      if((document.getElementById('mobile').value.length<10) || (document.getElementById('mobile').value.length>=20))
          {
			  valerr = true;
              alert("Mobile Number should be between min 10 and max 20 digits");
              document.getElementById('mobile').focus();
		      document.getElementById('mobile').value="";
              return false;
          }   
     }
	 
	 
	 //validation of STD code number
     if(document.getElementById('STD_Tel').value != "")
     {
        if(contact.stdvalid(document.getElementById('STD_Tel').value) == false)
        {
			valerr = true;
            alert("Invalid STD Code Number");
            document.getElementById('STD_Tel').focus();
	        document.getElementById('STD_Tel').value="";
            return false;
        }
      }

//validation of Landline number
     if(document.getElementById('LAND_Tel').value != "")
     {
         if(contact.Landphone(document.getElementById('LAND_Tel').value) == false)
         {
			 valerr = true;
             alert("Invalid Phone Number");
             document.getElementById('LAND_Tel').focus();
	         document.getElementById('LAND_Tel').value="";
             return false;
          }
     }

////vaidation of Fax field
//     if(document.getElementById('STD_fax').value != "")
//     {
//        if(contact.stdfaxvalid(document.getElementById('STD_fax').value) == false)//,"0123456789()"))
//        {
//            alert("Invalid STD Code Number");
//            document.getElementById('STD_fax').focus();
//	        document.getElementById('STD_fax').value="";
//            return false;
//        }
//      }
//
//      if(document.getElementById('FAX').value != "")
//      {
//         if(contact.Landfaxphone(document.getElementById('FAX').value) == false)
//         {
//             alert("Invalid Fax Number");
//             document.getElementById('FAX').focus();
//	         document.getElementById('FAX').value="";
//             return false;
//          }
//       }
//	   
	   
	   //validation of Emailid field	 
       if (document.getElementById('email_id').value == "") 
	   {
		   valerr = true;
		   alert("Please Enter Email ID");
		   document.getElementById('email_id').focus();
		   return false;
	    }
		else if (document.getElementById('email_id').value != "") 
		{
             if(contact.echeck(document.getElementById('email_id').value)==false)
             {
				 valerr = true;
                  alert("Invalid Email ID");
                  document.getElementById('email_id').focus();
                  document.getElementById('email_id').value="";
                  return false; 
             }
        }
        if (document.getElementById('thought_post').value == "") 
		{
			valerr = true;
		     alert("Please Enter Thoughts");
		     document.getElementById('thought_post').focus();
		     return false;
	    }
        if (document.getElementById('found_site').options[document.getElementById('found_site').selectedIndex].value=="select") 
		{   
		   valerr = true;
	        alert("Please select how you know this Site");
	        document.getElementById('found_site').focus();
		    return false;	
        }

      if(valerr == true)
	  {
		  //alert('hell')
		  //document.Feedbackfrm.submit();
		  document.location.href = 'http://Recent Uploaded UFT website_10.2.09/index.php?act=feed'
	  }
	  //document.location.href = 'http://Recent Uploaded UFT website_10.2.09/index.php' 
		 
	  
	  
	  //;
	  //window.location.href = 'http://Recent Uploaded UFT website_10.2.09/index.php'
	  //document.location.action='http://Recent Uploaded UFT website_10.2.09/index.php'
	 // document.Feedbackfrm.action="http://192.168.0.25:8080/uftech/Lookup.do";
	  //document.Feedbackfrm.submit();
  
	  
    },



//Function to include Confirmation page
confirmSubmit: function()
{
  var agree;
  agree=confirm("Are you sure you wish to continue?");
  if (agree)
	return true; 
  else
	return false ;
},

//function to validate title field
character: function(str)
{
  var validchar ="ABCDEFGHIJKLMNO PQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var valid="ABCDEFGHIJKLMNO PQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.*&";
  // str1=str.length;
  if(validchar.indexOf(str.value.charAt(0)) == -1)
  {
      return false;
  }
  for(i = 0; i < str.value.length; i++)
   {
     if(valid.indexOf(str.value.charAt(i)) == -1)
	 {
	    return false;
	 }
   }
   return true;
},

//funtion to validate name field
validateName: function(field)
 {
var valid = "ABCDEFGHIJKLMNO PQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.";
 for(i=0;i<field.value.length;i++)
 {
     if(valid.indexOf(field.value.charAt(i))==-1)
     {
        return false ;
	 }
   }
 },  

//function to validate company field
validatecompany: function(str)
{
  var validchar ="ABCDEFGHIJKL MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var valid="ABCDEFGHIJKLMNOP QRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-*.,()&";
  // str1=str.length;
  if(validchar.indexOf(str.value.charAt(0)) == -1)
  {
      return false;
  }
  for(i = 0; i < str.value.length; i++)
   {
     if(valid.indexOf(str.value.charAt(i)) == -1)
	 {
	    return false;
	 }
  }
},

//function to validate telephone std code number

stdvalid: function(sText)
{
   var ValidChars = "012345 6789()";
   var Char;
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) 
       {
             return false;
       }
   }
}, 


//funtion to validate mobile number

IsNumeric: function(sText)
{
   var ValidChars = "0123456789+";
   var Char;
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) 
       {
             return false;
       }
   }
}, 
 
// function to validate landline number

Landphone: function(sText)
{
   var ValidChars = "0123456789/";
   var Char;
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) 
       {
             return false;
       }
   }
}, 

//function to validate fax std code number

stdfaxvalid: function(sText)
{
   var ValidChars = "012345 6789()";
   var Char;
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) 
       {
             return false;
       }
   }
}, 

// function to validate fax number

Landfaxphone: function(sText)
{
   var ValidChars = "0123456789/";
   var Char;
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
       if (ValidChars.indexOf(Char) == -1) 
       {
             return false;
       }
   }
},

//Function to validate email id
echeck: function(str)//mail validation
{
  if(str.length!="")
   {
	 var suffix = str.substring(str.lastIndexOf('.')+1);	 		      								  
      if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
	   {
				
			 	    return false;
	   }
	   if ((suffix.length != 2) &&(suffix !='com')&&(suffix !='net') && (suffix !='org') && (suffix !='edu' )&&( suffix !='int') && (suffix !='mil' )&& (suffix !='gov') && (suffix !='arpa') &&(suffix !='biz' )&& (suffix !='aero') && (suffix !='name') && (suffix !='coop') && (suffix !='info') && (suffix !='pro') && (suffix !='museum')) 
      {
          alert('Invalid primary domain in Email address');
          return false;
      }				
    } 
},
	



	
showError: function () {
$('#contact-container .contact-message')
	.html($('<div class="contact-error">').append(contact.message))
	.fadeIn(200);
}
	
	
};






