// <!--
/**
 * Send to Phone Javascript
 */
var xmlHttpRequest = null; // the XMLHttpRequest Object
//var xmlAPIURL = ""; // the Server API URL
var sLabel="<b>Configuration Error: Please include label parameter in request</b>";
var sURL="<b>Configuration Error: Please include url parameter in request</b>";
var assetId = "0"; // article Id to be sent
var isProcessing = false; // indicate whether the program is calling the Request
var IE = document.all;
var MZ = (document.getElementById && !document.all);
if ((siteLabel!=null) && (siteLabel!=""))
          	sLabel=siteLabel;

if ((siteUrl!=null) && (siteUrl!=""))
               	sURL=siteUrl;

/********** show phone menu *********************/
function send2phoneMenu(obj, actionURL, _assetId){
	//alert("send2phoneMenu");
  if ( IE || MZ ){
     assetId = _assetId || "0";

  	 // xmlapi action url
  	 xmlAPIURL =  actionURL ;
     toggleVisibility('PhoneMenu');
     toggleVisibility('PhoneInputScreen','block');
     toggleVisibility('PhoneSuccessScreen', 'none');
     toggleVisibility('errorMessage', 'none');
     toggleVisibility('PhoneFaildedScreen', 'none');

     var menuObj = document.getElementById("PhoneMenu");
     menuObj.x = getXY(obj, "x");
     menuObj.y = getXY(obj, "y");
     //menuObj.style.left = menuObj.x + 90 + "px";
     menuObj.style.left = menuObj.x + "px";
     menuObj.style.top = menuObj.y + obj.offsetHeight + "px";
  }
  else {
  	alert("Sorry, your Browser does not support this function.");
  }
}

/********** hide phone meun *******************/
function hidePhoneMenu() {
	toggleVisibility('PhoneMenu', "none");
}

/************ Processing Request *******************/
function processRequest() {
  if (isProcessing) return;

  //var phoneInput = document.getElementsByName('phone') ;
  //var phoneNumber =  phoneInput[0].value + phoneInput[1].value  + phoneInput[2].value;
  //var phoneNumber =  phoneInput[0].value;
  
  var phoneNumber =  document.phoneForm.phone1.value + document.phoneForm.phone2.value  + document.phoneForm.phone3.value; 
  //alert("phoneNumber:" + phoneNumber);

  if(validateInput(phoneNumber)) {
    isProcessing = true;

    // try to call the XMLHttpRequest
    try{
		var queryString = "";
		queryString+= "assetId=" + assetId;
		queryString+= "&phoneNumber=" + phoneNumber;
		queryString+= "&send2phoneMessage=" + encodeURIComponent("To connect and bookmark the CNNMoney.com mobile site, click on the link, http://cnnmoney.mlogic.mobi/money03/item/bb");
		var actionURL = xmlAPIURL + "?" + queryString;
		callServerRequest(actionURL);
    }
    catch(err) {
    	errMsg = "* The application cannot contact the server at the moment. Please try again in a few seconds.";
		errMsg+= "\n" + err;
		//alert(errMsg + "");
    	showErrorMessage(errMsg);
    	isProcessing = false;
    }
  }
}

/*********** Validate User's Input ***************/
function validateInput(phoneNumber) {
  // validate phone number
  phoneNumber = phoneNumber.replace(/[^0-9]/g, "");
  if(phoneNumber.length != 10) {
	showErrorMessage("* Please enter valid phone number");
    return false;
  }

  return true ;
}

/*********** show error message **********************/
function showErrorMessage(errorMessage) {
	toggleVisibility('responseMessage', 'block');
	document.getElementById('responseMessageBody').innerHTML = errorMessage;
}

/************** call the Server API by dynamic <script> loading ****************/
function callServerRequest(actionURL){
	//alert(actionURL);
	var dt = new Date();
	actionURL+= "&dt=" + dt.valueOf();
    
	var oRemote = document.getElementById("oRemote");
	
	
	if(oRemote) {
		// do nothing now
		//alert("do nothig");
	}
	else {
		oRemote = document.createElement('div');
		oRemote.id = "oRemote";
		document.body.appendChild(oRemote);
	}
	
	oRemote = document.getElementById("oRemote");
	//alert("oRemote"+  oRemote);
	
	var oldScript  = document.getElementById("lastLoadedCmds");
	// remove the old loaded javascript
	if(oldScript) oRemote.removeChild(oldScript);
	
	var s = document.createElement("script"); 
	s.type="text/javascript"; 
	s.language="javascript";
	s.id = "lastLoadedCmds";
	
	if(navigator.userAgent.indexOf('Safari')) {
		s.charset = "utf-8"; 
	}
	
	s.src= actionURL; 
	s = oRemote.appendChild(s);
	//alert("s.src" + s.src);
	
	// if IE, we need to force the external script to re-render
	//if(IE) { alert('sending message...'); }
	
	isProcessing = false;
}


/************** To be called by Server *************/
function callbackClient(statusCode, replyMessage){
  //alert(statusCode);
  if(statusCode == "1") {
   	// "OK"
	isProcessing = false;
	showErrorMessage("Your text message was successfully sent!");

   }
   else {
   	// Bad
   	showErrorMessage("Unfortunately we are unable to send a message to this number");
   }
}


/******* Show/Hide Anything ******************/
function toggleVisibility(id, _visibility) {
 var obj = document.getElementById(id);

 var visibility = _visibility || null;

 if(visibility == 'block' || visibility == 'none') {
 	obj.style.display = visibility;
 }
 else {
   	if(obj.style.display == 'none')
		obj.style.display = 'block';
	else
		obj.style.display = 'none';
 }
}

/**** Get Object's X or Y position **********/
function getXY(obj, position){
  var xyValue = null;

  if(position == "x")
  	xyValue = obj.offsetLeft;
  else
  	xyValue = obj.offsetTop;

  var parentObj = obj.offsetParent;

  while (parentObj!=null){
     if(position == "x")
       xyValue+=parentObj.offsetLeft
     else
       xyValue+= parentObj.offsetTop;

     parentObj = parentObj.offsetParent;
  }

  return xyValue;
}

// -->