var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
            $("#backgroundPopup").css({
                    "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("fast");
            $("#popupContact").fadeIn("fast");
            popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContact").fadeOut("slow");
            popupStatus = 0;
    }
}

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    
    var winH = $(window).height();
    var winW = $(window).width();
    var centerDiv = $('#popupContact');
    centerDiv.css('top', winH/2-centerDiv.height()/2);
    centerDiv.css('left', winW/2-centerDiv.width()/2);

    //only need force for IE6
    $("#backgroundPopup").css({
            "height": windowHeight
    });
}

function LoadGeo(){
    var cy = '';
    var check = '';
    check = readCookie('ASCBSC');
    if(check == 'ASC'){
//        window.location = 'http://www.americansexcontacts.com/index.php?AffID=1000&CampID=2';
    }else if(check == 'BSC'){
        //window.location.href = 'http://www.britishsexcontacts.com/index.php?AffID=1000&CampID=2';
    }else{

//        cy = document.getElementById('geo_code').value();
//        if(cy == 'UK'){
//
//        }else{
//            //centering with css
            centerPopup();
            //load popup
            loadPopup();
//        }
    }
}

function DisableGeoPopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContact").fadeOut("slow");
            popupStatus = 0;
    }
}

function RedirectGeo(data){
    
    createCookie('ASCBSC',data);
    DisableGeoPopup();
    
//    if(data == 'ASC')
//        window.location = 'http://www.americansexcontacts.com/index.php?AffID=1000&CampID=2';
}

function createCookie(name, value)
{
   //alert(document.cookie = name+"="+value+";");
   document.cookie = name+"="+value+";";
}

function readCookie(name)
{
  //alert("name - "+name);
  //alert(document.cookie);
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  //alert("ca length - "+ca.length);
  //alert("Name - "+nameEQ);
  for(var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ')
            c = c.substring(1, c.length); //delete spaces
        if (c.indexOf(nameEQ) == 0){
            return c.substring(nameEQ.length, c.length);
        }
  }
  return null;
}
