function showSendtoPhone(suffix,svc_id,x,y,type){
	resetForm(suffix);
	
	document.getElementById('stph_popup'+suffix).style.top = (y+20)+'px';
	document.getElementById('stph_popup'+suffix).style.left = (x-20)+'px';
	
	if(type == 'mobile_web'){
		document.getElementById('stph_success'+suffix).innerHTML = '<p>Thank you! A text message was sent to your mobile phone with a link to the site.</p>';
	} else if(type == 'blackberry'){
		document.getElementById('stph_success'+suffix).innerHTML = '<p>Thank you! A text message was sent to your mobile phone with a link to install the application.</p>';
	}
	
	$('#stph_popup'+suffix).show("normal");

	document.getElementById('stph_svc_id'+suffix).value = svc_id;
	
	document.getElementById('stph_form'+suffix).style.display = "";
	document.getElementById('stph_success'+suffix).style.display = "none";
	document.getElementById('stph_optout'+suffix).style.display = "none";
	
	setTimeout("field1SetFocus('#stph_mn1"+suffix+"')",1000);
}

function hideSendtoPhone(suffix){
	$('#stph_popup'+suffix).hide("normal");
}

function  showValidationErrors(suffix, errors){
	document.getElementById('stph_phone_empty'+suffix).style.display = "none";
	document.getElementById('stph_phone_invalid'+suffix).style.display = "none";
	//document.getElementById('wc_empty').style.display = "none";
	
	for(var i=0; i<errors.length; i++){
		if(errors[i] == 'stph_phone_empty'){
			document.getElementById('stph_phone_empty'+suffix).style.display = "";
		} else if(errors[i] == 'stph_phone_invalid'){
			document.getElementById('stph_phone_invalid'+suffix).style.display = "";
		}
		
		/*if(errors[i] == 'stph_wc_empty'){
			document.getElementById('wc_empty').style.display = "";
		}*/
	}
}

function sendData(suffix){
	var stph_frm = document.getElementById('stph_form'+suffix);
	var svc_id = stph_frm.svc_id.value;
	var mn1 = stph_frm.mn1.value;
	var mn2 = stph_frm.mn2.value;
	var mn3 = stph_frm.mn3.value;
	//var wc_id = stph_frm.wc_id.value;
  	
  	$.ajax({
   		type: "POST",
   		url: "sendtomobile",
   		data: "svc_id="+svc_id+"&mn1="+mn1+"&mn2="+mn2+"&mn3="+mn3,
   		suffix: suffix,
   		success: function(data){
     		sendDataHandler(this.suffix, data);
   		}
    });
  	  	
}

function sendDataHandler(suffix, data){
	if((typeof data != 'undefined') && data != null){
		var response = eval("(" + data + ")");
		if((typeof response.result != 'undefined') && response.result == 'msg_sent'){
			document.getElementById('stph_submit'+suffix).style.display = "none";
			document.getElementById('stph_optout'+suffix).style.display = "none";
			document.getElementById('stph_success'+suffix).style.display = "";
		} else if((typeof response.errors != 'undefined') && response.errors == 'not_opted_in'){
			document.getElementById('stph_submit'+suffix).style.display = "none";
			document.getElementById('stph_success'+suffix).style.display = "none";
			document.getElementById('stph_optout'+suffix).style.display = "";		
		} else if((typeof response.validationErrors != 'undefined') && response.validationErrors.length > 0) {
			showValidationErrors(suffix, response.validationErrors);
		}
	}
}

function resetForm(suffix){
	var stph_frm = document.getElementById('stph_form'+suffix).reset();
	
	document.getElementById('stph_phone_empty'+suffix).style.display = "none";
	document.getElementById('stph_phone_invalid'+suffix).style.display = "none";
	document.getElementById('stph_submit'+suffix).style.display = "";
	//document.getElementById('wc_empty').style.display = "none";
	document.getElementById('stph_success'+suffix).innerHTML = '<p>Thank you! A text message was sent to your mobile phone with a link to install the application.</p>';
}

function field1SetFocus(elem_id){
	$(elem_id).focus();
}