var Hoffset="9px"
var Voffset="0"
var ie=document.all
var ns6=document.getElementById&&!document.all

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function AddWidgetFormCheck()
{
	if (document.thisform.formmodule.value == "new")
	{
		var errors = "";
		if (document.thisform.pname0.value=="" || document.thisform.pname0.value==null) 
		{
	    	errors = errors + "- widget ID must be entered\n";
		}
		if (document.thisform.creativeid0.value=="" || document.thisform.creativeid0.value==null) 
		{
			errors = errors + "- widget size must be selected\n";
		}
		if (document.thisform.widgetcode0.value=="" || document.thisform.widgetcode0.value==null) 
		{
			errors = errors + "- widget code must be entered\n";
		}
		if (errors!="")
		{
			alert (errors);
			return false;
		}
	}	
	return true;
}


function response(option,url,message) 
{
	if (option == "logout")
	{
		if (confirm(message))
		{
			window.location=url;
		}
	}	
	if (option == "deleteoffer")
	{
		if (confirm(message))
		{
			window.location=url;
		}
	}	
	return false;
}

function ReportFormCheck()
{
	if (document.thisform.product.value=="" || document.thisform.product.value==null) 
	{
    	alert("please select Product!");
        document.thisform.product.focus();
        return false;
	}
	
}

function PrintReport()
{
	//window.parent.divReport.focus();
	document.getElementById('divReport').focus();
	window.print();	
}

function radioupdate(radiobutton)
{
	var radiodaterange = document.getElementById('radiodaterange');
	var radiodateranges = document.getElementById('radiodateranges');
	
	f = thisform.radiodaterange; 
	
	if (radiobutton == "radiodateranges")
	{
		if (f.length>0)
		{
			for (var i=0; i < f.length; i++) 
			{	
				f[i].checked = false;
			}
		}
		radiodateranges.checked=true;	
	}
	if (radiobutton == "radiodaterange")
	{
		radiodateranges.checked=false;
	}
}	

function UploadFormCheck(option)
{
	retval = false;
	fval = document.thisform.filename.selectedIndex;
	if (option == "view")
	{
		randnum = Math.round(Math.random()*100000);			
		filename = "http://www.partnerspilot.com/" + document.thisform.filename[fval].value + "?" + randnum;
		doWindow(filename);
	}	
	if (option == "delete")
	{
		fval = "Sure to Delete: " + document.thisform.filename[fval].value;
		retval = confirm(fval);
	}
	return retval;
}

function doWindow(url)
{
	NewProdWindow=window.open(url,'NewProdWindow','location=yes,toolbar=yes,scrollbars=yes,resizable=yes,status=yes,width=770,height=450')
	NewProdWindow.focus()
}


function cpAddFormCheck()
{
	var errors = "";
	if (document.thisform.code.value=="") 
	{
    	errors = errors + "- client code missing\n";
    }
	if (document.thisform.password.value=="") 
	{
		errors = errors + "- password must be entered\n";
    }
    else
    {
		if (document.thisform.password.value.length<6) 
		{
			errors = errors + "- password must be at least 6 characters/digits long\n";
	    }
	}    
	if (document.thisform.firstname.value=="") 
	{
    	errors = errors + "- first name missing\n";
    }
	if (document.thisform.lastname.value=="") 
	{
    	errors = errors + "- last name missing\n";
    }
	if (document.thisform.company.value=="") 
	{
    	errors = errors + "- company name missing\n";
    }
	if (document.thisform.phone.value=="") 
	{
    	errors = errors + "- phone missing\n";
    }
    else
    {
		if (ValidateUSNumber(document.thisform.phone.value)==false)
		{
			errors = errors + "- invalid phone\n";
		}
    }
	if (document.thisform.email.value=="") 
	{
		errors = errors + "- email address missing\n";
    }
	if (document.thisform.email.value!="" && email_check(document.thisform.email.value)==false)
	{
		errors = errors + "- invalid email address\n";
	}

	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;

}

function cpUpdateFormCheck()
{
	var errors = "";
	if (document.thisformcplookup.password.value=="") 
	{
		errors = errors + "- password must be entered\n";
    }
    else
    {
		if (document.thisformcplookup.password.value.length<6) 
		{
			errors = errors + "- password must be at least 6 characters/digits long\n";
	    }
	}    
	if (document.thisformcplookup.firstname.value=="") 
	{
    	errors = errors + "- first name missing\n";
    }
	if (document.thisformcplookup.lastname.value=="") 
	{
    	errors = errors + "- last name missing\n";
    }
	if (document.thisformcplookup.company.value=="") 
	{
    	errors = errors + "- company name missing\n";
    }
	if (document.thisformcplookup.phone.value=="") 
	{
    	errors = errors + "- phone missing\n";
    }
    else
    {
		if (ValidateUSNumber(document.thisformcplookup.phone.value)==false)
		{
			errors = errors + "- invalid phone\n";
		}
    }
	if (document.thisformcplookup.email.value=="") 
	{
		errors = errors + "- email address missing\n";
    }
	if (document.thisformcplookup.email.value!="" && email_check(document.thisformcplookup.email.value)==false)
	{
		errors = errors + "- invalid email address\n";
	}

	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;

}

function ManualAddFormCheck()
{
	var errors = "";
	if (document.thisform.email.value=="") 
	{
		errors = errors + "- email address missing\n";
    }
	if (document.thisform.email.value!="" && email_check(document.thisform.email.value)==false)
	{
		errors = errors + "- invalid email address\n";
	}
	if (document.thisform.password.value=="") 
	{
		errors = errors + "- password must be entered\n";
    }
    else
    {
		if (document.thisform.password.value.length<6) 
		{
			errors = errors + "- password must be at least 6 characters/digits long\n";
	    }
	}    
	if (document.thisform.sitename.value=="") 
	{
    	errors = errors + "- web site name missing\n";
    }
	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}


function AccountFormCheck()
{
	var errors = "";
	if (document.thisform.firstname.value=="") 
	{
    	errors = errors + "- first name missing\n";
    }
	if (document.thisform.lastname.value=="") 
	{
    	errors = errors + "- last name missing\n";
    }
	if (document.thisform.address1.value=="") 
	{
    	errors = errors + "- address missing\n";
    }
	if (document.thisform.city.value=="") 
	{
    	errors = errors + "- city missing\n";
    }
	if (document.thisform.state.value=="") 
	{
    	errors = errors + "- state missing\n";
    }
	if (document.thisform.zip.value=="") 
	{
    	errors = errors + "- zip missing\n";
    }
    else
    {
		if (validateZIP(document.thisform.zip.value)==false)
		{
			errors = errors + "- invalid zip\n";
		}
    }
	if (document.thisform.phone.value=="") 
	{
    	errors = errors + "- phone missing\n";
    }
    else
    {
		if (ValidateUSNumber(document.thisform.phone.value)==false)
		{
			errors = errors + "- invalid phone\n";
		}
    }
	if (document.thisform.fax.value!="" && ValidateUSNumber(document.thisform.fax.value)==false)
	{
		errors = errors + "- invalid fax\n";
	}
	if (document.thisform.checkto.value=="") 
	{
    	errors = errors + "- checks made out to missing\n";
    }
	if (document.thisform.how.value=="") 
	{
    	errors = errors + "- how did you find out about our affiliation program missing\n";
    }
	if (document.thisform.howdesc.value!="" && document.thisform.howdesc.value.length>1000) 
	{
		errors = errors + "- description for how did you find about us must not be more than 1000 characters\n";
    }
	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}

function TaxFormCheck()
{
	var errors = "";
	if (document.thisform.taxid.value=="") 
	{
    	errors = errors + "- SSN or federal tax id missing\n";
    }
	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}

function EmailChangeFormCheck()
{
	if (document.thisform.password.value=="") 
	{
    	alert("please enter your current password!");
        document.thisform.password.focus();
        return false;
    }
	if (document.thisform.newemail.value=="") 
	{
    	alert("please enter new email address!");
        document.thisform.newemail.focus();
        return false;
    }
	if (document.thisform.newemail.value!="" && email_check(document.thisform.newemail.value)==false)
	{
    	alert("invalid new email address!");
        document.thisform.newemail.focus();
        return false;
	}
	if (document.thisform.vnewemail.value=="") 
	{
    	alert("please enter verify email address!");
        document.thisform.vnewemail.focus();
        return false;
    }
    if (document.thisform.newemail.value!=document.thisform.vnewemail.value) 
    {
    	alert("new email address and verify new email address do not match!");
        document.thisform.newemail.focus();
        return false;
    }
}

function ForgotFormCheck()
{
	if (document.thisform.username.value=="" && document.thisform.email.value=="") 
	{
    	alert("please enter your partner id OR your email address!");
        document.thisform.username.focus();
        return false;
    }
	if (document.thisform.username.value!="" && document.thisform.email.value!="") 
	{
    	alert("please enter either your partner id OR your email address, not both!");
        document.thisform.username.focus();
        return false;
    }
	if (document.thisform.email.value!="" && email_check(document.thisform.email.value)==false)
	{
    	alert("invalid email address!");
        document.thisform.email.focus();
        return false;
	}
    
}

function PasswordFormCheck()
{
	if (document.thisform.password.value=="") 
	{
    	alert("please enter your current password!");
        document.thisform.password.focus();
        return false;
    }
	if (document.thisform.newpassword.value=="") 
	{
    	alert("please enter new password!");
        document.thisform.password.focus();
        return false;
    }
	if (document.thisform.newpassword.value.length<6) 
	{
    	alert("new password must be at least 6 characters/digits long!");
        document.thisform.newpassword.focus();
        return false;
    }
    if (document.thisform.password.value==document.thisform.newpassword.value) 
    {
    	alert("new password cannot be same as the current password!");
        document.thisform.newpassword.focus();
        return false;
    }
	if (document.thisform.vnewpassword.value=="") 
	{
    	alert("please enter verify password!");
        document.thisform.vnewpassword.focus();
        return false;
    }
    if (document.thisform.newpassword.value!=document.thisform.vnewpassword.value) 
    {
    	alert("new password and verify new password do not match!");
        document.thisform.newpassword.focus();
        return false;
    }
}

function LoginFormCheck()
{
	if (document.thisform.username.value=="" || document.thisform.username.value==null) 
	{
    	alert("please enter username!");
        document.thisform.username.focus();
        return false;
    }
	if (document.thisform.password.value=="" || document.thisform.password.value==null) 
	{
    	alert("please enter password!");
        document.thisform.password.focus();
        return false;
    }
}

function cpRegisterFormCheck()
{
	var errors = "";
	if (document.thisform.firstname.value=="") 
	{
    	errors = errors + "- first name missing\n";
    }
	if (document.thisform.lastname.value=="") 
	{
    	errors = errors + "- last name missing\n";
    }
	if (document.thisform.company.value=="") 
	{
    	errors = errors + "- company name missing\n";
    }
	if (document.thisform.phone.value=="") 
	{
    	errors = errors + "- phone missing\n";
    }
    else
    {
		if (ValidateUSNumber(document.thisform.phone.value)==false)
		{
			errors = errors + "- invalid phone\n";
		}
    }
	if (document.thisform.email.value=="") 
	{
		errors = errors + "- email address missing\n";
    }
	if (document.thisform.email.value!="" && email_check(document.thisform.email.value)==false)
	{
		errors = errors + "- invalid email address\n";
	}

	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}

function JoinFormCheck()
{
	var errors = "";
	if (document.thisform.email.value=="") 
	{
		errors = errors + "- email address missing\n";
    }
	if (document.thisform.email.value!="" && email_check(document.thisform.email.value)==false)
	{
		errors = errors + "- invalid email address\n";
	}
	if (document.thisform.vemail.value=="") 
	{
    	errors = errors + "- verify email address missing\n";
    }
    if (document.thisform.email.value!=document.thisform.vemail.value) 
    {
	    errors = errors + "- email address and verify email address do not match\n";
    }
	if (document.thisform.firstname.value=="") 
	{
    	errors = errors + "- first name missing\n";
    }
	if (document.thisform.lastname.value=="") 
	{
    	errors = errors + "- last name missing\n";
    }
	if (document.thisform.address1.value=="") 
	{
    	errors = errors + "- address missing\n";
    }
	if (document.thisform.city.value=="") 
	{
    	errors = errors + "- city missing\n";
    }
	if (document.thisform.state.value=="") 
	{
    	errors = errors + "- state missing\n";
    }
	if (document.thisform.zip.value=="") 
	{
    	errors = errors + "- zip missing\n";
    }
    else
    {
		if (validateZIP(document.thisform.zip.value)==false)
		{
			errors = errors + "- invalid zip\n";
		}
    }
	if (document.thisform.phone.value=="") 
	{
    	errors = errors + "- phone missing\n";
    }
    else
    {
		if (ValidateUSNumber(document.thisform.phone.value)==false)
		{
			errors = errors + "- invalid phone\n";
		}
    }
	if (document.thisform.fax.value!="" && ValidateUSNumber(document.thisform.fax.value)==false)
	{
		errors = errors + "- invalid fax\n";
	}
	if (document.thisform.checkto.value=="") 
	{
    	errors = errors + "- checks made out to missing\n";
    }
	if (document.thisform.taxid.value=="") 
	{
    	errors = errors + "- SSN or federal tax id missing\n";
    }
	if (document.thisform.sitename.value=="") 
	{
    	errors = errors + "- web site name missing\n";
    }
	if (document.thisform.sitedesc.value=="") 
	{
    	errors = errors + "- web site description missing\n";
    }
    else
    {
		if (document.thisform.sitedesc.value.length>1000)     	
		{
			errors = errors + "- web site description must not be more than 1000 characters\n";
		}
    }
	if (document.thisform.sitevisitors.value=="") 
	{
    	errors = errors + "- monthly unique web site visitors missing\n";
    }
	if (document.thisform.sitedate.value=="") 
	{
    	errors = errors + "- date web site established missing\n";
    }
	if (document.thisform.how.value=="") 
	{
    	errors = errors + "- how did you find out about our affiliation program missing\n";
    }
	if (document.thisform.howdesc.value!="" && document.thisform.howdesc.value.length>1000) 
	{
		errors = errors + "- description for how did you find about us must not be more than 1000 characters\n";
    }
	if (document.thisform.securitycode.value=="") 
	{
    	errors = errors + "- security code missing\n";
    }
	if(checkEmpty(checkradiovalue(document.thisform.agree))) 
	{
		errors = errors + "- You must AGREE to our Terms and Conditions\n";
	}
	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}

function email_check(string) 
{
  	var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  	return addressPattern.test(string);
}

function ValidateUSNumber(phonenumber) 
{ 
	var CleanedString=""; 
 	var index = 0; 
 	var LimitCheck; 
 	LimitCheck = phonenumber.length;
 	while (index != LimitCheck) 
 	{ 
  		if (isNaN(parseInt(phonenumber.charAt(index)))) 
	  	{ } 
	  	else 
	  	{ 
	  		CleanedString = CleanedString + phonenumber.charAt(index); 
	  	} 
	  	index = index + 1; 
 	}
	if (CleanedString.length == 10) 
	{ 
	 	phonenumber = "(" + CleanedString.substring(0,3) + ") " + CleanedString.substring(3,6) + "-" + CleanedString.substring(6,10); 
	}
	else 
	{ 
  		CleanedString = phonenumber; 
 		return false
 	} 
} 

function validateZIP(zipcode) 
{
	var valid = "0123456789";
	if (zipcode.length!=5) 
	{
		return false;
	}
	for (var i=0; i < zipcode.length; i++) 
	{
		temp = "" + zipcode.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			return false;
		}
	}
	return true;
}

function checkEmpty(strng) 
{
	if (strng == null || strng == "") 
	{
		return true;
	}
	for (i=0;i<strng.length;i++) 
	{
		if (strng.charAt(i) != ' ')
		return false;
	}
	return true;
}

function checkradiovalue(f)
{
	if (f.length>0)
	{
		for (var i=0; i < f.length; i++)
		{
			if (f[i].checked)
			{
				return rad_val = f[i].value;
			}
		}
	}
	else
	{
		if (f.checked)
		{
			return rad_val = f.value;
		}
	}    
	return null;
}
function checkEmpty(strng) 
{
	if (strng == null || strng == "") 
	{
		return true;
	}
	for (i=0;i<strng.length;i++) 
	{
		if (strng.charAt(i) != ' ')
		return false;
	}
	return true;
}

function SYS_trackObject(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=SYS_trackObject(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;}

function ie_compatible(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function postionoffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function noedge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(Hoffset)*-1 : parseInt(Voffset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? ie_compatible().scrollLeft+ie_compatible().clientWidth-30 : window.pageXOffset+window.innerWidth-40
helpobj.contentmeasure=helpobj.offsetWidth
if (windowedge-helpobj.x < helpobj.contentmeasure)
edgeoffset=helpobj.contentmeasure+obj.offsetWidth+parseInt(Hoffset)
}
else{
var windowedge=ie && !window.opera? ie_compatible().scrollTop+ie_compatible().clientHeight-15 : window.pageYOffset+window.innerHeight-18
helpobj.contentmeasure=helpobj.offsetHeight
if (windowedge-helpobj.y < helpobj.contentmeasure)
edgeoffset=helpobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhelp(menucontents, obj, e, tipwidth){
if ((ie||ns6) && document.getElementById("helpbox")){
helpobj=document.getElementById("helpbox")
helpobj.innerHTML=menucontents
helpobj.style.left=helpobj.style.top=-500
if (tipwidth!=""){
helpobj.widthobj=helpobj.style
helpobj.widthobj.width=tipwidth
}
helpobj.x=postionoffset(obj, "left")
helpobj.y=postionoffset(obj, "top")
helpobj.style.left=helpobj.x-noedge(obj, "rightedge")+obj.offsetWidth+"px"
helpobj.style.top=helpobj.y-noedge(obj, "bottomedge")+"px"
helpobj.style.visibility="visible"
obj.onmouseout=hidehelp
}
}

function hidehelp(e){
helpobj.style.visibility="hidden"
helpobj.style.left="-500px"
}

function createhelpbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "helpbox")
document.body.appendChild(divblock)
}

if (window.addEventListener)
window.addEventListener("load", createhelpbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhelpbox)
else if (document.getElementById)
window.onload=createhelpbox


	function EditOfferFormCheck()
	{
		formerror = "";
		formmasterio = document.thisform.masterio.value;
		if (formmasterio=="")
		{
			formerror = formerror+"\"offerID\" must be entered\n";
		}
		else
		{
			testmasterio = formmasterio.toLowerCase();
			if (testmasterio == "new")
			{
				formerror = formerror+"\"offerID\" is invalid\n";
			}
		}
		formtitle = document.thisform.title.value;
		if (formtitle=="")
		{
			formerror = formerror+"\"title\" must be entered\n";
		}
		//formpriority = document.thisform.priority.value;
		//if (formpriority=="")
		//{
		//	formerror = formerror+"\"priority\" must be entered\n";
		//}
		formurltype = document.thisform.urltype.value;
		formurl = document.thisform.url.value;
		if (formurltype == "custom" && formurl=="")
		{
			formerror = formerror+"\"URL\" must be entered\n";
		}
		if (formurl!="")
		{
			if (!isURL(formurl)) 
			{
				formerror = formerror+"\"URL\" is invalid\n";
			}
		}	
		if (formerror != "")
		{
	    	alert(formerror);
	        return false;
		}
	}

function isURL(s) {
  	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function AddCreativeFormCheck()
{
	if (document.thisform.formpname.value == "new")
	{
		if (document.thisform.creativename0.value=="" || document.thisform.creativename0.value==null) 
		{
	    	alert("please select creative filename");
	        document.thisform.creativename0.focus();
	        return false;
		}
	}
}

function insertTab(event,obj) {
    var tabKeyCode = 9;
    if (event.which) // mozilla
        var keycode = event.which;
    else // ie
        var keycode = event.keyCode;
    if (keycode == tabKeyCode) {
        if (event.type == "keydown") {
            if (obj.setSelectionRange) {
                // mozilla
                var s = obj.selectionStart;
                var e = obj.selectionEnd;
                obj.value = obj.value.substring(0, s) + 
                    "\t" + obj.value.substr(e);
                obj.setSelectionRange(s + 1, s + 1);
                obj.focus();
            } else if (obj.createTextRange) {
                // ie
                document.selection.createRange().text="\t"
                obj.onblur = function() { this.focus(); this.onblur = null; };
            } else {
                // unsupported browsers
            }
        }
        if (event.returnValue) // ie ?
            event.returnValue = false;
        if (event.preventDefault) // dom
            event.preventDefault();
        return false; // should work in all browsers
    }
    return true;
}
