/** 
 * @fileoverview File name: mobile.js
 * This file is to be used by all the Ajax related pages for handling 
 * the client side ajax request, and then send back the data to display 
 * it in the cooresponding place holder.
 * The copyright belongs to {@link http://www.theweathernetwork.com} Pelmorex inc.
 *
 * @author Daniel, 07MAR2006
 * @version		1.0
 */

/** 
 * This is a global variable presents an Ajax object.
 * Type: object of GetXmlHttpObject(stateChanged)
 * Variable name: xmlHttp
 */
var xmlHttp

/** 
 * This is a global variable presents a DIV tag name.
 * Type: string
 * Variable name: placeholder
 */
var placeholder

///** 
// * This is a global variable presents a div tag height.
// * Type: integer
// * Variable name: saveHeight
// */
//var saveHeight

/**
 * This is an basic Ajax function to implement the method responseText
 */
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		if(xmlHttp.status == 200){
			//HTTP OK
			document.getElementById(placeholder).innerHTML=xmlHttp.responseText;
		}
		else{
			// something went wrong, report the error
			error( "HTTP "+request.status+". An error was encountered: "+ request.statusText );
		}		
	} 
} 

/**
 * This is an basic Ajax function to get the XmlHttpObject.
 * It checks the user browser type and return the correct object.
 * @param {object} handler
 * @returns An aJax XmlHttpObject object
 * @type object
 */
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0){
		//alert("This example doesn't work in Opera") 
		//return 
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp;
	}
	else if (navigator.userAgent.indexOf("MSIE")>=0){ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP"
		} 
		try{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e){ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return NULL;
		} 
	}
	else if (navigator.userAgent.indexOf("Mozilla")>=0){
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp;
	}
	else return NULL;
}

/**
 * This is a genetic function to handle ajax data.
 * For param para: para[0]=>number of parameter that passed to the buffer;
 * para[i]=>the value of each parameter.
 * @param {string} divtag div tag name for returning ajax data
 * @param {string} myevent the event that this function will invoke
 * @param {array} para parameter array, 
 */
function ajaxDataHandler(divtag, myevent)
{ 
	var url = myevent
	placeholder = divtag
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null);
}


/**
 * This is a function to handle phone number input
 */
lastKey=null;
function hop(el, next, maxLength)
{
  // Strip out trailing non-numeric from current el,
  // and move focus from el to next as necessary.

  var pos = el.value.length ? "0123456789".indexOf(el.value.charAt(el.value.length - 1)) : 0;

  if (hop.arguments.length == 2)
    maxLength = el.maxLength;

  if (pos == -1)
    el.value = el.value.slice(0, el.value.length - 1);

  if ((lastKey == el.name + (el.value.length - 1)) && (el.value.length >= maxLength))
    next.focus();

  if (el.name == 'area')
  {
    if (el.value.charAt(0) == '1')
      el.value = el.value.slice(1);

    if (el.value.length > maxLength)
    {
      // cut and paste handler - check whole string for non-alpha
      pos = 0;
      do
      {
	if ("0123456789".indexOf(el.value.charAt(pos)) == -1)
	  el.value = pos ? el.value.slice(0, pos) + el.value.slice(pos + 1) : el.value.slice(1);
	else
	  pos++;
      } while (pos < el.value.length);

      // now divvy up the data amongs the other fields
      if (el.value.charAt(0) == '1')
	el.value = el.value.slice(1);

      if (el.value.length > 3)
      {
	el.form.elements.firstthree.value = el.value.slice(3,6);
	el.form.elements.lastthree.value = el.value.slice(6,10);
	el.value = el.value.slice(0,3);

	if (el.form.elements.lastthree.value.length > 0)
	  el.form.elements.lastthree.focus();
	else
	  if (el.form.elements.firstthree.value.length > 0)
	  {
	    if (el.form.elements.firstthree.value.length == 3)
	      el.form.elements.lastthree.focus();
	    else
	      el.form.elements.firstthree.focus();
	  }
      }
    }
  }
  lastKey = el.name + (el.value.length);
}

/**
 * This is a function to handle ajax call to send traffic eye SMS
 * @param {string} divtag div tag name for returning ajax data
 * @param {string} URL the php file to be called
 */
function sendTrafficEyeSMS(divTag, URL)
{
        //$lang: not been used since we don't have french version yet.
	var error = "";
	var areacode = this.document.frSendSMS.areacode;
	var firstthree = this.document.frSendSMS.firstthree;
	var lastthree = this.document.frSendSMS.lastthree;
	var SMSProvider = this.document.frSendSMS.SMSProvider;
	var phoneType = this.document.frSendSMS.phonetypevalue;
	//alert(phoneType.length);
	if (areacode.value == "" || areacode.value.length < 3)
	{
		error = "Please enter a correct area code !\n";
		alert(error);
		areacode.focus();
		return;
	}
	else if (firstthree.value == "" || firstthree.value.length < 3)
	{
		error = "Please enter a correct phone number !\n";
		alert(error);
		firstthree.focus();
		return;
	}
        else if (lastthree.value == "" || lastthree.value.length < 4)
	{
                error = "Please enter a correct phone number !\n";
		alert(error);
		lastthree.focus();
		return;
	}        
	else if (this.document.frSendSMS.SMSProvider.value == "")
	{
		error = "Please select an SMS provider !\n";
		alert(error);
		SMSProvider.focus();
		return;
	}
	else if (phoneType)
	{
		// set var radio_choice to false
		var radio_choice = false;
		// Loop from zero to the one minus the number of radio button selections
		for (counter = 0; counter < phoneType.length; counter++)
		{
			// If a radio button has been selected it will return true
			// (If not it will return false)
			if (phoneType[counter].checked){
				radio_choice = true;
				var selectPhoneType = phoneType[counter].value;
			}
		}
		if (!radio_choice)
		{
			// If there were no selections made display an alert box 
			error = "Please select a phone type !\n";
			alert(error);
			phoneType.focus();
			return;
		}
		else{
			var phoneNum = areacode.value+firstthree.value+lastthree.value;
			var theURL = URL+'?phoneNum='+phoneNum+'&SMSProvider='+SMSProvider.value+'&selphonetype='+selectPhoneType;
			this.ajaxDataHandler(divTag,theURL);
		}
	}
}

function changeProvider(phoneType)
{
	var SMSProvider = this.document.frSendSMS.SMSProvider;
	switch (phoneType){
		case 'phone':
			SMSProvider[0].selected="selected";
			SMSProvider.disabled="disabled";
			break;
		case 'blackberry':
			SMSProvider.disabled="";
			break;
	}
}

/**
 * This is a function to handle ajax call to send weather eye SMS
 * @param {string} divtag div tag name for returning ajax data
 * @param {string} URL the php file to be called
 * @param {string} lang the language flag, en:English, fr:French
 */
function sendWeatherEyeSMS(divTag, URL, lang)
{
	var error = "";
	var areacode = this.document.frSendSMS.areacode;
	var firstthree = this.document.frSendSMS.firstthree;
	var lastthree = this.document.frSendSMS.lastthree;
	var SMSProvider = this.document.frSendSMS.SMSProvider;
	var submitFlag = this.document.frSendSMS.event;
	if (areacode.value == "" || areacode.value.length < 3)
	{
                if(lang=='fr'){
                    error = "Veuillez entrer le bon indicatif régional !\n";
                }
                else {
                    error = "Please enter a correct area code !\n";
                }
		alert(error);
		areacode.focus();
		return;
	}
	else if (firstthree.value == "" || firstthree.value.length < 3)
	{
                if(lang=='fr'){
                    error = "Veuillez entrer le bon numéro de téléphone !\n";
                }
                else {
                    error = "Please enter a correct phone number !\n";
                }
		alert(error);
		firstthree.focus();
		return;
	}
        else if (lastthree.value == "" || lastthree.value.length < 4)
	{
                if(lang=='fr'){
                    error = "Veuillez entrer le bon numéro de téléphone !\n";
                }
                else {
                    error = "Please enter a correct phone number !\n";
                }
		alert(error);
		lastthree.focus();
		return;
	}        
	else if (this.document.frSendSMS.SMSProvider.value == "")
	{
                if(lang=='fr'){
                    error = "Veuillez sélectionner un fournisseur de message texte !\n";
                }
                else {
                    error = "Please select an SMS provider !\n";
                }
		alert(error);
		SMSProvider.focus();
		return;
	}
	else{
		var phoneNum = areacode.value+firstthree.value+lastthree.value;
		var theURL = URL+'?phoneNum='+phoneNum+'&SMSProvider='+SMSProvider.value+'&slang='+lang;
		this.ajaxDataHandler(divTag,theURL);
	}
}


function sendMobileWebSMS(divTag, URL, lang)
{
	var error = "";
	var areacode = this.document.frSendSMS.areacode;
	var firstthree = this.document.frSendSMS.firstthree;
	var lastthree = this.document.frSendSMS.lastthree;
	var SMSProvider = this.document.frSendSMS.SMSProvider;
	if (areacode.value == "" || areacode.value.length < 3)
	{
                if(lang=='fr'){
                    error = "Veuillez entrer le bon indicatif régional !\n";
                }
                else {
                    error = "Please enter a correct area code !\n";
                }
		alert(error);
		areacode.focus();
		return;
	}
	else if (firstthree.value == "" || firstthree.value.length < 3)
	{
                if(lang=='fr'){
                    error = "Veuillez entrer le bon numéro de téléphone !\n";
                }
                else {
                    error = "Please enter a correct phone number !\n";
                }
		alert(error);
		firstthree.focus();
		return;
	}
        else if (lastthree.value == "" || lastthree.value.length < 4)
	{
                if(lang=='fr'){
                    error = "Veuillez entrer le bon numéro de téléphone !\n";
                }
                else {
                    error = "Please enter a correct phone number !\n";
                }
		alert(error);
		lastthree.focus();
		return;
	}   
	else if (this.document.frSendSMS.SMSProvider.value == "")
	{
                if(lang=='fr'){
                    error = "Veuillez sélectionner un fournisseur de message texte !\n";
                }
                else {
                    error = "Please select an SMS provider !\n";
                }
		alert(error);
		SMSProvider.focus();
		return;
	}     
	else{
		var phoneNum = areacode.value+firstthree.value+lastthree.value;
		var theURL = URL+'?phoneNum='+phoneNum+'&SMSProvider='+SMSProvider.value+'&slang='+lang;
		this.ajaxDataHandler(divTag,theURL);
	}
}