function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
//************************************************************************
//  This script is used  to check the validation for subscribe form
//   And used ajax function to save information in database 
//    Sep 2008 By Harish O.  
//**************************************************************************
/*function valid_smsform(){
var form = document.sms_information;

	if( trim(form.name.value) == ''){
			alert('Please enter your name');
			form.name.value = "";
			form.name.focus();
	} else if (trim(form.emailid.value) ==""){
			alert("Please enter your Email address.")
			form.emailid.value = "";
			form.emailid.focus(); 
	} else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(trim(form.emailid.value))) && (trim(form.emailid.value) !="")){
			alert("Invalid E-mail Address! Please re-enter.")
			form.emailid.focus(); 
	} else if ( trim(form.mobileno.value) == "" ){
			alert('Please enter your Mobile no.');
			form.mobileno.value = "";
			form.mobileno.focus(); 

	} else if ( document.sms_information['flavor[]'].value == '' ) {
			alert('Please select atleast one flavor.');
	} else {
			usedAjax();
	}
}
*/
var selectedOptions = [];
function countSelected(select,maxNumber){
	for(var i=0; i<select.options.length; i++){
		if(select.options[i].selected && !new RegExp(i,'g').test(selectedOptions.toString())){
			selectedOptions.push(i);
		}
		if(!select.options[i].selected && new RegExp(i,'g').test(selectedOptions.toString())){
			selectedOptions = selectedOptions.sort(function(a,b){return a-b});
			for(var j=0; j<selectedOptions.length; j++) {
				if(selectedOptions[j] == i) {
					selectedOptions.splice(j,1);
				}
			}
		}
		if(selectedOptions.length > maxNumber){
			alert('You can only select '+maxNumber+' flavors at a time!');
			select.options[i].selected = false;
			selectedOptions.pop();
			document.body.focus();
		}  
	}    
}
function toggleSelect() {
	if(document.getElementById('flavorSelect').disabled == true) {
		document.getElementById('flavorSelect').disabled = false;
	} else {
		document.getElementById('flavorSelect').disabled = true;
	}
}
function valid_smsform(){
	var form = document.sms_information;
	if( trim(form.name.value) == ''){
		alert('Please enter your name');
		form.name.focus();
	} else if (form.alert_type.value == 3 && ( trim(form.emailid.value) =="" )){
			form.emailid.value = '';
			alert('Please enter your Email Address');
			form.emailid.focus(); 
	} else if (form.alert_type.value == 3 && ( trim(form.mobileno.value) =="" )){
                        form.mobileno.value = '';
                        alert('Please enter your Mobile number');
                        form.emailid.focus(); 

	} else if (form.alert_type.value == 2 && (  trim(form.emailid.value) =="" )){
		form.emailid.value = '';
		alert('Please enter your Email address');
		form.emailid.focus(); 
		
	} else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.emailid.value)) && (form.alert_type.value == 3 || form.alert_type.value == 2 )){
			alert("Please enter a valid Email Address")
			form.emailid.focus(); 
			
	} else if (form.alert_type.value == 1 && ( trim(form.mobileno.value)=="" )){
			alert('Please enter your Mobile number');
			form.mobileno.focus(); 
			
	} else if ( document.sms_information['flavor[]'].value == '' && form.all_flavors.checked == false) {
		alert('Please select atleast one flavor');
	} else {
		usedAjax();
	}
}


var xmlhttp;
 function  usedAjax(){
    var http = getXHTTP(); // This executes when the page first loads.
    var form = document.sms_information;
    var selectedArray = new Array();
	var selObj = document.getElementById('flavorSelect');
	var url="sms_popup/save_subscrition_value.php?";
	var count = 0;
	var discount_info = 0;
	  for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
		  selectedArray[count] = selObj.options[i].value;
		  count++;
		}
	  }
	 // alert( selectedArray );
  	if ( form.info_discount.checked == 0 ) {
  		 discount_info = 2;
    } else {
    	 discount_info = 1;
	}		
	
	var id = 'name='+form.name.value+'&mobileno='+form.mobileno.value+'&flavor='+selectedArray+'&location_id='+form.location.value+'&emailid='+form.emailid.value+'&info_discount='+discount_info+'&alert_type='+form.alert_type.value;
	
    doHttpRequest(url+id);
	 
	 function doHttpRequest(url) {  
		  http.open("GET", url, true);
		  http.onreadystatechange = getHttpRes;
		  http.send(null);
	}
	
	function getHttpRes() {
	  if (http.readyState == 4) { 
		    res = http.responseText;  // These following lines get the response and update the page
			alert(res);
			dhtmlwindow.close( ajaxwin );
			
		 }
	}
			
	 function getXHTTP() {
			  var xhttp;
			   try {   // The following "try" blocks get the XMLHTTP object for various browsers…
				  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				  try {
					xhttp = new ActiveXObject("Microsoft.XMLHTTP");
				  } catch (e2) {
					 // This block handles Mozilla/Firefox browsers...
					try {
					  xhttp = new XMLHttpRequest();
					} catch (e3) {
					  xhttp = false;
					}
				  }
				}
			  return xhttp; // Return the XMLHTTP object
			}
	 		 
}
//************************************************************************
//  This script is used  to check the validation of unsubscribe form
//   And used ajax function to check in database that info is in database 
//   Or not which is provieded by use:  
//   15 Sep 2008 By Harish O.  
//**************************************************************************


/*

function valid_smsform_unsubscribe(){

	var form = document.unsubscribe_form;

	/*if( form.emailid.value == '' ||  form.mobileno.value == ''){
		alert('Please enter either Email id AND Mobile no');
		form.emailid.focus();
	} else if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.emailid.value) ) && form.emailid.value != '' ){
		alert("Invalid E-mail Address! Please re-enter.")
		form.emailid.focus(); 
	} else {
		unsubscribeAjax();
	}
	
	
	if( trim(form.emailid.value) == ''){
		alert('Please enter Email id');
		form.emailid.value = "";
		form.emailid.focus();
	} else if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(trim(form.emailid.value)) ) && trim(form.emailid.value) != '' ){
		alert("Invalid E-mail Address! Please re-enter.")
		form.emailid.focus(); 
	} else if ( form.mobileno.value == '') {
		alert('Please enter Mobile no.');
		form.mobileno.value = "";
		form.mobileno.focus();
		
	} else {
		unsubscribeAjax();
	}
}

*/



function valid_smsform_unsubscribe(){

	var form = document.unsubscribe_form;

	if( trim(form.emailid.value) == '' &&  trim(form.mobileno.value) == ''){
		alert('Please enter your Email address or Mobile number');
		form.emailid.focus();
	} else if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.emailid.value) ) && trim(form.emailid.value) != '' ){
		alert("Please enter a valid Email address")
		form.emailid.focus(); 
	} else {
		unsubscribeAjax();
	}
	
}


var xmlhttp;
 function  unsubscribeAjax(){

    var http = getXHTTP(); // This executes when the page first loads.
    var form = document.unsubscribe_form;
   	var url="sms_popup/unsubscrition_value.php?";
	
	var id = 'mobileno='+form.mobileno.value+'&emailid='+form.emailid.value+'&location_id='+form.location.value;
					
    doHttpRequest(url+id);
	 
	 function doHttpRequest(url) {  
		  http.open("GET", url, true);
		  http.onreadystatechange = getHttpRes;
		  http.send(null);
	}
	
	function getHttpRes() {
	  if (http.readyState == 4) { 
		    res = http.responseText;  // These following lines get the response and update the page
			
			if(res ==''){
			 	dhtmlwindow.close( ajaxwin );
				openmypage_un_afterCheck();
				
			 } else {
				alert(res);
				dhtmlwindow.close( ajaxwin );
				openmypage_un();
			}				
			
		 }
	}
			
	 function getXHTTP() {
			  var xhttp;
			   try {   // The following "try" blocks get the XMLHTTP object for various browsers
				  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				  try {
					xhttp = new ActiveXObject("Microsoft.XMLHTTP");
				  } catch (e2) {
					 // This block handles Mozilla/Firefox browsers...
					try {
					  xhttp = new XMLHttpRequest();
					} catch (e3) {
					  xhttp = false;
					}
				  }
				}
			  return xhttp; // Return the XMLHTTP object
			}
	 		 
}

//************************************************************************
//  This script is used  to check the validation for subscribe form
//   And used ajax function to update information in database 
//    Sep 2008 By Harish O.  
//**************************************************************************



function uncheck_alerttype(){
	var form = document.sms_information_update;
	
	if (  form.unsubscribe_alert.checked == true ) {
	      form.info_discount.checked = false;
	} 
}




function valid_update_smsform(){
	var form = document.sms_information_update;

	
	if( trim(form.name.value) == ''){
		alert('Please enter your name');
		form.name.value = "";
		form.name.focus();
	} else if ( document.sms_information_update['flavor[]'].value == ''  && form.all_flavors.checked == false) {
		alert('Please select atleast one flavor.');
	} else {
		updatedAjax();
	}

}


var xmlhttp;
 function  updatedAjax(){
    
    var http = getXHTTP(); // This executes when the page first loads.
    var form = document.sms_information_update;
    var selectedArray = new Array();
	var selObj = document.getElementById('flavorSelect');
	var url="sms_popup/save_subscrition_value.php?";
	var count = 0;
	var discount_info = 0;
	  for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
		  selectedArray[count] = selObj.options[i].value;
		  count++;
		}
	  }
  	if ( form.info_discount.checked == 0 ) {
  		 discount_info = 2;
    } else {
    	 discount_info = 1;
	}		
	
  
   // here 2 is used for inactive[unsubscrie]   and 1 is for active [subscribe] 
   
    status1 = (form.unsubscribe_alert.checked == 0 ) ? '1': '2' ; 
  
    status_email = (form.unsubscribe_email.checked == 0 ) ? '1': '2' ; 
     
    status_sms = (form.unsubscribe_sms.checked == 0 ) ? '1': '2' ; 
   	status_all = (form.all_flavors.checked == 0 ) ? '1': '2' ; 
   	   
	var id = 'name='+form.name.value+'&flavor='+selectedArray+'&location_id='+form.location.value+'&info_discount='+discount_info+'&alert_type='+form.alert_type.value+'&status='+status1+'&mobileno='+form.mobileno.value+'&emailid='+form.emailid.value+'&status_email='+status_email+'&status_sms='+status_sms+'&status_all='+status_all;
	
    doHttpRequest(url+id);
	 
	 function doHttpRequest(url) {  
		  http.open("GET", url, true);
		  http.onreadystatechange = getHttpRes;
		  http.send(null);
	}
	
	function getHttpRes() {
	  if (http.readyState == 4) { 
		    res = http.responseText;  // These following lines get the response and update the page
			alert(res);
			dhtmlwindow.close( ajaxwin );
			
		 }
	}
			
	 function getXHTTP() {
			  var xhttp;
			   try {   // The following "try" blocks get the XMLHTTP object for various browsers…
				  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				  try {
					xhttp = new ActiveXObject("Microsoft.XMLHTTP");
				  } catch (e2) {
					 // This block handles Mozilla/Firefox browsers...
					try {
					  xhttp = new XMLHttpRequest();
					} catch (e3) {
					  xhttp = false;
					}
				  }
				}
			  return xhttp; // Return the XMLHTTP object
			}
	 		 
}
