var timer;
var h = -450;	// set this to the same value of the top property for the pa div in the style sheet
var t = 140;	// set this to the actual pixel distance from the top where you want the pa div to be at the end of the descent

function startAp(cookiename) {
    if (cookiename != null) {
        var cookievalue = getCookieValue(cookiename);
        if (cookievalue) {
            //Allready shown, don't show again. Hide add
            hideAp();
        } else {
            //Not set. Set cookie (either session or permanent) and show add    
            if (testSessionCookie()) {
                writeSessionCookie(cookiename,1)
            } else if (testPersistentCookie()) {
                writePersistentCookie(cookiename,1)
            }
            showAp();
        }
    } else {
        //Cookie name not specified, simply show ad
        showAp();
    }
}

function hideAp() { 
	if (document.layers) document.layers.pa.visibility = 'hide';
	else if (document.all) document.all.pa.style.visibility = 'hidden';
	else if (document.getElementById) document.getElementById("pa").style.visibility = 'hidden';
}

function showAp() { 
	state=typeof tPos;
	if(state=='undefined') tPos = h;
	if(tPos < t) { 
		tPos+=10;
		if (document.layers) document.layers.pa.top = tPos+"px";
		else if (document.all) document.all.pa.style.top = tPos+"px";
		else if (document.getElementById) document.getElementById("pa").style.top = tPos+"px";
	}

	if(timer!=null) clearInterval(timer);
	timer = setTimeout("showAp()",10);
}

function getoPos() {
	if (document.layers) alert(document.layers.pa.top);
	else if (document.all) alert(document.all.pa.style.top);
	else if (document.getElementById) alert(document.getElementById("pa").style.top);
}

function validate(){
  
    var errorMsg="";
    var error=true;
  

    //validate address
    var trimmed = frm.name.value.replace(/^\s+|\s+$/g, '') ;
    if (trimmed == "" || trimmed == "Name") {
        if(error==true){     
           frm.name.focus();
        }
        
        errorMsg=errorMsg+"Please provide Name in the textbox provided.\n";
        error=false;
    }

    var trimmed = frm.company.value.replace(/^\s+|\s+$/g, '') ;
    if (trimmed == "" || trimmed == "Company Name") {
        if(error==true){     
           frm.company.focus();
        }
        
        errorMsg=errorMsg+"Please provide Company name in the textbox provided.\n";
        error=false;
    }

    //validate email
    var str = frm.email.value; // email string
     
    var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
    var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
     if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
		
      }
      else{
        
         if(error==true){     
           frm.email.focus();
         }
	errorMsg=errorMsg+"Incorrect email address.\n";
        
	error= false;
      }

    

    if(error==false){
       alert(errorMsg);
       return error; 
    }else{
      return error;
    }
}

/*Methods for cookies*/
function writeSessionCookie (cookieName, cookieValue) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
}
function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}
function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
    return true 
  else
    return false;
}
function testPersistentCookie () {
  writePersistentCookie ("testPersistentCookie", "Enabled", "minutes", 1);
  if (getCookieValue ("testPersistentCookie")=="Enabled")
    return true  
  else 
    return false;
}
function writePersistentCookie (CookieName, CookieValue) {
    var expireDate = new Date ();
    expireDate.setDate(expireDate.getDate() + 1);
    document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}