 $(document).ready(function () {
	$("#validateioncodehint").simpletip({ 
			content:'Please enter the 4 digit validation code received on your mobile'
		}); 
} );


var insertData = function() {
	var allVals = [];
	 $('[name=00N20000002VKIu]:checked').each(function() {
		allVals.push($(this).val());
	});

	var choice = allVals.join(',') ;
	$("<input type='hidden' value='"+choice+"' name='choice'>").appendTo("#lodhaForm");
	var orghtml =$('#contactbox_submit').html();
	$('#contactbox_submit').html("<img src='images/processing.gif' border='0' ") ;

	$.ajax({
		url: 'wp-enquiry.php',
		data: $('#lodhaForm').serialize() + '&action=send',
		type: 'post',
		cache: false,
		dataType: 'html',
		complete: function (xhr) {
			var response = xhr.responseText ;
			
			if(response == 'SUCCESS') {
				if($('#countrycombo').val() == '0091' ) {
					confrence.initCallPatch();
				}
				else {
				   document.lodhaForm.submit();
				}
			}
			else {
				$('#contactbox_submit').html(orghtml) ;
			}
		}
	});
}
var confrence = {
	oldMoblieNo: null ,
	oldVerificationCode :null ,
	ajaxinProgress : 0 ,
	verificationajaxinprogress :0,
	__verified :false,
	onlyNumbers: function(evt){
		try{
			var e = event || evt; // for trans-browser compatibility
			var charCode = e.which || e.keyCode;
		
			if (charCode > 31 && (charCode < 48 || charCode > 57))
			return false;

			return true;
		}
		catch(err) {
		}
	},
	generateValidationCode: function(){
		
		if($('#countrycombo').val() != '0091' ) {
			$('#validateioncodediv').css({
				'display': 'none' 
			});
			confrence.activeSubmitButton();
				
			return false ;
		}
		else {
			confrence.deactiveSubmitButton();
			$('#validateioncodediv').css({
				'display': 'block' 
			});	
			$('#mobile_contact_text').css({
				'display': 'none' 
			});	
			$('.mobile_contact_text').css({
				'display': 'none' 
			});	
		}

		/** Mobile No Validation */


		var mobileno = $('#00N20000001SJmb1').val();
			
		if (!mobileno) {
			return false ;
		}
		else if (!riocontact.isDigit(mobileno)){
			$.achtung({message: 'Please enter a valid Mobile number.', timeout: 5}); 	return false ;
		}
		else if(riocontact.isBlankSpaces(mobileno)){
			$.achtung({message: 'Mobile No should not contain Blank Spaces ', timeout: 5});			return false ;
		}
		else if (mobileno.substr(0, 1) == "0") {
			$.achtung({message: 'Mobile no. should not start with 0.', timeout: 5});	 	return false ;
		}
		else if(mobileno.length < 10 ){
			$.achtung({message: 'Please check your mobile number.', timeout: 5});		return false ;
		}

	
		if(this.oldMoblieNo !=  mobileno) {
			this.ajaxinProgress = 1	;
			confrence.__verified = false ;
			
			// Create a new sticky notification, save as foo
			alertmsg = $.achtung({message: 'Sending Validation code ...', timeout: 0});
		
			this.oldMoblieNo = mobileno ;

			$.ajax({
			url: 'wp-confrence.php',
			data: $('#lodhaForm').serialize() + '&action=randomnumber',
			type: 'post',
			cache: false,
			dataType: 'html',

			complete: function (xhr) {
					var response = xhr.responseText.split('#') ;
					
					$('#validationcode').val(response[1]) ;
		   			
					if(xhr.responseText.search(/error/) >=0) {
						// Update with new message
						confrence.oldMoblieNo = null ;
						alertmsg.achtung('update', {message: 'Not sent Successfully ',timeout: 2});

					}
					else {
						// Update with new message
						alertmsg.achtung('update', {message: 'Successfully sent!',timeout: 2});
					}
				

					if($('#countrycombo').val() == '0091' ) {


						$('#validateioncodediv').css({
							'display': 'block' 
						});	
						$('#mobile_contact_text').css({
							'display': 'none' 
						});	
						$('.mobile_contact_text').css({
							'display': 'none' 
						});	
					}
					else {
					 	
						$('#validateioncodediv').css({
							'display': 'none' 
						});

					
					}
					confrence.ajaxinProgress = 0 ;
				}
			});
		} else {
			 	$('#validateioncodediv').css({
							'display': 'block' 
				});	
				$('#mobile_contact_text').css({
					'display': 'none' 
				});	
				$('.mobile_contact_text').css({
					'display': 'none' 
				});	
		}
	},
	matchValidationCode : function(caller){
		if($('#countrycombo').val() != '0091' ) {
			return true ;

		}
		
		if(!$('#smsvalidationcode').val() ) {
			confrence.deactiveSubmitButton();
			return false ;
		}
		if($('#smsvalidationcode').val().length != 4  ) {
			confrence.deactiveSubmitButton();
			return false ;
		}
		if(confrence.verificationajaxinprogress == 0 && (this.oldVerificationCode != $('#smsvalidationcode').val())) {
			confrence.deactiveSubmitButton();
			confrence.verificationajaxinprogress = 1 ;
			this.oldVerificationCode = $('#smsvalidationcode').val() ;
			// Remove all notifications
			if(typeof vmsg != 'undefined')
			vmsg.achtung('close');
			
			vmsg = $.achtung({message: 'Verifying Validation code, Please wait ...', timeout: 0});	
		
			$.ajax({
				url: 'wp-confrence.php',
				data: $('#lodhaForm').serialize() + '&action=matchvalidationcode',
				type: 'post',
				cache: false,
				dataType: 'html',
				complete: function (xhr) {
						var response = xhr.responseText ;
						confrence.verificationajaxinprogress = 0;
						if(response == 'SUCCESS') {
							vmsg.achtung('update', {message: 'Validation code verified successfully' ,timeout:1});
							confrence.activeSubmitButton();
							confrence.__verified = true ;
							if(caller == 'validation') {
							  validation('matchValidationCode');
							}
						}
						else {
							vmsg.achtung('close');
							confrence.__verified = false ;
								$("#dialog").html('Validation code does not match, please re enter correct code') ;
								$('#dialog').dialog('open');
								//$.achtung({message: 'Validation code does not match, please re enter correct code', timeout: 2});	
								return false ;
						}

					}
				});
		 }
		
		
	},
	initCallPatch: function() {

	  	$.ajax({
			url: 'wp-confrence.php',
			data: $('#lodhaForm').serialize() + '&action=patchcall',
			type: 'post',
			cache: false,
			dataType: 'html',
			complete: function (xhr) {
					var response = xhr.responseText.split('#') ;
					if(confrence.ajaxinProgress == 0) {
						document.lodhaForm.submit();
					}
					return true ;
				}
		});
	
	} ,
	activeSubmitButton : function() {
		$('#submitactive').css({
			'display': 'block' 
		});
		$('#submitdisabled').css({
			'display': 'none' 
		});
				
	} ,
	deactiveSubmitButton : function() {
		$('#submitactive').css({
			'display': 'none' 
		});
		$('#submitdisabled').css({
			'display': 'block' 
		});
				
	}
	
}
$(function() {
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true
	});
}) ;

function showSelected() {

	var selObj = document.getElementById('countrycombo');
	var txtIndexObj = document.getElementById('txtIndex');
	var txtValueObj = document.getElementById('mcode');
	var txtTextObj = document.getElementById('txtText');
	//var txtlcountry = document.getElementById('lcountry');

	var selIndex = selObj.selectedIndex;
	txtIndexObj.value = selIndex;
	txtValueObj.value = selObj.options[selIndex].value;
	//txtlcountry.value = selObj.options[selIndex].value;
	var country = selObj.options[selIndex].text;
	document.getElementById('00N20000001SJmA').value = country;
	document.getElementById('00N20000001SJmb').value = txtValueObj.value;

}


var riocontact = {
	emptyname : 'Please enter your Name',
	message : null ,
	validatePancardno : function(panno) {
		var regexvalidpanno=/^[A-Z]{5}\d{4}[A-Z]{1}$/;  //this is the pattern of regular expersion
		
		if ( regexvalidpanno.toUpperCase().test(panno) == false) {
			return false ;
		}
		else if (panno.length > 10 ){
			return false ;
		}
		return true ;
	},
	isDigit: function(s){
		var i = 0; 
			while( i < s.length ){
				var c = s.charAt(i); 
					if((c >= "0") && (c <= "9")){
						i++; continue;
					} 
					else 
						return false;
			} 
		return true;
	},
	onlyNumbers: function(evt){
		var e = event || evt; // for trans-browser compatibility
		var charCode = e.which || e.keyCode;
		
		if (charCode > 31 && (charCode < 48 || charCode > 57))
			return false;

		return true;
	},
	isLetterAndBlank : function(s) {
		var i = 0;
		while (i < s.length) {
			var c = s.charAt(i);
			if (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == " ")) {
				i++;
				continue;
			} else return false;
		}
		return true;
	},
	isLetter :function (c) {
		if (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z"))) return true;
		else return false;
	},
	isEmpty	:function (s) {
		return ((s == null) || (s.length == 0));
	}	,
	isDigit :function (s) {
		var i = 0;
		while (i < s.length) {
			var c = s.charAt(i);
			if ((c >= "0") && (c <= "9")) {
				i++;
				continue;
			} else return false;
		}
		return true;
	},

	isBlankSpaces:function (s) {
		var i = 0;
		while (i < s.length) {
			if (s.charAt(i) == " ") {
				i = i + 1;
				continue;
			} else return false;
		}
		return true;
	} ,
	isName :function (s) {
		return (s.match("Name"));
	} ,
	isCity : function (s) {
		return (s.match("city"));
	},
	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;
	}


};

function validation(caller) {
	riocontact.message = '';

	/** Name Validation */
	var name = $('#last_name').val();
	
	if (!$('#last_name').val()) {
		riocontact.message += 'Please enter Name. ';
		riocontact.message +="<br/>" ;
	}

	else if (riocontact.isEmpty(name) || riocontact.isBlankSpaces(name) || riocontact.isName(name) ||(!riocontact.isLetterAndBlank(name)) ) {
		riocontact.message += 'Please enter valid Name.';
		riocontact.message +="<br/>" ;
	}

	/** Email Validataion */
	
	var email = $('#contactrio-email').val();
	
	if (!email) {
		riocontact.message += 'Email Id is required. ';
		riocontact.message +="<br/>" ;
	}
	else {
		if (!riocontact.validateEmail(email)) {
			riocontact.message += 'Enter valid Email Id . ';
			riocontact.message +="<br/>" ;
		}
	}

	/**  Country Validation */

	if (!$('#countrycombo').val()) {
		riocontact.message += 'Please select Country . ';
		riocontact.message +="<br/>" ;
	}

	/** City Validation */

	var city = $('#CityR').val();

	city = city.slice(0, 1).toUpperCase() + city.slice(1); 

	if (!$('#CityR').val()) {
		riocontact.message += 'Please enter City Name. ';
		riocontact.message +="<br/>" ;
	}
	
	else if (riocontact.isEmpty(city) || riocontact.isBlankSpaces(city) || riocontact.isCity(city)) {
		riocontact.message += 'Please enter a valid city. '; 
		riocontact.message +="<br/>" ;
	}

	else if (!riocontact.isLetterAndBlank(city)) {
		riocontact.message += 'Please enter a valid city. ';
		riocontact.message +="<br/>" ;		
	} 
 
	/** Mobile No Validation */
	

	var mobileno = $('#00N20000001SJmb1').val();
		
	if (!mobileno) {
		riocontact.message += 'Mobile No is required.';
		riocontact.message +="<br/>" ;
	}
	else if (!riocontact.isDigit(mobileno)){
		riocontact.message += 'Enter valid Mobile No .';
		riocontact.message +="<br/>" ;
	}
	else if(mobileno.length < 10  ){
		riocontact.message += 'Please check your mobile number.';
		riocontact.message +="<br/>" ;
	}

	var mobc = document.getElementById('mcode').value;
	var mob = document.getElementById('00N20000001SJmb1').value;

	var result = mobc + mob;


	var others = "Others";
	if (document.getElementById('00N20000001SJmA').value != "India") {
		var foreigner = "Foreigner";
		document.getElementById('00N20000001SJm4').value = foreigner;
		document.getElementById('00N20000001SJn4').value = city;
		document.getElementById('00N20000001SJm3').value = others;
		document.getElementById('00N20000001SJmb').value = result;
		document.getElementById('00N20000001SJmA').value;

	}
	else {

		if (city == "Mumbai") {
			var mum = "Mumbaiite";
			document.getElementById('00N20000001SJm4').value = mum;
			document.getElementById('00N20000001SJm3').value = city;
			document.getElementById('00N20000001SJmb').value = mob;
			document.getElementById('00N20000001SJmA').value; /*alert(mob); */
		}
		else {
			var otherInd = "Other Indian";
			document.getElementById('00N20000001SJm4').value = otherInd;

			if (city != "Pune" || city != "Hyderabad") {
				document.getElementById('00N20000001SJn4').value = city;
				document.getElementById('00N20000001SJm3').value = others;
				document.getElementById('00N20000001SJmb').value = mob;
				document.getElementById('00N20000001SJmA').value;

			}
			else { /* alert(city);*/
				document.getElementById('00N20000001SJm3').value = city;
				document.getElementById('00N20000001SJmA').value;

			}
		}
	}
	if($('#validateioncodediv').css('display') == 'block' && !$('#smsvalidationcode').val() && confrence.ajaxinProgress == 0 ) {
		riocontact.message += 'Please enter validation code received on your mobile';
		riocontact.message +="<br/>" ;
	}
	if (riocontact.message.length > 0 ) {
	
		$("#dialog").html(riocontact.message) ;
		$('#dialog').dialog('open');
		
		return false;
	}
	else {
	
		if(confrence.__verified == true || 	($('#countrycombo').val() != '0091' ) ) {
			insertData() ;
		}
		else {
			if(caller != 'matchValidationCode' ) 
			confrence.matchValidationCode('validation');
		}
	}
	
	return false ;

}
