var httpObj = false;
	try {
		  httpObj = new XMLHttpRequest();
		} catch (trymicrosoft) {
		  try {
				httpObj = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (othermicrosoft) {
			try {
			  httpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
			  httpObj = false;
			}
		}
	}

/*function DisplayError(message){
	document.getElementById("msg_div").style.display = "block";
	document.getElementById("msg_div").innerHTML = message;
}

function HideError(){
	document.getElementById("msg_div").innerHTML = "";
	document.getElementById("msg_div").style.display = "none";
}*/

function DisplayError(Message){
	/*document.getElementById("msg_div").style.display = "block";
	document.getElementById("msg_div").innerHTML = message;*/
	
	document.getElementById("msg_div").style.display = "block";
		alert("hi")
	document.getElementById("msg_div").style.border = "1px solid #d12f19";

	document.getElementById("msg_div").style.background = "#f7cbc2";
	document.getElementById("msg_div").innerHTML = '<p style="color:#d12f19;">' + Message + '</p>';
}

function DisplaySuccess(Message){
	document.getElementById("msg_div").style.display = "block";
	document.getElementById("msg_div").style.border = "1px solid #05bf00";
	document.getElementById("msg_div").style.background = "#c4fdbe";
	document.getElementById("msg_div").innerHTML = '<p style="color:#048701;">' + Message + '</p>';
}

function HideError(){
	document.getElementById("msg_div").style.border = "";
	document.getElementById("msg_div").style.background = "";
	document.getElementById("msg_div").innerHTML = "";
	document.getElementById("msg_div").style.display = "none";
}

function AddNewContact(){
	var url = urlpath+"AddAddressBook.php";
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById("address_book").innerHTML = "<p style='text-align:center;margin-top:80px; margin-bottom:80px;'><img src='./images/loadinfo.net.gif'><br><img src='./images/loading.gif'></p>";
		}
		if(httpObj.readyState==4){
			document.getElementById("address_book").innerHTML = httpObj.responseText;
		}
	}
	httpObj.send(null);
}

function addNewAddress()
{	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	mobile_no = document.getElementById('contactno').value;
	if(document.getElementById('contactname').value == ''){

		DisplayError("Please enter name.");
		return false;
	}
	if(IsAlphaNumeric(document.getElementById('contactname').value)){
		DisplayError("Please enter only alphanumeric characters in name.");
		return false;
	}
	if(mobile_no == ''){

		DisplayError("Please enter mobile number.");
		return false;
	}
	if(mobile_no){
		if( !IsOnlyNumbers(mobile_no) ) {

			DisplayError("Mobile number appears to be incorrect!.");
			return false;
		}

		if( mobile_no.length != 10 ) {

			DisplayError("Mobile number appears to be incorrect!.");
			return false;
		}

		if( mobile_no.charAt(0) != '9' ) {

			DisplayError("Mobile number appears to be incorrect!.");
			return false;
		}
	}

	/*if(document.getElementById('contactemail').value == ''){

		DisplayError("Please enter email.");
		return false;
	}
	if (!filter.test(document.getElementById('contactemail').value)) {
		DisplayError("Please enter correct email.");
		return false;
	}

	if(document.getElementById('contactcity').value == ''){
		DisplayError("Please enter city.");
		return false;
	}
	
	if(IsAlphaNumeric(document.getElementById('contactcity').value)){
		DisplayError("Only alphanumeric chracters are allowed in city.");
		return false;
	}*/

	if(trim(document.getElementById('contactemail').value) != ''){
		if (!filter.test(document.getElementById('contactemail').value)) {
			DisplayError("Please enter correct email.");
			return false;
		}
	}

	if(trim(document.getElementById('contactcity').value) != ''){
		if(IsAlphaNumeric(document.getElementById('contactcity').value)){
			DisplayError("Only alphanumeric chracters are allowed in city.");
			return false;
		}
	}

	var contactname = document.getElementById("contactname").value;
	var contactno = document.getElementById("contactno").value;
	var contactemail = document.getElementById("contactemail").value;
	var contactcity = document.getElementById("contactcity").value;

	var url = urlpath+'AddAddressBook.php?nickname='+contactname+'&contactno='+contactno+'&contactemail='+contactemail+'&contactcity='+contactcity;
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById("add_contact_body").innerHTML = "<p style='text-align:center;margin-top:40px;margin-bottom:40px; '><img src='/images/loadinfo.net.gif'><br><img src='./images/loading.gif'></p>";
		}
		if(httpObj.readyState==4){
			DisplaySuccess('Your contact has been added successfully.');
			document.getElementById("add_contact_body").innerHTML = '<div id="account_main" style="margin-top:0px; width:442px;"><div id="account_main_top"><div id="tr_gray5"></div></div><div id="account_main_middle"><div id="account_whitebox" style="width:426px;"><div id="account_whitebox_top"> <div id="tl_white"></div><div id="tr_white"></div></div><div id="account_whitebox_middle" style="padding-bottom:0px;"><div id="contact_box"><div id="contact_alpha"><p id="alpha">Invite your friends to Join YouMint </p></div><div id="contact_body"><div id="add_contact_form"><ul><li id="name"><label>Name:</label>&nbsp;<input name="contactname" id="contactname" type="text"/></li><li id="Mobile"><label>Mobile:&nbsp;</label><input name="contactno" id="contactno" maxlength="10" type="text"/></li><li id="email"><label>Email:&nbsp;</label><input name="contactemail" id="contactemail" type="text"/></li><li id="city"><label >City:&nbsp;</label><input name="contactcity" id="contactcity" type="text"/></li></ul> <p><input name="Add" onClick="javascript:addNewAddress();" value="Add Contact" class="button" type="submit" /> </p> </div><!-- end of add_contact_form div --></div><!-- end of contact_body div --></div></div><!-- End of account_whitebox_middle --><div class="account_whitebox_bottom"><div id="bl_white"></div><div id="br_white"></div></div></div>  <!-- End of account_whitebox --></div> <!-- End of Account_main_middle --><div id="account_main_bottom"><div id="bl_gray5"></div><div id="br_gray5"></div></div></div> <!-- End of Account_main-->';
			document.getElementById("add_contact_body").style.display = "none";
			document.getElementById("contact_box").style.display = "block";
			document.getElementById("contact_box").innerHTML = httpObj.responseText;
		}
	}
	httpObj.send(null);
}


function DelAddress(Id)
{
	if(confirm('Are you sure you want to delete this friend?')){
		var alphabet = document.getElementById("alpha1").value;
		var url = urlpath+'AddAddressBook.php?id='+Id+"&alphabet="+alphabet;
		httpObj.open("POST",url,true);
		httpObj.onreadystatechange = function(){
			if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
				document.getElementById("contact_box").innerHTML = "<p style='text-align:center;margin-top:40px;margin-bottom:40px; '><img src='./images/loadinfo.net.gif'><br><img src='./images/deleting.gif'></p>";
			}
			if(httpObj.readyState==4){
				DisplaySuccess('Your contact has been deleted successfully.');
				document.getElementById("contact_box").innerHTML = httpObj.responseText;
			}
		}
		httpObj.send(null);
	}
}

function MobileNumber(Mobile){
	document.getElementById("mobile").value = Mobile;
	var newno = 160;
	var newno1 = 80;
	var mobile = document.getElementById('mobile').value;
	var url = urlpath+"CheckRegMobile.php?mobile="+mobile;
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==4){
		var test = httpObj.responseText;
		if(test == 1){	
			 document.getElementById("remLen").value = newno;
			 document.getElementById("remLen1").value = newno;
			 checkMyCount();
		}else{
			document.getElementById("remLen").value = newno1;
		    document.getElementById("remLen1").value = newno1;
		}
	  }
	}
	httpObj.send(null);	
}

/*function MobileNumber(Mobile){
	document.getElementById("mobile").value = Mobile;
}*/

function SendSMS()
{
	alert("hi")	
	mobile_no = document.getElementById('mobile').value;
	friends = document.getElementById('friends').value;
	friends_mob = document.getElementById('from_no').value;
	message = document.getElementById('message').value;
   // if((mobile_no == friends_mob)){
		
	//	DisplayError("Don't worry,this service works! You don't need to send a message to yourself. :-)");
	//	return false;
	//}
	
//	if((mobile_no == '')  && ( friends == 'select')){
	//	DisplayError("Please enter the recipients mobile number.");
	//	return false;
	//}

	//if((mobile_no != '')  && (friends != 'select')){
	//	DisplayError("Please select only one.");
	//	return false;
	//}
	//if(mobile_no){
	//	if( !IsOnlyNumbers(mobile_no) ) {
		//	DisplayError("Mobile number appears to be incorrect!");
	//		return false;
	//	}
	//	if( mobile_no.length != 10 ) {
		//	DisplayError("Mobile number appears to be incorrect!");
	//		return false;
	//	}

	//	if( mobile_no.charAt(0) != '9' ) {
		//	DisplayError("Mobile number appears to be incorrect!");
	//		return false;
	//	}
//	}
		


//	if(document.getElementById('message').value == ''){
		//DisplayError("Please Enter Your Message");
//		return false;
//	}
	//var url = urlpath+'SendingSms.php?to_no='+mobile_no+'&message='+message+'&from_no='+friends_mob;
	var url = '/ajax/sending-sms.asp?toMobileNumber='+mobile_no+'&message='+message+'&fromMobileNumber='+friends_mob;
	httpObj.open("GET",url,true);
	//document.getElementById('sponsorAdDiv').style.display = 'block';
	//document.getElementById('sponsorAdDiv').className = 'show';

	httpObj.onreadystatechange = function(){
		
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById("FreeSmsDisplay").innerHTML = "<p style='text-align:center;margin-top:10px; margin-bottom:80px;'><img src='/images/loadinfo.net.gif'><br><img src='/images/sending.gif'></p>";
		}
		/*if(httpObj.readyState==4){
			if(httpObj.responseText.indexOf('--yes--') != -1){
				ShowSmsSuccess();	
			}else {
				document.getElementById('FreeSmsDisplay').innerHTML = httpObj.responseText;
			}
		}*/
		if(httpObj.readyState==4){
		if(httpObj.responseText.indexOf('--not authenticated--') != -1){
				ShowAuthentication(mobile_no,message,friends_mob);
			}else if(httpObj.responseText.indexOf('--yes--') != -1){
				ShowSmsSuccess();	
			}else {
				document.getElementById('FreeSmsDisplay').innerHTML = httpObj.responseText;
			}
		}

	}
	httpObj.send(null);
}

function ShowAuthentication(to_no,message,from_no){
	DisplayError("You cannot send more than 80 characters to a non YouMint member.");
	document.getElementById("FreeSmsDisplay").innerHTML = '<div style="position:relative; left:7px; margin-top:10px;"><p style="text-align:left; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000;"> Mobile no.</p><p style="margin-top:5px;"><input name="mobile" id="mobile" type="text" value='+ to_no +' style="width:130px;" maxlength="10" onBlur="javascript:CheckMyMobile();" /></p><p style="text-align:left; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000; margin-top:5px;"> Message:</p><p style="margin-top:5px;"><textarea name="message" id="message" onKeyUp="javascript:checkMyCount();" rows="5" cols="18">'+ message +'</textarea></p></div><div style="width:186px; position:relative; left:7px; margin-top:10px;"><p><input readonly type="text" id="remLen" name="remLen" size="3" maxlength="3" value="80" class="inputstyle" style="width:22px;height:14px;"><input readonly type="hidden" id="remLen1" name="remLen1" value="80" style="width:50px; padding:2px;border:1px solid #CCCCCC; font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000; font-size:12px;"/> (Char Left)</span></p></div>';
}

function ShowSmsSuccess(){
	var url = urlpath+"FreeSmsSuccess.php";
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==4){	
			document.getElementById("FreeSmsDisplay").innerHTML = httpObj.responseText;
			HideError();
			document.getElementById("SendSmsButton").innerHTML = '<p class="send_sms">&nbsp;</p>';
			document.getElementById("FreeSmsAgain").style.display = 'block';
		}
	}
	httpObj.send(null);
}

function ShowContacts(value){
	HideError();
	var url = urlpath + "AddAddressBook.php?alphabet=" + value;
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById("contact_body").innerHTML = "<p style='text-align:center;margin-top:40px;margin-bottom:40px; '><img src='./images/loadinfo.net.gif'><br><img src='./images/loading.gif'></p>";
		}
		if(httpObj.readyState==4){
			document.getElementById("contact_box").innerHTML = httpObj.responseText;
		}
	}
	httpObj.send(null);
}	

function DisplayEditRecords(id,count){
	var hidediv = "contact_"+id;
	var opendiv = "contact_"+id+"_"+count;
	document.getElementById(opendiv).style.display = "block";
}

function CloseEditRecords(id,count){
	var opendiv = "contact_"+id+"_"+count;
	document.getElementById(opendiv).style.display = "none";
}

function SaveEditAddress(id,count){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	var friend_name = "friend_name_"+id+"_"+count;
	var friend_mobile = "friend_mobile_"+id+"_"+count;
	var friend_email = "friend_email_"+id+"_"+count;
	var friend_city = "friend_city_"+id+"_"+count;

	var name = trim(document.getElementById(friend_name).value);
	var mobile = trim(document.getElementById(friend_mobile).value);
	var email = trim(document.getElementById(friend_email).value);
	var city = trim(document.getElementById(friend_city).value);
	
	if(name == ''){
		DisplayError("Please enter name.");
		return false;
	}
	if(IsAlphaNumeric(name)){
		DisplayError("Please enter only alphanumeric characters in name.");
		return false;
	}
	if(mobile == ''){
		DisplayError("Please enter mobile number.");
		return false;
	}
	if(mobile){
		if(!IsOnlyNumbers(mobile)) {
			DisplayError("Mobile number appears to be incorrect!.");
			return false;
		}
		if( mobile.length != 10 ) {
			DisplayError("Mobile number appears to be incorrect!.");
			return false;
		}
		if( mobile.charAt(0) != '9' ) {
			DisplayError("Mobile number appears to be incorrect!.");
			return false;
		}
	}

	/*if(email == ''){
		DisplayError("Please enter email.");
		return false;
	}
	if (!filter.test(email)) {
		DisplayError("Please enter correct email.");
		return false;
	}

	if(city == ''){
		DisplayError("Please enter city.");
		return false;
	}
	
	if(IsAlphaNumeric(city)){
		DisplayError("Only alphanumeric chracters are allowed in city.");
		return false;
	}*/

	if(email!=''){
		if (!filter.test(email)) {
			DisplayError("Please enter correct email.");
			return false;
		}
	}

	if(city!=''){
		if(IsAlphaNumeric(city)){
			DisplayError("Only alphanumeric chracters are allowed in city.");
			return false;
		}
	}

	CloseEditRecords(id,count);
	var opendiv = "edit_"+id;
	var opendiv1 = "contact_"+id;
	var alphabet = document.getElementById("alpha1").value;
	
	
	var url = urlpath+"AddAddressBook.php?alphabet="+alphabet+"&nickname="+name+"&mobile="+mobile+"&email="+email+"&city="+city+"&id="+id+"&count="+count;
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1||httpObj.readyState==2||httpObj.readyState==3){
			document.getElementById('contact_box').innerHTML = "<p style='text-align:center;margin-top:30px;'><img src='./images/updating.gif'></p>";
		}
		if(httpObj.readyState==4){
			document.getElementById('contact_box').innerHTML = httpObj.responseText;
			DisplaySuccess("Contact has been updated.");
		}	
	}
	httpObj.send(null);
}

function MobileVerifyPage(){
	window.location = urlpath+"MobileVerification.html";
}

function ReSendVerificationCode(){
	//var code = document.getElementById('hidden_code').value;
	var mobile = document.getElementById('mobile').value;
	//var code = document.getElementById('new_hidden_code').value;
	var codesent = document.getElementById('numberofverificationcodesent').value;
	//var url = urlpath+"MobileVerifyFreeSms.php?Code="+code+"&CodeSent="+codesent;
	var url = urlpath+"MobileVerifyFreeSms.php?CodeSent="+codesent;
	httpObj.open("POST",url,true);
	document.getElementById('msg_div').style.display = "block";
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1 || httpObj.readyState==2 || httpObj.readyState==3){
			document.getElementById('msg_div').innerHTML = "<p style='text-align:center;margin-top:5px;margin-bottom:5px; '><img src='./images/loadinfo.net.gif'><br><img src='./images/sending.gif'></p>";
			document.getElementById('VerificationCodeDiv').style.display = "none";
			document.getElementById('savebutton').style.display = "none";
		}else{
			var response = 	httpObj.responseText;
			ResponseArray = response.split('--');
			
			if(ResponseArray[0]==4){
				//document.getElementById('msg_div').innerHTML = 'Verification code has been sent to your mobile.';
				document.getElementById('hidden_code').value = ResponseArray[1];
				document.getElementById('msg_div').innerHTML = 'A verification code has been sent to your mobile (+91'+mobile+') as an SMS.';
				document.getElementById('MailCustomerSupport').style.display = "block";
				document.getElementById('savebutton').style.display = "block";
			}else{
				document.getElementById('hidden_code').value = ResponseArray[1];
				document.getElementById('savebutton').style.display = "block";
				//document.getElementById('msg_div').innerHTML = 'Verification code has been sent to your mobile.';
				document.getElementById('msg_div').innerHTML = 'A verification code has been sent to your mobile (+91'+mobile+') as an SMS.';
				document.getElementById('VerificationCodeDiv').style.display = "block";
				document.getElementById('numberofverificationcodesent').value = response;
			}
		}
	}
	httpObj.send(null);
}

function VerifyMobile(){
	var code = document.getElementById('hidden_code').value;
	var mobilecode = document.getElementById('mobilecode').value;
	
		if(mobilecode==''){
			DisplayError("Please enter verification code.");
			return false;
		}
		if(mobilecode!=code){
			DisplayError("Please enter correct verification code.");
			return false;
		}
	var url = urlpath+"MobileVerifyFreeSms.php";
	httpObj.open("POST",url,true);
	document.getElementById('msg_div').style.display = "block";
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1 || httpObj.readyState==2 || httpObj.readyState==3){
			document.getElementById('msg_div').innerHTML = "<p style='text-align:center;margin-top:5px;margin-bottom:5px; '><img src='./images/loadinfo.net.gif'><br><img src='./images/updating.gif'></p>";
			document.getElementById('VerificationCodeDiv').style.display = "none";
			document.getElementById('savebutton').style.display = "none";
		}
		if(httpObj.readyState==4){
			document.getElementById('msg_div').innerHTML = '';
			document.getElementById('msg_div').style.display = "none";	
			window.location = urlpath+"FreeSms.php";
			return false;
		}
	}
	httpObj.send(null);
}	

function VerifyMobile1(){
	var code = document.getElementById('hidden_code').value;
	var mobilecode = document.getElementById('mobilecode').value;
	
		if(mobilecode==''){
			DisplayError("Please enter verification code.");
			return false;
		}
		if(mobilecode!=code){
			DisplayError("Please enter correct verification code.");
			return false;
		}
	var url = urlpath+"MobileVerifyFreeSms.php";
	httpObj.open("POST",url,true);
	document.getElementById('msg_div').style.display = "block";
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1 || httpObj.readyState==2 || httpObj.readyState==3){
			document.getElementById('msg_div').innerHTML = "<p style='text-align:center;margin-top:5px;margin-bottom:5px; '><img src='./images/loadinfo.net.gif'><br><img src='./images/updating.gif'></p>";
		}
		if(httpObj.readyState==4){
			document.getElementById('msg_div').innerHTML = '';
			document.getElementById('msg_div').style.display = "none";	
			window.location = urlpath+"FreeSms.php";
			return false;
		}
	}
	httpObj.send(null);
}	

function ChangeMobile(){
	var mobile = document.getElementById('new_mobile').value;
	var url = urlpath+"ChangeMobileNumber.php?mobile="+mobile;
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==1 || httpObj.readyState==2 || httpObj.readyState==3){
			document.getElementById('msg_div').innerHTML = "<p style='text-align:center;margin-top:5px;margin-bottom:5px; '><img src='./images/loadinfo.net.gif'><br><img src='./images/updating.gif'></p>";
		}
		if(httpObj.readyState==4){
			var strContent = httpObj.responseText;
			if(strContent=='--1--'){
				document.getElementById('msg_div').innerHTML = '';
				document.getElementById('msg_div').style.display = "none";	
				DisplayError("Mobile number already registered on YouMint.");
			}else{
				document.getElementById('ShowMobileNumber').style.display = "none";
				document.getElementById('details').style.display = "block";
				document.getElementById('details').style.display = strContent;
			}
		}
	}
	httpObj.send(null);
}

/*function checkMyCount(){
	var fT, count, msga, ulta;

	 msga = 80;
	 count = document.getElementById("message").value.length;
	 ulta = msga - count;
	 document.getElementById("remLen").value = ulta;
		 if(count>msga){
			document.getElementById("message").value = document.getElementById("message").value.substring(0, msga);
			document.getElementById("remLen").value = msga - document.getElementById("message").value.length;
		}
}*/

function checkMyCount(){
	var fT, count, msga, ulta;
	 if(document.getElementById("remLen1").value == 100){
		  msga = 100;
	 }else if(document.getElementById("remLen1").value == 160){
		  msga = 148;
	 }
	 count = document.getElementById("message").value.length;
	 ulta = msga - count;
	 document.getElementById("remLen").value = ulta;
		 if(count>msga){
			document.getElementById("message").value = document.getElementById("message").value.substring(0, msga);
			document.getElementById("remLen").value = msga - document.getElementById("message").value.length;
		}
}


function CheckMyMobile(){
	
	var newno = 160;
	var newno1 = 80;
	var mobile = document.getElementById('mobile').value;
	var url = urlpath+"CheckRegMobile.php?mobile="+mobile;
	httpObj.open("POST",url,true);
	httpObj.onreadystatechange = function(){
		if(httpObj.readyState==4){
		var test = httpObj.responseText;
		if(test == 1){	
			 document.getElementById("remLen").value = newno;
			 document.getElementById("remLen1").value = newno;
		}else{
			document.getElementById("remLen").value = newno1;
		    document.getElementById("remLen1").value = newno1;
		}
	  }
	}
	httpObj.send(null);	
}

