/*
 Dt: 11/10/2008
 Summary of Code:
 
 When the Widget is loaded, it calls the "GetExpiry" function to get the Expiration Time.
 The returned hashed timestamp value is stored in a hidden form field, "expiry".

 After expiry is returned from the server, widget is ready to be used by the user.

 When user enters phone number, on each "KeyUp" event, goToPrefix / goToSuffix / goToSend function is called.
 These functions check the length of the textfield and trigger the auto-tab if a field is full.

 When user presses "SEND", the "validate" function is called which validates phone number length and also checks if the input is numeric or not.
 If valid, "doRequest" function is called, else an error message is displayed to the user in a pop up.

 In doRequest, an Ajax call to the proxy is made, along with required parameters.
 Returned JSON output is displayed as a message to the user in a pop up.

 */

//get the Expiration Time by calling the proxy GetExpiry function
function getExpiry(){
	$('error').hide();
	$('content').hide();
	$('sidebar').hide();	
	$('result').hide();
	$('spinner').show();

	//get the expiration time

	new Ajax.Request("GetExpiry.aspx",
		{
		    //make GET request

		    method: 'get',

		    onCreate: function() {

		        //display spinner

		        $('spinner').show();
		    },

		    onComplete: function() {

		        //hide spinner

		        $('spinner').hide();
		        $('content').show();
		        $('sidebar').show();
		        $('area').focus();
		    },

		    onSuccess: function(transport) {

		        //store the retrieved timestamp hash

		        $('expiry').value = transport.responseText;
		    },

		    onFailure: function() {

		        //display error message

		        $('result').innerHTML = "Error: unable to fetch timestamp.";
		    }
		}
	);
}

function clearError(){
	//remove the error box
	Effect.Fade('error');
}

//validate the form and make a request to the server
function validate(){
//validate length and check if the number is numeric or not

	if (isNaN($F('area')) || $F('area').length!=3) {
		$('error').innerHTML="Area Code is Invalid<br/> <a href='#' onClick='clearError()'>close</a>";
		$('area').focus();
		$('error').show();
		return false;
	}

	if (isNaN($F('prefix')) || $F('prefix').length!=3) {
		$('error').innerHTML="Phone Number is Invalid<br/> <a href='#' onClick='clearError()'>close</a>";
		$('prefix').focus();
		$('error').show();
		return false;
	}
	
	if (isNaN($F('suffix'))  || $F('suffix').length!=4) {
		$('error').innerHTML="Phone Number is Invalid<br/> <a href='#' onClick='clearError()'>close</a>";
		$('suffix').focus();
		$('error').show();
		return false;
	}

	$('content').hide();
	$('sidebar').hide();
	
	//form validated, call doRequest
	doRequest();	
}

//submit the phone number by calling the proxy Do function
function doRequest(){
	var ajax=new Ajax.Request("Do.aspx",
		{
			
			method:'get',
			parameters:{area:$F('area'),
						prefix:$F('prefix'),
						suffix:$F('suffix'),
						project_id:"test",
						expiry:$F('expiry')},
			onCreate:function(){
				//display spinner
				$('spinner').show();
				
			},
			onComplete:function(){
				//hide spinner
				$('spinner').hide();
				$('result').show();
			},
			onSuccess:function(transport){
				 	var resp=transport.responseText.evalJSON();	
					$('msg').innerHTML = resp.reason;		
			},
			onFailure:function(){
					$('msg').innerHTML = "Oops! Some error occured";
			}
		}
	);
}

function reload(){
	//hide the result and get new expiry timestamp
	getExpiry();
}


function goToPrefix(elem, ev){
	
	//check if SHIFT+TAB is pressed
	if(ev.shiftKey && ev.keyCode==9){
		elem.value='';
		elem.focus();
		return false;
	}
	if(elem.value.length == 3){
		//move to prefix field
		$('prefix').focus();
	}
}

function goToSuffix(elem, ev){
	
	//check if SHIFT+TAB is pressed
	if(ev.shiftKey && ev.keyCode==9){
		elem.value='';
		elem.focus();
		return false;
	}
	if(elem.value.length == 3){
		//move to suffix field
		$('suffix').focus();
	}
}

function goToSend(elem, ev){
	
	//check if SHIFT+TAB is pressed
	if(ev.shiftKey && ev.keyCode==9){
		elem.value='';
		elem.focus();
		return false;
	}
	if(elem.value.length == 4){
		//move to send button
		$('send').focus();
	}
}
