/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$jq("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$jq("#backgroundPopup").fadeIn("slow");
		$jq("#popupSmsMe").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$jq("#backgroundPopup").fadeOut("slow");
		$jq("#popupSmsMe").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $jq("#popupSmsMe").height();
	var popupWidth = $jq("#popupSmsMe").width();
	//centering
	$jq("#popupSmsMe").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$jq("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//function to filter out the desired keys (in this case everything except digits
function checkForInt(evt){
 var charCode = ( evt.which != null ) ? evt.which : event.keyCode
 return (charCode < 32 || charCode == 46 || (charCode >= 48 && charCode <= 57))
}

//sending the data to the desired mob no.
function smsDetails(frm){
	var mobNo = frm['destNo'];
	var txtName = frm['ldName'];
	var txtPhone = frm['ldPhone'];
	var txtAddress = frm['ldAddress'];
	
	if((mobNo == null || mobNo.value.length < 10) || txtName == null || txtPhone == null || txtAddress == null){
		showAlert("E","ERROR: Invalid Phone Number or missing Showroom Details!!");
	}else{
	
		mobNo = filterStr(mobNo.value);
		txtName = filterStr(txtName.value);
		txtPhone = filterStr(txtPhone.value);
		txtAddress = filterStr(txtAddress.value);
		
		var xml = getXhttp( );
	    
		if (!xml){
				showAlert("E","ERROR: Unable to send details. Status: XML001");
		}else{
			showAlert("P","PLEASE WAIT: Processing Request...");
			
			//prepare the submit string
			xml.open('POST', '/smsme.php',true);
			//make sure to add following header to dynamically generated page!!
		    	xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xml.send('to='+mobNo+'&txtName='+txtName+'&txtPhone='+txtPhone+'&txtAddress='+txtAddress);
	
			xml.onreadystatechange = function(){
				if(xml.readyState == 4){
					if(xml.status == 200){
						//hide PLEASE WAIT
						hideAlert();
						
						//check if error was NOT returned
						if(xml.responseText.indexOf("ERROR") == -1){
							showAlert("S",xml.responseText);
						}else{ //if error returned, display accordingly
							showAlert("E",xml.responseText);
						}
					}else{
						showAlert("E","ERROR: Unable to send details. Status: "+xml.status)
					}
				}
			}//END onreadystatechange
		}
	}

}
function showAlert(alertType, alertMsg){
	//disable SEND button
	$jq("#sendSmsBtn").attr("disabled", "disabled");
	$jq("#sendSmsBtn").attr("style", "cursor:wait;");
	$jq("#smsResult").html(alertMsg);
	if(alertType == "S"){
		$jq("#smsResult").toggleClass("success");
	}else if(alertType == "E"){
		$jq("#smsResult").toggleClass("error");
	}else{
		$jq("#smsResult").toggleClass("pleaseWait");
	}
	$jq("#smsResult").slideDown(300);
	
	if(alertType == "S" || alertType == "E"){
		setTimeout("hideAlert()",5000);
	}
}

function hideAlert(){
	$jq("#smsResult").slideUp(150);
	$jq("#smsResult").html("");
	$jq("#smsResult").removeClass("success error pleaseWait");
	//enable SEND button
	$jq("#sendSmsBtn").removeAttr("disabled");
	$jq("#sendSmsBtn").removeAttr("style");
}

//CONTROLLING EVENTS IN jQuery
$jq(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$jq("#smsMeBtn").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$jq("#popupSmsMeClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$jq("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$jq(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});