// adding common elements used by new JS functions

var mousex;
var mousey;
var isIE = document.all?true:false;

var emailCookieStr = 'vt-email-address=';
var pswdCookieStr = 'vt-password=';

if (!isIE) document.captureEvents(Event.MOUSEMOVE);

// end new JS functionality

function ws(foo) {window.status = foo; return true;}
function foo() {window.status = ""; return true;}

function showqrWhatsThis()
{
    document.getElementById('qrWhatsThis').style.display = 'block';
}

function hideqrWhatsThis()
{
    document.getElementById('qrWhatsThis').style.display = 'none';
}

function echeck(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1 ||
		str.indexOf(at)==0 ||
		str.indexOf(at)==lstr ||
		str.indexOf(at,(lat+1)) !=-1 ||
		str.indexOf(dot)==-1 ||
		str.indexOf(dot)==0 ||
		str.indexOf(dot)==lstr ||
		str.substring(lat-1,lat)==dot ||
		str.substring(lat+1,lat+2)==dot ||
		str.indexOf(dot,(lat+2))==-1 ||
		str.indexOf(" ") !=-1)
	{
	   alert("Invalid Email Address (" + str + ").\nPlease Enter your Email Address to signup for a free VersionTracker account.");
	   return false;
	}

	 return true;			
}
function echeckli(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1 ||
		str.indexOf(at)==0 ||
		str.indexOf(at)==lstr ||
		str.indexOf(at,(lat+1)) !=-1 ||
		str.indexOf(dot)==-1 ||
		str.indexOf(dot)==0 ||
		str.indexOf(dot)==lstr ||
		str.substring(lat-1,lat)==dot ||
		str.substring(lat+1,lat+2)==dot ||
		str.indexOf(dot,(lat+2))==-1 ||
		str.indexOf(" ") !=-1)
	{
	   alert("Invalid Email Address (" + str + ").\nPlease Enter your Email Address to log in to your account.");
	   return false;
	}

	 return true;			
}
	
function ValidateEmail()
{
	var emailID=document.QuickReg.qrEmail;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address to signup for a free VersionTracker account.");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	return true;
 }
	
function jumpTo(URL_List)
{
   var URL = URL_List.options[URL_List.selectedIndex].value;
   var urlArray = new Array();
   urlArray = URL.split('/');
   //alert(URL);
   if(URL != 0)
   {
      top.location.href = URL;
	  //set cookie for this cat
	 document.cookie = "cat=" + urlArray[3] + ";path=/";
   }
}

function isBlank(str) 
{
    for (var i = 0; i < str.length; i++) 
    {
        var c = str.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
        return true;
}

function showElement(elementID)
{
    document.getElementById(elementID).style.visibility = 'visible';
    document.getElementById(elementID).style.display = 'block';
}

function showElementPositioned(elementID, xAdjust, yAdjust)
{
    var thisX = mousex + xAdjust;
    var thisY = mousey + yAdjust;
    if( thisX < 0 )
    {
        thisX = 0;
    }
    if( thisY < 0 )
    {
        thisY = 0;
    }
    document.getElementById(elementID).style.left = thisX+'px';       // place popup at the mouse X (left) location
    document.getElementById(elementID).style.top = thisY+'px';            // place popup at the mouse Y (top) location 
    showElement(elementID)
}

function showElementFixed(elementID, xAdjust, yAdjust)
{
    document.getElementById(elementID).style.left = xAdjust+'px';       // place popup at the mouse X (left) location
    document.getElementById(elementID).style.top = yAdjust+'px';            // place popup at the mouse Y (top) location 
    showElement(elementID)
}
function hideElement(elementID)
{
    document.getElementById(elementID).style.visibility = 'hidden';
    document.getElementById(elementID).style.display = 'none';
}
function unhideElement(elementID)
{
    document.getElementById(elementID).style.visibility = 'visible';
}

function showFreeRegPopInForm()
{
	alert("showFreeRegPopInForm:Here!");
	d = document.getElementById('ScratchPad');
	setInner('');
//	if(is_ie)
//	{
//		document.getElementById('versionSelectList').style.visibility = 'hidden';
//	}
	basicRPCForm('freeRegPopInForm', 400, -325, 360);
}

function quickRegPopInForm(dest,xpos,ypos)
{	
	d = document.getElementById('ScratchPad');
	setInner('');
//	if(is_ie)
//	{
//		document.getElementById('versionSelectList').style.visibility = 'hidden';
//	}
	if(xmlhttp)
    {
        xmlhttpdo('/php/jsrpc.php?function=quickRegPopInForm&dest='+dest);
    }
    else
    {
        jsrsdo('quickRegPopInForm', Array(dest));
    }
	 showElementPositioned('ScratchPad',xpos,ypos);
}

function loginPopInForm(dest,xpos,ypos)
{	
	d = document.getElementById('ScratchPad');
	setInner('');
//	if(is_ie)
//	{
//		document.getElementById('versionSelectList').style.visibility = 'hidden';
//	}
	if(xmlhttp)
    {
        xmlhttpdo('/php/jsrpc.php?function=loginPopInForm&dest='+dest);
    }
    else
    {
        jsrsdo('loginPopInForm', Array(dest));
    }
	 showElementPositioned('ScratchPad',xpos,ypos);
}

function basicRPCForm(functionName, divWidth, xAdjust, yAdjust)
{
    d = document.getElementById('ScratchPad');
    d.innerHTML = 'Loading...';
    d.style.width=divWidth+'px';
    
    if(xmlhttp)
    {
        xmlhttpdo('/php/jsrpc.php?function='+functionName);
    }
    else
    {
        jsrsdo(functionName, Array())
    }
    showElementPositioned('ScratchPad',xAdjust,yAdjust);
}
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
function getSelectedRadio(buttonGroup) {
    // returns the array number of the selected radio button or -1 if no button is selected
    if (buttonGroup[0]) {  // if the button group is an array (one button is not an array)
       for (var i=0; i<buttonGroup.length; i++) {
          if (buttonGroup[i].checked) {
             return i
          }
       }
    } else {
       if (buttonGroup.checked) { return 0; }  // if the one button is checked, return zero
    }
    // if we get to this point, no radio button is selected
    return -1;
 }  // Ends the "getSelectedRadio" function
 
// encode a url for passing as a query string within another url
function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
  }

// this function gets the cookie, if it exists
function getCookie( name ) 
{
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
        return null;
    }
    if ( start == -1 ) 
        return null;
        
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) 
        end = document.cookie.length;
        
    return unescape( document.cookie.substring( len, end ) );
}

function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* IE iFrame shim for select box bug */
function ieHover(elem) {
  var ieDivShim = document.getElementById(elem);
  // add in the iframe
  ieDivShim.innerHTML = '<iframe src="about:blank" scrolling="no" frameborder="0" id="ieTrickFrame"></iframe>' + ieDivShim.innerHTML;
  // get the iframe & adjust
  var ieMat = ieDivShim.firstChild;
	ieMat.style.width  = ieDivShim.offsetWidth+"px";
	ieMat.style.height = ieDivShim.offsetHeight+"px";	
	ieMat.style.zIndex = ieDivShim.style.zIndex - 1;
}

function setMobileCookie(site)
{
	var expires = new Date('January 1, 2010 00:00:00');
	document.cookie = 'vt-mobile=' + site +'; expires=' + expires.toGMTString() + '; path=/; domain=versiontracker.com;';
	window.location = 'http://iphone.versiontracker.com';
}

