// JavaScript Document
function mouseCoords(ev){
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop
    };
}
document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;

var dragObject  = null;
var mouseOffset = null;

function getMouseOffset(target, ev){
    ev = ev || window.event;

    var docPos    = getPosition(target);
    var mousePos  = mouseCoords(ev);
    return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}
function getPosition(e){
    var left = 0;
    var top  = 0;

    while (e.offsetParent){
        left += e.offsetLeft;
        top  += e.offsetTop;
        e     = e.offsetParent;
    }

    left += e.offsetLeft;
    top  += e.offsetTop;

    return {x:left, y:top};
}
function mouseMove(ev){
    ev           = ev || window.event;
    var mousePos = mouseCoords(ev);
    //ge('bula12345').value+="x:"+mousePos.x+', '+'y: '+mousePos.y+"\n";
    if(dragObject){    
                dragObject.style.cursor = 'move';
                ge('mover').style.position = 'absolute';
                ge('mover').style.top      = (mousePos.y - mouseOffset.y)+'px';
                ge('mover').style.left     = (mousePos.x - mouseOffset.x) +'px';
                if(parseInt(ge('mover').style.left)<0){
                    ge('mover').style.left='0px';
                }
                
                if(parseInt(ge('mover').style.top)<0){
                    ge('mover').style.top='0px';
                }
                    //cont=getPosition(ge('crop_inserter'));
                    //container.x=cont.x;
                    //container.y=cont.y;

         
            return false;
            
    }
}
function mouseUp(){
    if(dragObject){
        dragObject.style.cursor = 'pointer';
        dragObject = null;
        //mouseOffset = getMouseOffset(this, ev);
    }
}

function makeDraggable(item){
    if(!item) return;
    item.onmousedown = function(ev){
        dragObject  = this;
        dragObject.style.cursor = 'move';
        mouseOffset = getMouseOffset(this, ev);
        
        //alert('bula: '+item.id);
        return false;
    }
}
function ge(id){
return document.getElementById(id);
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


function create_plimbaret(){
    if(Get_Cookie( 'newsletter' )) return null;
	 //if(!ge( 'mover' )) return null;
    close_crop();
	var image_width=450;
	var image_height=220;
    var mover=document.createElement('div');
    mover.id="mover";
    mover.style.position='absolute';
    //mover.className='moverqq';
    var xx=getScrollXY();
    mover.style.left=(screen.width-image_width-20)/2+'px';
	mover.style.backgroundImage="url('images/bg_news.png')";
    mover.style.top=xx[0]+150+'px';
	mover.style.backgroundRepeat="no-repeat";
    mover.style.width=(image_width+20)+'px';
    mover.style.height=(image_height+70)+'px';
    
   // mover.style.border="1px solid #000000";
  //  mover.style.backgroundColor="#FFFFFF";
//    mover.style.textAlign='center';
	if(!document.body) {
			setTimeout("create_plimbaret",1000);
			return null;
	}
    document.body.appendChild(mover);
    // title 
    var movert=document.createElement('div');
    movert.id="titleMover";
    //mover.className='moverqq';
    movert.style.width=(image_width+20)+'px';
    movert.style.height=18+'px';   
    //movert.style.borderSize="1px solid #000000";
    //movert.style.backgroundColor="#0055e5";

//    mover.style.textAlign='center';
    mover.appendChild(movert);
	var moverta=document.createElement('div');
    //mover.className='moverqq';
    moverta.style.width=(image_width+20)+'px';
    moverta.style.height=18+'px';   
	moverta.style.position ='absolute';
	moverta.style.top ='8px';
	moverta.style.left ='6px';
	moverta.style.cursor ='pointer';
    //movert.style.borderSize="1px solid #000000";
    //movert.style.backgroundColor="#0055e5";
    moverta.innerHTML=' &nbsp; Join the Platinum Community';
    moverta.style.fontFamily='arial';
    moverta.style.fontSize='14px';
	moverta.style.fontSize='14px';
    moverta.style.fontWeight='bold';
	moverta.style.color='#333333';
//    mover.style.textAlign='center';
    movert.appendChild(moverta);
	var moverta=document.createElement('div');
    //mover.className='moverqq';
    moverta.style.width=(image_width+20)+'px';
    moverta.style.height=18+'px';   
	moverta.style.position ='absolute';
	moverta.style.top ='7px';
	moverta.style.left ='5px';
	moverta.style.cursor ='pointer';
    //movert.style.borderSize="1px solid #000000";
    //movert.style.backgroundColor="#0055e5";
    moverta.innerHTML=' &nbsp; Join the Platinum Community';
    moverta.style.fontFamily='arial';
    moverta.style.fontSize='14px';
	moverta.style.fontSize='14px';
    moverta.style.fontWeight='bold';
	moverta.style.color='#FFFFFF';
//    mover.style.textAlign='center';
    movert.appendChild(moverta);

    // div close
    var inchis=document.createElement('div');
    inchis.style.width='22px';
    inchis.style.height='22px';
	//inchis.style.border='1px #ffffff solid';
	inchis.style.cursor='pointer';
    //inchis.innerHTML="<a href='javascript:close_crop();' style='text-decoration:none;color:#000000;' title='Close Window'>[x]</a>";
    inchis.style.position='absolute';
    inchis.style.right='4px';
	inchis.onclick=function (){
		 close_crop();
		}
    inchis.style.top='4px';
    movert.appendChild(inchis);
    //----------------------------------------
    
    //---------------------------------------------
    var mainer_cont=document.createElement('div');
    mainer_cont.style.width=image_width+'px';
    mainer_cont.id='crop_inserter';
    mainer_cont.style.height=image_height+'px';
    mainer_cont.style.marginLeft='10px';
    mainer_cont.style.marginTop='10px';
    mainer_cont.style.marginBottom='10px';
    //mainer_cont.style.backgroundImage = 'url('+image_href+')';
    mover.appendChild(mainer_cont);
    //---------------------------------------------
    //var crop_cont=document.createElement('div');
    //crop_cont.id='plutitor';
    //crop_cont.style.width=parseInt(crop_width*percentage/100)+'px';
    //crop_cont.style.position='absolute';
    //crop_cont.style.textAlign='left';
    //crop_cont.style.height=parseInt(crop_height*percentage/100)+'px';
   // document.body.appendChild(crop_cont);
    //alert( crop_cont.style.width+' '+crop_cont.style.height);
   // var doterer=document.createElement('div');
   // doterer.id='doterer';
   // doterer.style.position='relative';
   // doterer.style.width=parseInt(crop_width*percentage/100)-2+'px';
   // doterer.style.height=parseInt(crop_height*percentage/100)-2+'px';
   // doterer.className='crop_dottedDiv';
   // crop_cont.appendChild(doterer);
    //----------------------------------------------
   
    //---------------------------------------------
    makeDraggable(document.getElementById('titleMover'))
    var mover=document.createElement('div');

    ge('crop_inserter').innerHTML+=  '<table width=\"447\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">\n  \n  <tr>\n    <td colspan=\"2\"><div align=\"justify\"><span class=\"divstil1\"><br \/>\n          <img src=\"images\/ebook2.png\"  align=\"left\" style=\'margin-right:5px\' \/>Sign-up for our mailing       list to receive helpful information, tips, and advice to grow your business,       increase sales, and help you be a more effective Real Estate agent.        As an added bonus, you\'ll receive Laura Duggan\'s new e-book <strong>\"10       Things Every Buyer Should See BEFORE Making an Offer on a Home\", a       $39.95 value, absolutely free<\/strong> with no further obligations.        \n    It\'s just our way of saying thanks for joining the Platinum community.<\/span> <br>   <\/div><\/td>\n  <\/tr>\n  <tr>\n    <td width=\"147\" class=\"divstil2\">First Name:<\/td>\n    <td width=\"292\" class=\"divstil1\"><label>\n      <input type=\"text\" name=\"news_fname\" id=\"news_fname\" style=\"width:290px; border:#CCCCCC 1px solid; padding:0px; margin:0px; background:transparent \" \/>\n    <\/label><\/td>\n  <\/tr>\n  <tr>\n    <td class=\"divstil2\">Last Name:<\/td>\n    <td class=\"divstil1\"><input type=\"text\" name=\"news_lname\" id=\"news_lname\"  style=\"width:290px; border:#CCCCCC 1px solid; padding:0px; margin:0px; background:transparent\" \/><\/td>\n  <\/tr>\n  <tr>\n    <td class=\"divstil2\">E-Mail:<\/td> \n    <td class=\"divstil1\"><input type=\"text\" name=\"news_email\" id=\"news_email\"  style=\"width:290px; border:#CCCCCC 1px solid; padding:0px; margin:0px;background:transparent \" \/><\/td>\n  <\/tr>\n  <tr>\n    <td class=\"divstil1\">&nbsp;<\/td>\n    <td class=\"divstil1\"><label> \n      <input type=\"submit\" name=\"button\" id=\"button\" value=\"Signup\" style=\"width:150px\" onclick=\'submit_infos()\' \/>\n    <\/label><br><a href=\'javascript:donotshowup();\'>Do not show this message again<\/a><\/td>\n  <\/tr>\n<\/table>';
	
    ge('mover').appendChild(mover);
	
}
function close_crop(){
	if(ge('mover')){
		document.body.removeChild(ge('mover'));
	}
}
function submit_infos(){
	if(ge('news_fname').value==''){
		alert('Please Make sure you fill the First Name field!');
		return 0;
	}
	if(ge('news_lname').value==''){
		alert('Please Make sure you fill the Last Name field!');
		return 0;
	}
	if(!isValidEmail(ge('news_email').value)){
		alert('Please Make sure you fill the Email field!');
		return 0;
	}
	if(ge('news_fname').value!='' && ge('news_lname').value!='' && isValidEmail(ge('news_email').value)){
		//alert("f="+ge('news_fname').value+"&l="+ge('news_lname').value+"&e="+ge('news_email').value);
		ajaxRunFunction('jason/submit_newsletter.php',"newsletter_submited","f="+ge('news_fname').value+"&l="+ge('news_lname').value+"&e="+ge('news_email').value);
	}
}

function isValidEmail(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function newsletter_submited(string){
	string=unescape(string);
	ge('crop_inserter').innerHTML= '<table width=\"447\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n  <tr>\n    <td align=\"center\" class=\"divstil1\"><p><br \/>\n<br \/>\n<br \/>\n<br \/>\n   <b>   Thank you for joining the Platinum community.  <br \/>\n      <br \/>\n      You will       be receiving a confirmation e-mail with further instructions on how       to obtain your free e-book.  <br \/>\n      <br \/>\n    You can now close this window at any       time.</b><\/p>\n    <p>&nbsp; <\/p><\/td>\n  <\/tr>\n<\/table>';
    Set_Cookie( 'newsletter', 'nopopup', 1000, '', '', '' );
}

function donotshowup(){
   Set_Cookie( 'newsletter', 'nopopup', 1000, '', '', '' ); 
   close_crop();
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
    
    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );
        
        
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    
        // if the extracted name matches passed check_name
        if ( cookie_name == check_name )
        {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 )
            {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found )
    {
        return null;
    }
}    
