//---------------------------- Functions to check availability of user name USING AJAX ----------------------


var xmlHttp
function SendCoupon()
{ 

var ran_number= Math.random()*5;					// to counter the problem of non-duplicate http calls by ajax
										// that every call will be different




var intlcode = document.sendcouponform.intlcode.value;
intlcode=trimAll(intlcode); 	
intlcode = removeSpaces2(intlcode);					// defined below

var intlcodeL = document.sendcouponform.intlcode.value.length;


if(intlcodeL<2)
{
	alert("Please Enter Correct Country Code");
	window.document.sendcouponform.intlcode.focus();
	return false;
}


var mobile = document.sendcouponform.mobile.value;
mobile=trimAll(mobile); 	
mobile = removeSpaces2(mobile);					// defined below

var mobileL = document.sendcouponform.mobile.value.length;


if(mobileL<7)
{
	alert("Please Enter Correct Mobile Number");
	window.document.sendcouponform.mobile.focus();
	return false;
}


var customerid = document.sendcouponform.hiddenid.value;
customerid=trimAll(customerid); 	
var customeridL = document.sendcouponform.hiddenid.value.length;


if(customeridL<1)
{
	alert("Problem Sending SMS");
	return false;
}


var couponid = document.sendcouponform.hiddencouponid.value;
couponid=trimAll(couponid); 	
var couponidL = document.sendcouponform.hiddencouponid.value.length;


if(couponidL<1)
{
	alert("Problem Sending SMS");
	return false;
}



var ip = document.sendcouponform.hiddenip.value;
ip=trimAll(ip); 	




var ipL = document.sendcouponform.hiddenip.value.length;


if(ipL<5)
{
	alert("InValid Information");
	return;
}






	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }

	var url="http://www.coupontxt.com/ctrial/send.php"

//	var url="http://192.168.1.113/coupon/ctrial/send.php"

	url=url+"?cid="+customerid
	url=url+"&coid="+couponid
	url=url+"&concode="+intlcode
	url=url+"&mobile="+mobile
	url=url+"&ip="+ip
	url=url+"&sentmethod=web"
	
	try
	{	
		if(document.sendcouponform.optinbox.checked==true)			// user want to optin to receive more offers
		{
			url=url+"&optin=true";
		}
		else
		{
			url=url+"&optin=false";
		}
	}
	catch(e)
	{
		url=url+"&optin=false";

	}
	
	url=url+"&unused="+ran_number



	
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)




}
//------------------------------------------------------------------------------------------------------------------------------------------

function stateChanged() 
{ 
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	 //document.getElementById("output1").innerHTML=xmlHttp.responseText                // output1 is id of span element used to display output echo coming from php file
	
	if(xmlHttp.responseText == "Successfully Sent") 
	{
		alert("Success : Coupon Sent Successfully");
		
		//Close window without warning
		window.opener='X'; 
		window.open('','_parent',''); 
		window.close();
	}
	else
	{
		alert("Problem : " + xmlHttp.responseText);
	}
 } 
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


//------------------ String Trim Functions -----------------------------

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')              // left trim
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ')       // right trim
	{
		sString = sString.substring(0,sString.length-1);
	}

return sString;
}
//---------------------------- Functions to check availability of user name USING AJAX  ENDS  HERE----------------------



//---------------------------------------------------------------

function closewindow()
{

	window.close();

}





//--------------------------------------------------------------------------------------------

var gcounter = 1;
function ShowTermsAndConditions()
{
	gcounter++;
	
	if(gcounter%2==0)
	{
		term1.style.display="";		// show
		intervalid = setInterval('WinScroll()',100);			// defined below


	}
	else
	{
		term1.style.display="none";	// hide

	}

}


//---------------------------------------------------------------------------------------------


var times=0;
var intervalid=0;
function WinScroll()
{
	times++;					
	this.scrollTo(0,1000);	
	if(times==2)
	{
		clearInterval(intervalid);				// Run 2 times and stop the timer
	}
}
		

		
//------------------------------------------------------------------------------------------------------------


function removeSpaces2(string) 					// removes spaces from mobile number
{
 return string.split(' ').join('');
}
