// JavaScript library for ListandPress.com
// Copyright 2006 Avinesh Bangar (avineshb at gmail dot com)

var digits = "0123456789";
var validWorldPhoneChars = "+()- ";
var minDigitsInIPhoneNumber = 10;
var arSelected = new Array();
var rArray = new Array();

function checkExtended() 
{
  if (window.event && window.event.keyCode >= 122) 
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return false;
  }
}

function checkExtended2() 
{
  if (window.event && window.event.keyCode == 32 || window.event.keyCode >= 122) 
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return false;
  }
}

// Check whether string s is empty.
function is_empty(s)
{   
	return ((s == null) || (s.length == 0))
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isPhoneNum(num)
{
	if (checkInternationalPhone(num.value)==false)
	{
		return false;
	}
	return true;
 }

function isEmailAddr(email)
{
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}

function GetRadioValue(rArray)
{
	try
	{
		for (var i=0;i<rArray.length;i++)
		{
			if (rArray[i].checked)
				return rArray[i].value;
		}
	
		return false;
	}
	catch (e) { return true; }
}

function checkForm()
{
	if(document.register_form.prefix.value == "Select")
	{
		alert("Please select a prefix.");
		document.register_form.prefix.focus();
		return false;
	}
	if(is_empty(document.register_form.firstname.value))
	{
		alert("You must enter your first name.");
		document.register_form.firstname.focus();
		return false;
	}
	if(is_empty(document.register_form.lastname.value))
	{
		alert("You must enter your last name.");
		document.register_form.lastname.focus();
		return false;
	}
	if(is_empty(document.register_form.company.value))
	{
		alert("You must enter your company.");
		document.register_form.company.focus();
		return false;
	}
	if(is_empty(document.register_form.addr1.value))
	{
		alert("You must enter your address.");
		document.register_form.addr1.focus();
		return false;
	}
	if(is_empty(document.register_form.city.value))
	{
		alert("You must enter your city.");
		document.register_form.city.focus();
		return false;
	}
	if(is_empty(document.register_form.state.value))
	{
		alert("You must enter your state or province.");
		document.register_form.state.focus();
		return false;
	}
	if(is_empty(document.register_form.zip.value))
	{
		alert("You must enter your zip or postal code.");
		document.register_form.zip.focus();
		return false;
	}
	if(document.register_form.country.value == "None")
	{
		alert("Please select a country.");
		document.register_form.country.focus();
		return false;
	}
	if(is_empty(document.register_form.phone.value))
	{
		alert("Please enter a phone number.");
		document.register_form.phone.focus();
		return false;
	}
	if (document.register_form.email.value == "")
	{
		alert("Please enter your e-mail address.");
    	document.register_form.email.focus();
    	return false;
  	}
	if (document.register_form.email.value.length < 6)
	{
		alert("Please enter at least 6 characters in the e-mail field.");
		document.register_form.email.focus();
		return false;
	}
  	if (!isEmailAddr(document.register_form.email.value))
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		document.register_form.email.focus();
		return false;
	}
	if (document.register_form.cemail.value == "")
	{
		alert("Please enter a value for the confirm e-mail field.");
    	document.register_form.cemail.focus();
    	return false;
  	}
	if (document.register_form.cemail.value.length < 6)
	{
		alert("Please enter at least 6 characters in the confirm e-mail field.");
		document.register_form.cemail.focus();
		return false;
	}
  	if (!isEmailAddr(document.register_form.cemail.value))
	{
		alert("Please enter a complete email address in the form: user@domain.com");
		document.register_form.cemail.focus();
		return false;
	}
	if (document.register_form.email.value != document.register_form.cemail.value)
	{
		alert("E-mail addresses do not match. Please re-enter them.");
		document.register_form.email.value = "";
		document.register_form.cemail.value = "";
		document.register_form.email.focus();
		return false;
	}
	try 
	{
	if (document.register_form.password.value == "")
	{
		alert("Please enter a password.");
    	document.register_form.password.focus();
    	return false;
  	}
	if (document.register_form.password.value.length < 4)
	{
		alert("Please enter at least 4 characters in the password field.");
		document.register_form.password.focus();
		return false;
	}
	if (document.register_form.cpassword.value == "")
	{
		alert("Please enter a value for the password confirmation field.");
    	document.register_form.cpassword.focus();
    	return false;
  	}
	if (document.register_form.cpassword.value.length < 4)
	{
		alert("Please enter at least 4 characters in the password confirmation field.");
		document.register_form.cpassword.focus();
		return false;
	}
	if (document.register_form.password.value != document.register_form.cpassword.value)
	{
		alert("Passwords do not match. Please re-enter them.");
		document.register_form.password.value = "";
		document.register_form.cpassword.value = "";
		document.register_form.password.focus();
		return false;
	}
	}
	catch (e) {}
	
	if(!GetRadioValue(document.register_form.media))
	{
		alert("Please answer question 3.");
		return false;
	}

	if(is_empty(document.register_form.products.value))
	{
		alert("Please answer question 5.");
		document.register_form.products.focus();
		return false;
	}
}

function checkPasswords()
{
	if (document.password_form.password.value == "")
	{
		alert("Please enter a password.");
    	document.password_form.password.focus();
    	return false;
  	}
	if (document.password_form.password.value.length < 4)
	{
		alert("Please enter at least 4 characters in the password field.");
		document.password_form.password.focus();
		return false;
	}
	if (document.password_form.cpassword.value == "")
	{
		alert("Please enter a value for the password confirmation field.");
    	document.password_form.cpassword.focus();
    	return false;
  	}
	if (document.password_form.cpassword.value.length < 4)
	{
		alert("Please enter at least 4 characters in the password confirmation field.");
		document.password_form.cpassword.focus();
		return false;
	}
	if (document.password_form.password.value != document.password_form.cpassword.value)
	{
		alert("Passwords do not match. Please re-enter them.");
		document.password_form.password.value = "";
		document.password_form.cpassword.value = "";
		document.password_form.password.focus();
		return false;
	}
}

function checkContactForm()
{
	if (document.contact_form.cname.value == "")
	{
		alert("Please enter your full name.");
    	document.contact_form.cname.focus();
    	return false;
  	}
	if (document.contact_form.cname.value.length < 8)
	{
		alert("Please enter your full name.");
    	document.contact_form.cname.focus();
    	return false;
  	}
	if (document.contact_form.cemail.value == "")
	{
		alert("Please enter your e-mail address.");
    	document.contact_form.cemail.focus();
    	return false;
  	}
	if (document.contact_form.cemail.value.length < 8)
	{
		alert("Please enter at least 8 characters in the e-mail address field.");
		document.contact_form.cemail.focus();
		return false;
	}
	if (!isEmailAddr(document.contact_form.cemail.value))
	{
		alert("Please enter a complete email address in the form: user@domain.com");
		document.contact_form.cemail.focus();
		return false;
	}	
	if (document.contact_form.comments.value == "")
	{
		alert("Please enter a comment.");
    	document.contact_form.comments.focus();
    	return false;
  	}
}

function checkIndex(thename)
{
	var idx = document.card_form.ctype.selectedIndex;
	switch (idx)
	{
		case 1 : {
			document.getElementById("title").innerHTML = "Insert Title:<span class=\"orangelargebold\">*</span>";
			document.getElementById("circ").innerHTML = "Counts:<span class=\"orangelargebold\">*</span>";
			document.getElementById("countexample").innerHTML = "Example: Monthly Package Inserts 10,000<BR>Monthly Product Fulfillment Inserts 5,000";
			document.getElementById("rateexample").innerHTML = "Example: Package Insert $100 /M, A5 Card Printing $25 /M";
			document.getElementById("auditedtitle").style.display = "none";
			document.getElementById("audited").style.display = "none";
			document.getElementById("min").style.display = "block";
			document.getElementById("orderqty").style.display = "block";
			break;
		}
		case 2 : {
			document.getElementById("title").innerHTML = "List Title:<span class=\"orangelargebold\">*</span>";
			document.getElementById("circ").innerHTML = "Counts:<span class=\"orangelargebold\">*</span>";
			document.getElementById("countexample").innerHTML = "Example: Universe 100,000; Monthly Hotline 10,000";
			document.getElementById("rateexample").innerHTML = "Example: Base $100 /M, Selection $10 /M, Email $50 /F";
			document.getElementById("auditedtitle").style.display = "none";
			document.getElementById("audited").style.display = "none";
			document.getElementById("min").style.display = "block";
			document.getElementById("orderqty").style.display = "block";
			break;
		}
		case 3 : {
			document.getElementById("title").innerHTML = "Magazine Title:<span class=\"orangelargebold\">*</span>";
			document.getElementById("circ").innerHTML = "Circulation:<span class=\"orangelargebold\">*</span>";
			document.getElementById("countexample").innerHTML = "Example: January 100,000; February 125,000";
			document.getElementById("rateexample").innerHTML = "Example: Front Inside $10,0000 Full Run<BR>1/2 Page Color $5,000 Full Run<BR> 1/2 Page B&W $4,000 Full Run";
			document.getElementById("auditedtitle").style.display = "block";
			document.getElementById("audited").style.display = "block";
			document.getElementById("min").style.display = "none";
			document.getElementById("orderqty").style.display = "none";
			break;
		}
		case 4 : {
			document.getElementById("title").innerHTML = "Newspaper Title:<span class=\"orangelargebold\">*</span>";
			document.getElementById("circ").innerHTML = "Circulation:<span class=\"orangelargebold\">*</span>";
			document.getElementById("countexample").innerHTML = "Example: Daily Edition 250,000; Weekend Edition 350,000";
			document.getElementById("rateexample").innerHTML = "Example: Front Inside $10,0000 Full Run<BR>1/2 Page Color $5,000 Full Run<BR> 1/2 Page B&W $4,000 Full Run";
			document.getElementById("auditedtitle").style.display = "block";
			document.getElementById("audited").style.display = "block";
			document.getElementById("min").style.display = "none";
			document.getElementById("orderqty").style.display = "none";
			break;
		}
	}
}

function setOpacity(testObj,opacity) 
{
	testObj.style.opacity = opacity/10;
	testObj.style.filter = 'alpha(opacity=' + opacity*10 + ')';
}

function DisableFormElements(myForm)
{
  objElems = myForm.elements;
  for(i=0;i<objElems.length;i++)
  {
  	if (objElems[i].name != 'ctype')
	{
    	objElems[i].disabled = true;
		setOpacity(objElems[i],1);
	}
  }
}

function EnableFormElements(myForm)
{
  objElems = myForm.elements;
  for(i=0;i<objElems.length;i++)
  {
  	if (objElems[i].name != 'ctype')
	{
    	objElems[i].disabled = false;
		setOpacity(objElems[i],10);
	}
  }
}

function checkCardForm()
{
	if(document.card_form.ctype.value == "none")
	{
		alert("Please select a card type.");
		document.card_form.ctype.focus();
		return false;
	}
	if(is_empty(document.card_form.title.value))
	{
		alert("You must enter a title.");
		document.card_form.title.focus();
		return false;
	}
	if(is_empty(document.card_form.elements["segment[]"][0].value))
	{
		var idx = document.card_form.ctype.selectedIndex;
		
		if (idx == 0 || idx == 1)
		{
			alert("You must enter a value for counts -> segment.");
		}
		else
		{
			alert("You must enter a value for circulation -> segment.");
		}
		document.card_form.elements["segment[]"][0].focus();
		return false;
	}
	if(is_empty(document.card_form.elements["qty[]"][0].value))
	{
		var idx = document.card_form.ctype.selectedIndex;
		
		if (idx == 0 || idx == 1)
		{
			alert("You must enter a value for counts -> qty.");
		}
		else
		{
			alert("You must enter a value for circulation -> qty.");
		}
		document.card_form.elements["qty[]"][0].focus();
		return false;
	}
	if(is_empty(document.card_form.desc.value))
	{
		alert("You must enter a description.");
		document.card_form.desc.focus();
		return false;
	}
	if(document.card_form.elements["mcountry[]"].selectedIndex == -1)
	{
		alert("You must specify market(s) covered.");
		document.card_form.elements["mcountry[]"][0].focus();
		return false;
	}
	if(is_empty(document.card_form.elements["description[]"][0].value))
	{
		alert("You must enter rate -> description details.");
		document.card_form.elements["description[]"][0].focus();
		return false;
	}
	if(is_empty(document.card_form.elements["cost[]"][0].value))
	{
		alert("You must enter rate -> cost details.");
		document.card_form.elements["cost[]"][0].focus();
		return false;
	}
	if(is_empty(document.card_form.elements["per[]"][0].value))
	{
		alert("You must enter rate -> per details.");
		document.card_form.elements["per[]"][0].focus();
		return false;
	}
	if(is_empty(document.card_form.discounts.value))
	{
		alert("You must enter commission or discount details.");
		document.card_form.discounts.focus();
		return false;
	}
	if(is_empty(document.card_form.currency.value))
	{
		alert("You must enter currency information.");
		document.card_form.currency.focus();
		return false;
	}
	if(is_empty(document.card_form.orderqty.value))
	{
		var idx = document.card_form.ctype.selectedIndex;
		
		if (idx == 0 || idx == 1)
		{
			alert("You must enter a value for minimum order quantity.");
			document.card_form.orderqty.focus();
			return false;
		}
	}
	if(is_empty(document.card_form.leadtime.value))
	{
		alert("Please enter lead time for delivery.");
		document.card_form.leadtime.focus();
		return false;
	}
}

function validate()
{
	if (document.form1.agree.checked)
	{
		document.form1.continueb.disabled = false;
	}
	else
	{
		document.form1.continueb.disabled = true;
	}
}

function nextPg()
{
	location.href="register_step2.php";
}
