/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//AJAX Components
//Generic XML HTTP Object
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try	{
		//Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {	
		//Internet Explorer
		try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
}

function ajaxGetResponse(url){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	//alert(url);
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);	
	return xmlHttp.responseText;
	//alert(result);	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function findDOM(objectId) {
	if (document.getElementById) {
		return (document.getElementById(objectId));}
	if (document.all) {
		return (document.all[objectId]);}
}

function wait(msecs){
	var start = new Date().getTime();
	var cur = start
	while((cur - start) < msecs){
		cur = new Date().getTime();
	}
}

function waitASecond(){
	//Do Nothing Just to set the timeout	
}
function imageEffect(){
	//Wait a millisecond for the next DIV
	pulse('alerts');
	setTimeout("waitASecond()",300);
	setTimeout("pulse('votingpoling')",700);
	setTimeout("waitASecond()",1000);
	setTimeout("pulse('mobiledonations')",1500);
	setTimeout("waitASecond()",1800);
	setTimeout("pulse('text2update')",2300);
	setTimeout("waitASecond()",2600);
	setTimeout("pulse('text2win')",3100);
	setTimeout("waitASecond()",3400);
	setTimeout("pulse('couponing')",3900);
	setTimeout("waitASecond()",4200);
	setTimeout("pulse('text2screen')",4700);
	setTimeout("waitASecond()",5000);
	setTimeout("pulse('text4info')",5500);
	setTimeout("waitASecond()",5800);
	setTimeout("pulse('text2capture')",6300);
	setTimeout("waitASecond()",6600);
	setTimeout("pulse('textaquestion')",7100);
	

}

function pulse(element){
	var image,speed,width, height,sWidth,sHeight,sXpos,sYpos,gWidth,gHeight,gXpos,gYpos,percentage,splitter,i;
	image = document.getElementById(element);
	 
	width = image.offsetWidth;
	height = image.offsetHeight;
	
	percentage = 100 / width;
	
	gXpos = (width / 2) * -1;
	gYpos = (height / 2) * -1;
	gWidth = width * percentage;
	gHeight = height * percentage;
	
	//Make Image Grow
	imageGrow(element,gWidth,gHeight);
	
	//Wait
	setTimeout("waitASecond()",150);
	
	//Make Image Shrink
	setTimeout("imageShrink('"+element+"','"+width+"','"+height+"')",450);
		
}

function imageGrow(element,mW,mH) {
	var timer = null;
		
	growImg = document.getElementById(element);
 	img_width = growImg.offsetWidth; 
	img_height = growImg.offsetHeight;
	
 	//Make the image Grow
    if(img_width != mW) {
        zoom("+",element,img_height/img_width,mW,mH);
		timer = setTimeout('imageGrow(\''+element+'\',\''+mW+'\',\''+mH+'\')', 1);
    } else {
        clearTimeout('timer');
    }
	
}

function imageShrink(element,mW,mH) {
	//dynamic image resizing
	var timer = null;
	
	growImg = document.getElementById(element);
	img_width = growImg.offsetWidth; 
	img_height = growImg.offsetHeight;
	
	//Make the image Grow
    if(img_width != mW) {
        zoom("-",element,img_height/img_width,mW,mH);
		timer = setTimeout('imageShrink(\''+element+'\',\''+mW+'\',\''+mH+'\')', 1);
    } else {
        clearTimeout('timer');
    }
	
}

function zoom(type,imgx,sz,mW,mH) {
	imgd = findDOM(imgx);
	
	if (type=="+" && imgd.width < mW && imgd.height < mH) {
		imgd.width += 2;
		imgd.height += (2*sz);
		//imgd.top = (imgd.height / 2) * -1;
		//imgd.left = (imgd.width / 2) * -1;		
	}
	
	if (type=="-" && imgd.width > mW && imgd.height > mH) {
		imgd.width -= 2;
		imgd.height -= (2*sz);
		//imgd.top = (imgd.height * 2);
		//imgd.left = (imgd.width * 2);	
	}
} 
	
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr,option; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) {
	
	//option = getSelectedImage();
	
	//if(option != a[0].name){
		x.src=x.oSrc;
		
		if(a[0].name == "Immediacy" || a[0].name == "Complementary" || a[0].name == "Connection" || a[0].name == "Results" || a[0].name == "Gateway"){
			var banners=new fadeSlideShow({
				wrapperid: "indexMenuImage", //ID of blank DIV on page to house Slideshow
				dimensions: [733, 270], //width/height of gallery in pixels. Should reflect dimensions of largest image
				imagearray: [
					["images/rfm_banner.jpg","applications.php","_self"],
					["images/menu1.jpg","applications.php","_self"],
					["images/menu2.jpg","applications.php","_self"],
					["images/menu3.jpg","applications.php","_self"],
					["images/menu4.jpg","applications.php","_self"],
					["images/menu5.jpg","applications.php","_self"]],		
				displaymode: {type:'auto', pause:7000, cycles:0, wraparound:false, randomize:false},
				persist: false, //remember last viewed slide and recall within same session?
				fadeduration: 500, //transition duration (milliseconds)
				descreveal: "always",
				togglerid: ""
			})
		} else if(a[0].name == "brand" || a[0].name == "charity" || a[0].name == "agency" || a[0].name == "testimonial") {
			mainImageSwap(gup('t'));
		} else if(a[0].name == "retail" || a[0].name == "broadcast" || a[0].name == "nonprofit" || a[0].name == "healthcare" || a[0].name == "agency" || a[0].name == "events") {
			mainImageSwap(gup('t'));
		}
					
	//}
	
  }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  
  if(!d) 
  	d=document; 
  
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  
  if(!(x=d[n])&&d.all) 
  	x=d.all[n]; 
  
  for (i=0;!x&&i<d.forms.length;i++) 
  	x=d.forms[i][n];
  
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
  	x=MM_findObj(n,d.layers[i].document);
	
  if(!x && d.getElementById) 
  	x=d.getElementById(n); 

 return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; 
	document.MM_sr=new Array; 
	for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){
			document.MM_sr[j++]=x; 
			if(!x.oSrc) 
				x.oSrc=x.src; 
			
			x.src=a[i+2];
			
			if(a[0] == "Immediacy" || a[0] == "Complementary" || a[0] == "Connection" || a[0] == "Results" || a[0] == "Gateway"){
				//selectImage(a[0],a[4]);
				mainImageSwap(a[0]);
			} else if(a[0] == "brand" || a[0] == "charity" || a[0] == "agency" || a[0] == "testimonial") {
				//selectImage(a[0],a[4]);
				mainImageSwap(a[0]);
			} else if(a[0] == "retail" || a[0] == "broadcast" || a[0] == "nonprofit" || a[0] == "healthcare" || a[0] == "agency" || a[0] == "events") {
				//selectImage(a[0],a[4]);
				mainImageSwap(a[0]);
			}
			
			if(a[1] != ""){
				var showCaseStudy = document.getElementById(a[1]);
				
				if(showCaseStudy != null)
					showCaseStudy.style.display = "block";			
			}
		}
}

function removeCaseStudy( element ){
	var showCaseStudy = document.getElementById(element);
	showCaseStudy.style.display = "none";	
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function selectImage(option,page){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url,selected;
	
	if(option != "reset"){
		selected = getSelectedImage();
		if(selected != option)
			resetMenu(page,selected);
	}
	
	url="ajax.php?action=menu&option="+option+"&page="+page;
	//alert(url);
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);	
	result = xmlHttp.responseText;
	//alert(result);	
}

function getSelectedImage(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	url="ajax.php?action=menu";
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);	
	result = xmlHttp.responseText;
	return result;
}

function resetMenu(page,old){
	if(old.length > 0){
		var img = document.getElementById(old);
		if(page == "index"){
			if(old == "Immediacy"){
				img.src = "images/immediacy_btn.png";
			}else if(old == "Complementary"){
				img.src = "images/complementary_btn.png";
			}else if(old == "Connection"){
				img.src = "images/connection_btn.png";
			}else if(old == "Result"){
				img.src = "images/results_btn.png";
			}else if(old == "Gateway"){
				img.src = "images/gateway_btn.png";
			}	
		}else if(page == "clients"){
			
		}
	}	
}


function signupToNewsletter(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
		
	var url,email,result;
	
	email = document.getElementById("email");
	
	url="ajax.php?action=newsletter&email=" + email.value;
	//alert(url);
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);	
	
	result = xmlHttp.responseText;
	//alert(result);
	/*if(result == "true")
		alert("Newsletter submitted successfully");
	else
		alert("Newsletter error");
	*/
}

function activateClient(id,check){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
		
	var url,active,result;
	
	if(check.checked)
		active = 1;
	else
		active = 0;
		
	url="ajax.php?action=activateclient&value=" + active + "&cid=" + id;
	//alert(url);
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);	
	
	result = xmlHttp.responseText;
}

function smsDemo(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
		
	var url,params,result,npx,areacode,extension,msisdn;
	
	areacode = document.getElementById("areaCode");
	npx = document.getElementById("npx");
	extension = document.getElementById("extension");
	
	if(areacode.value != "" && npx.value != "" && extension.value != "")
		msisdn = areacode.value + npx.value + extension.value;
	else
		msisdn = "";
	
	//alert(msisdn);
	
	if(msisdn != "" && msisdn.length == 10){
		url="ajax.php?action=demo&msisdn="+msisdn;
		//alert(url);
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);	
		result = xmlHttp.responseText;
		//alert(result);
		if(result == "true"){
			var demoForm = document.getElementById("demoTxt");
			demoForm.innerHTML = "<table><tr><td colspan=\"5\" valign=\"top\" height=\"25\"><div style=\"position:relative; top:10px;\">Thank you for using our demo!<br /><a href=\"Javascript:reloadForm();\">Go Back</a></td></tr></table>";
		} else
			alert("Demo error:" + result);	
	} else
		alert("Bad Phone");
	
}

function reloadForm(){
	var demoForm = document.getElementById("demoTxt");
	
	demoForm.innerHTML = "<table><tr><td colspan=\"5\" valign=\"top\" height=\"25\"><div style=\"position:relative; top:10px;\">Try our demo, Enter your number below</div></td></tr><tr><td><img src=\"images/sms_icon.png\" /></td>\n"
									+ "<td><input type=\"text\" name=\"areaCode\" id=\"areaCode\" size=\"3\" maxlength=\"3\" onkeyup=\"return autoTab(this, 3, event);\" /></td>"
									+ "<td><input type=\"text\" name=\"npx\" id=\"npx\" size=\"3\" maxlength=\"3\" onkeyup=\"return autoTab(this, 3, event);\" /></td>"
									+ "<td><input type=\"text\" name=\"extension\" id=\"extension\" size=\"4\" maxlength=\"4\" onkeyup=\"return autoTab(this, 3, event);\"/></td>"
									+ "<td><img src=\"images/submit.png\" name=\"demoSubmit\" id=\"demoSubmit\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"swapImage('demoSubmit','','images/submit_over.png',1)\" onmouseup=\"Javascript:smsDemo();\" /></td></tr></table>";	
									
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}

	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}
	
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	
	return true;
}

function getMobileServiceClients(service){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
		
	var url,params,result;
	
	if(service != ""){
		url="ajax.php?action=casestudies&service="+service;
		//alert(url);
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);	
		result = xmlHttp.responseText;
		return result;
	}
}


function getElm(eID) {
	return document.getElementById(eID);
}
function show(eID) {
	getElm(eID).style.display='block';
}
function hide(eID) {
	getElm(eID).style.display='none';
}
function setOpacity(eID, opacityLevel) {
	var eStyle = getElm(eID).style;
	eStyle.opacity = opacityLevel / 100;
	eStyle.filter = 'alpha(opacity='+opacityLevel+')';
}
function fade(eID, startOpacity, stopOpacity, duration) {
	var speed = Math.round(duration / 100);
	var timer = 0;
	if (startOpacity < stopOpacity){
		for (var i=startOpacity; i<=stopOpacity; i++) {
			setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
			timer++;
		} return;
	}
	for (var i=startOpacity; i>=stopOpacity; i--) {
		setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
		timer++;
	}
}
function fadeIn(eID) {
	setOpacity(eID, 0); show(eID); var timer = 0;
	for (var i=1; i<=100; i++) {
		setTimeout("setOpacity('"+eID+"',"+i+")", timer * 5);
		timer++;
	}
}
function fadeOut(eID) {
	var timer = 0;
	for (var i=100; i>=1; i--) {
		setTimeout("setOpacity('"+eID+"',"+i+")", timer * 3);
		timer++;
	}
	setTimeout("hide('"+eID+"')", 310);
}
