function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function strToLower(str) {
	ltext = document.getElementById(str).value
	casechanged=ltext.toLowerCase()
	document.getElementById(str).value = casechanged;
}

function strTrim(str) {
var elem = document.getElementById(str).value;
document.getElementById(str).value = elem.replace(/^\s+|\s+$/g, '');
}

function strProperCase(str) {
var elem = document.getElementById(str).value;
document.getElementById(str).value = elem.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
}

function echeck(str) {
		var emailID=document.getElementById("from");
		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 address");
			emailID.focus();
			return false;
		}

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

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

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

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

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

function ValidateForm(){
	var emailID=document.getElementById("from");
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email address");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus();
		return false;
	}
	return true;
 }

// This receives a parameter which indicates if user is already registered or not
// If user is registered, he is not taken through reg process
var disableIt = "no";
function fnValidateForm(toDo) {
	document.getElementById('submit_btn').style.display = 'none';
	if ((document.register.name.value == "") || (document.register.from.value == ""))
	{
		alert("To receive your FREE* package, please fill out both fields in the form provided.");
		document.getElementById('submit_btn').style.display = '';
		document.register.name.focus();
		return false;
	} else {
		var name = document.getElementById('name').value;
		var uname = replaceForURL(name);
		var nameparts = name.split(" ");
		var fname = replaceForURL(nameparts[0]);
		var lname = replaceForURL(nameparts[1]);
		var i = 0;
		for(i=2;i<nameparts.length;i++)
			lname += replaceForURL(" "+nameparts[i]);
		if(lname == "undefined") lname = "";
		var email = replaceForURL(document.getElementById('from').value);
		if(toDo != "yes" && cookieReady('curuser') == "no") { startRegistrationProcess(uname, email, fname, lname); }
		else {
			$.blockUI({ message: '<h1><img src="Loading.gif" /> Just a moment...</h1>' });
			disableIt = "yes";
			document.getElementById("register").action = "https://www.bipolardisorderdvd.com/signup11/cms/orderform.php?verify=true&Contact0Email="+email+"&Contact0FirstName="+fname+"&type=CustomFormWeb&Contact0Country=&contactId=185176&Contact0StreetAddress2=&Contact0State=&xid=8fd3150e3aabacfc042b277f708bb189&Contact0LastName="+lname+"&Contact0StreetAddress1=&Contact0PostalCode=&Contact0Phone1=&Contact0City=";
			document.getElementById("register").submit();
		}
		return false;
	}
}
// If user is not registered, start registration process
function startRegistrationProcess(uname, email, fname, lname) {
	// Invoke the registration process in hidden iframe
	document.getElementById('regFrame').src='redirectHandle.php?email='+email+'&name='+uname;
	$.blockUI({ message: '<h1><img src="Loading.gif" /> Just a moment...</h1>' });
	// Run a dom hook to wait untill registration process does not completes
	domHook(3, email, fname, lname);
	return true;
}
// Domhook, which utilizes server side sleep ability wait until registration does not completes
function domHook(theTime, email, fname, lname) {
	$.ajax({
	type: "GET",
	url: "domWait.php",
	data: "wait="+theTime,
	success: function(msg){ 
		disableIt = "yes";
		document.getElementById("register").action = "https://www.bipolardisorderdvd.com/signup11/cms/orderform.php?verify=true&Contact0Email="+email+"&Contact0FirstName="+fname+"&type=CustomFormWeb&Contact0Country=&contactId=185176&Contact0StreetAddress2=&Contact0State=&xid=8fd3150e3aabacfc042b277f708bb189&Contact0LastName="+lname+"&Contact0StreetAddress1=&Contact0PostalCode=&Contact0Phone1=&Contact0City=";
		document.getElementById("register").submit();
	}
	});
	return true;
}
// Make url parameters url safe
function replaceForURL(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace(',', '%2C');
	str = str.replace('-', '%2D');
	str = str.replace('.', '%2E');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	str = str.replace('_', '%5F');
	return str;
}
// Function to check if cookie exists
function cookieReady(c_name) {
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			return "yes";
		}
	}
	return "no";
}
// Custom javascript sleep, not optimized, use dom hook isntead
function sleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
