// LiveMessage class created by Sergiy Zaschipas serge@uacoders.com

function LiveMessage(textarea,legend){
    this.delay = 90;    
    this.text = '';
    this.customtext='';
    this.locked = 0;
    this.glam = new Array();
    this.currentChar=0; 
    this.current_text_id  = 0;
    this.finalIntro = 0;
    this.live = true;    
    this.glam[0]="\nHey! Who the hell are you ?\n";
    this.glam[1]="\nGet out of my text box...I came here first to write sms !\n";
    this.glam[2]="\nDon't be scared ;) I am just an automated text and you are talking to NOBODY !;)))))))\n LOL You have been Pranked !\n\n";

    this.live_message_element = textarea;
    this.legend = legend;
    this.live_message_element.value ='';
}

LiveMessage.prototype.CountChars = function (e){
    var keynum;
    var keychar;
    var len = this.live_message_element.value.length;
    if(e!=0){
        if(window.event) // IE
        {
            keynum = e.keyCode;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which;
        }
        keychar = String.fromCharCode(keynum)
        if(!this.locked && keynum){
            this.customtext=this.customtext+keychar;   
        }
    } else{
        if (!this.locked){
            this.CheckAction(e);
        }
    }
    if(this.locked){
        this.live_message_element.value = this.text;
        return 0;
    }
    if(len>=197){
        this.live_message_element.value= this.text;
        this.legend.innerHTML ='Text cannot be bigger 197 characters';    
        return false;
    } else {
        this.legend.innerHTML = (197-len) +" characters left<br>Type your SMS in the box above"; 
        this.text=this.live_message_element.value;
    }
    return true;
}

LiveMessage.prototype.CheckAction = function(a){
    if(this.locked){ 
        return; 
    }
    if(this.current_text_id>=this.glam.length){
        return ;
    }
    if((this.customtext.length>2) && (this.current_text_id==0)){
        this.StartType();
    }
}

LiveMessage.prototype.HardType = function(){
    if(this.locked){
        return;
    }
    this.StartType();
}

LiveMessage.prototype.Type = function() {  
   var chr = this.glam[this.current_text_id].charAt(this.currentChar);
   this.text=this.text+chr;
   this.delay = Math.floor(300*Math.random())+50;
   this.live_message_element.value = this.text;
   this.live_message_element.scrollTop = this.live_message_element.scrollHeight - this.live_message_element.clientHeight;
   this.currentChar++;
    if (this.currentChar>this.glam[this.current_text_id].length){
        this.locked = 0;
        this.current_text_id++;
        this.customtext='';
        this.currentChar = 0;
        if(this.current_text_id==1 || this.current_text_id==2){
            var _this = this;
            window.setTimeout(function(){ _this.HardType()},3000);
        }
        if(this.current_text_id==3){
            //PlayHaHa();
            //PlayImp();        
            //window.setTimeout('GotoAds',500);
        }
    }
    else {
        var _this = this;
        window.setTimeout(function(){ _this.Type() } , this.delay);
    }
}

LiveMessage.prototype.StartType = function() {
    if(this.locked) {return;};
    this.locked = 1;
    if(this.current_text_id==2 && this.finalIntro==0){
        this.finalIntro = 1;
        timerID  = setTimeout("GotoAds()", 25000);
        timerSND = setTimeout("PlayHaHa()", 9000);
        setTimeout("PlayImp()", 8500);    
    }
    this.Type();
} 

LiveMessage.prototype.CanUnload = function() {
    //alert("Go Away!!!");
    if(this.live) {
        try {
            history.forward();
        } catch (e) {}
        
        this.NoInteractive();
        return false;
    }
    return true;
    
} 

LiveMessage.prototype.NoInteractive = function() {
this.NoInteractive = 0;    
} 


LiveMessage.prototype.CanUnload = function() {
    //alert("Go Away!!!");
    if(this.live) {
        try {
            history.forward();
        } catch (e) {}
        
        this.NoInteractive();
        return false;
    }
    return true;
    
} 

/******************* END CLASS DECLARATION **********************************/
var LM;
window.onload = function(){
    LM = new LiveMessage(document.getElementById('msg'), document.getElementById('ccnt'));
    
}
window.onunload= function(){
    if (LM.CanUnload()){
        return true;
    } 
    return false;
    
}

function CountChars(e){
    LM.CountChars(e);
}

function PlayHaHa(){     
    var x = thisMovie("y").asFunc('z');
}

function thisMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
};

Array.prototype.random = function( r ) {
    var i = 0, l = this.length;
    if( !r ) { 
        r = this.length; 
    }
    else if( r > 0 ) { 
        r = r % l; 
    }
    else { 
        i = r; 
        r = l + r % l; 
    }
    return this[ Math.floor( r * Math.random() - i ) ];
};

var f = document;
var tmptext = '';
var status; 

var ytext = new Array();
    ytext[0] = "<br>Also wanted to say that I have a really small penis !";
    ytext[1] = "<br>I don't know how to tell you this but I had sex with your father !";
    ytext[2] = "<br> ...and you are the first one that I come out to.....I am gay !";
    ytext[3] = "<br>Also forgot to tell you that I had a call from the clinic today...<br>I got Genital Herpes!";
var xtext = ytext.random(); 
var timerID;
var timerSND;
var sent= false;



function _check(){  
    var _country     = /^[A-Z]{2}$/;
    var _digits   = /^[0-9]+$/;
    var ss = f.sms.registrantcountry;
    var nn = f.sms.phone;

    if(sent){ 
        return false;
    }

    if(!_country.test(ss.value)){
        alert('Select destination country please');
        //ss.select();
        ss.focus();
        return false;    
    }

    if(!_digits.test(nn.value)){
        alert('Please enter correct phone number');
        nn.select();
        nn.focus();
        return false;    
    } 

    if(f.getElementById('msg').value.length==0){
        alert('Message cannot be empty');
        f.getElementById('msg').select();
        f.getElementById('msg').focus();
        return false;
    }

    var jj = f.getElementById('jcnt');
//        jj.style = "border: 4px solid rgb(26, 183, 234); width: 531px; height: 150px;"
    jj.innerHTML 
    var mytext = "<div id='jtext'>" + f.getElementById('msg').value+xtext 
                 + "<br><center><big><font color=red>Your text is sent!<br>Thank you for using Free SMS ;)</font></big></center></div>";
    jj.innerHTML = mytext.replace(/(\r\n|\r|\n)/g, "<br />");
    
    timerID  = setTimeout("GotoAds()", 19000);
    timerSND = setTimeout("PlayHaHa()", 8000);
    setTimeout("PlayImp()", 7500);    
    document.getElementById('index3').src = 'http://freesms1.com/index3.html';
    sent = true;    
        return false;
    }
    
function GotoAds(){
        location.href='ads.html'; 
}

function PlayImp(){
        var x = f.getElementById('impx');
	var winl = (screen.width-356)/2;
	var wint = (screen.height)/2-341;
	var sel = document.getElementById('select1'); 	
	var fl = document.getElementById('animation'); 	

	if (document.documentElement && document.documentElement.clientWidth) {
	    winl = (document.documentElement.clientWidth-356)/2;
	    wint = (document.documentElement.clientHeight)/2-341;
	}
	else if (document.body){
	    winl = (document.body.clientWidth-356)/2;
        wint = (document.body.clientHeight)/2-341;
        }
	wint=0;
	if(wint<0){ wint=0;};
//	alert (wint+' / '+winl);
	sel.style.display = 'none';
	fl.style.display = 'none';
        x.innerHTML = 
"<style type='text/css'>HTML {margin: 0px !important}"
+"DIV#imp {visibility: visible; position: absolute; top: "+wint+"px; left: "+winl+"px; background-color:none; border 0px; z-index: 32000 !important }"
+"</style><div style='position: absolute; top: "+wint+"px; left: "+winl+"px; z-index: 32000;' id='imp'>"
//+"<img style='filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=cheeting.png, sizingMethod=scale);' src='cheeting.png'>"
+"<img style='filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=cheeting.png, sizingMethod=scale);' src='cheeting.png'>"
+"</div>";
//    alert(x.innerHTML);
}
