var subscribed;

var cookieText = "1";
var cookiePrefix = ""

var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
var endCookieFlag = 'cbEndCookie';

if (wwwFlag > 0) {
    cookiePrefix = "www";
}

var cookieName = cookiePrefix + 'subscribed';

function showad() {
        if ( !Get_Cookie( cookieName ) ) {
                window.setTimeout('showad2()', 5000);
        }
}

function showad2() {
    e = document.getElementById("ad");
    center(e);
    e.style.visibility = "visible"
}

function center(object) {
    object.style.marginLeft = "-" + parseInt(object.offsetWidth / 2) + "px";
    object.style.marginTop = "-" + parseInt(object.offsetHeight / 2) + "px";
}

function Set_Cookie() {
	if (document.cookie != document.cookie) {
	    index = document.cookie.indexOf(cookieName);
	} else {
	    index = -1;
	}
	if (index == -1) {
	    document.cookie=cookieName+"="+cookieText+endCookieFlag+"; expires=Monday, 04-Apr-2020 05:00:00 GMT";
	}
}

function Get_Cookie() {
	if (document.cookie) {
		index = document.cookie.indexOf(cookieName);
		if (index != -1) {
			namestart = (document.cookie.indexOf("=", index) + 1);
			nameend = document.cookie.indexOf(endCookieFlag+";", index);
			if (nameend == -1) {
				nameend = 0;
			}
			subscribed = document.cookie.substring(namestart, nameend);
			return subscribed;
		}
	} else {
		return false
	}
}

function Delete_Cookie() {
	if (document.cookie != document.cookie) {
		index = document.cookie.indexOf(cookieName);
	} else {
		index = -1;
	}
	if (index == -1) {
		document.cookie=cookieName+"=GONE"+endCookieFlag+"; expires=Monday, 19-Aug-1996 05:00:00 GMT";
	}
	document.location.href = document.location.href;
	//to reload without queryString parameters, use the line below instead
	//document.location = document.location.href.substring(0,document.location.href.indexOf("?"));
}

function isValidEmail(str) {

                var at="@"
                var dot="."
                var lat=str.indexOf(at)
                var lstr=str.length
                var ldot=str.indexOf(dot)
                if (str.indexOf(at)==-1){
                   alert("Invalid E-mail ID")
                   return false
                }

                if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                   alert("Invalid E-mail ID")
                   return false
                }

                if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                    alert("Invalid E-mail ID")
                    return false
                }

                 if (str.indexOf(at,(lat+1))!=-1){
                    alert("Invalid E-mail ID")
                    return false
                 }

                 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                    alert("Invalid E-mail ID")
                    return false
                 }

                 if (str.indexOf(dot,(lat+2))==-1){
                    alert("Invalid E-mail ID")
                    return false
                 }
                
                 if (str.indexOf(" ")!=-1){
                    alert("Invalid E-mail ID")
                    return false
                 }

                 return true                                    
        }

function isValidQuestion(str) {
    var lstr=str.length
    if (lstr < 10) {
        alert("Invalid question, please provide more accurate...")
        return false
    }
    return true
}

function onSubmitForm() {
        if (!isValidEmail(document.form.e.value)) {
                //alert('Problem with email address, please check it again!');
                return false;
        }
        if (!isValidQuestion(document.form.cf_interest.value)) {
            return false;
        }
		Set_Cookie();
        return true;
}


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;
        }
}

window.onload = showad();
