// Toggle Discount Recruiting Products;
function displayDiscounted(cID, iFlag, cValue) {
	// iFlag is deprecated;
	
	var nationwideSuperFreshContainer = document.getElementById('nationwideSuperFreshContainer');
	var nationwideFreshContainer = document.getElementById('nationwideFreshContainer');
	var nationwideDiscountedContainer = document.getElementById('nationwideDiscountedContainer');
	var nationwideAgedContainer = document.getElementById('nationwideAgedContainer');
		
	if( nationwideSuperFreshContainer ){
		nationwideSuperFreshContainer.style.display = "none";
	}
	if( nationwideFreshContainer ){
		nationwideFreshContainer.style.display = "none";
	}
	if( nationwideDiscountedContainer ){
		nationwideDiscountedContainer.style.display = "none";
	}
	if( nationwideAgedContainer ){
		nationwideAgedContainer.style.display = "none";
	}
	document.getElementById(cID).style.display = "block";
	checkDiscountPkg();
}

// Update Back Page;
function setByID(cID, cVal) {
	if (document.getElementById(cID)) {
		document.getElementById(cID).value = cVal;
	}
}

// Click Appropriate Product;
function checkDiscountProd(cID) {
	if (document.getElementById(cID)){
		document.getElementById(cID).click();
	}
}

// Click 1st Product Package;
function checkDiscountPkg() {
	// Find Radio Checked;
	var prodSelected = valueDiscountProd();
	if (prodSelected == 'SuperFresh' && document.getElementById('ProdGroup0')) {
		document.getElementById('ProdGroup0').checked = 'true';
		setByID('back_page', 'recruiting-monthly-super-fresh-surveyed-leads.php');
	} else if (prodSelected == 'Fresh' && document.getElementById('ProdGroup')) {
		document.getElementById('ProdGroup').checked = 'true';
		setByID('back_page', 'recruiting-monthly-fresh-surveyed-leads.php');
	} else if (prodSelected == 'Discounted' && document.getElementById('ProdGroup2')) {
		document.getElementById('ProdGroup2').checked = 'true';
		setByID('back_page', 'recruiting-monthly-discounted-surveyed-leads.php');
	} else if (prodSelected == 'Aged' && document.getElementById('ProdGroup3')) {
		document.getElementById('ProdGroup3').checked = 'true';
		setByID('back_page', 'recruiting-monthly-aged-surveyed-leads.php');
	}
}

// Find Value for Clicked Product
function valueDiscountProd() {
	if (document.productForm.discountprod[0]){
		for (var i=0; i<document.productForm.discountprod.length; i++)  {
			if (document.productForm.discountprod[i].checked) {
				var discountValue = document.productForm.discountprod[i].value;
			}
		}
	}else {
		var discountValue = document.productForm.discountprod.value;
	}
	return (discountValue);
}

//calculate 10% of selected package to set to daily cap from hidden form fields
function setCap() {
    var setCapProcess = false;
    if (document.body.ProdID != null && document.body.daily_cap != null)
    { // Standard Packages
        var setCapType = "ProdID";
        var setCapProcess = true;
    }
    if (document.body.PromoID != null && document.body.daily_cap != null)
    { // Promo Packages
        var setCapType = "PromoID";
        var setCapProcess = true;
    }
    if (setCapProcess == true)
    {
        for (i=0;i<eval("document.body."+setCapType+".length");i++)
        {
        	if (eval("document.body."+setCapType+"["+i+"].checked"))
        	{
        		document.body.daily_cap.value = Math.floor(eval("document.body.qty_" + eval("document.body."+setCapType+"["+i+"].value") + ".value") * .1);
        	}
        }
    }
}
function checkTZ() {
    var f = document["bodyLocal"]["timezone[]"];
    var x = 0;

    for(var i = 1; i < f.length; i++){
        if (f[i].checked) {
            x++;
        }
        if(f[i].checked){
            ischecked = true;
        }
    }
    if(x > 0){
        f[0].checked = false;
    }

    if (x >= f.length -1) {
        allTZ();
    }

}
function checkTZ2() {
    var f = document["body"]["timezone[]"];
    var x = 0;

    for(var i = 1; i < f.length; i++){
        if (f[i].checked) {
            x++;
        }
        if(f[i].checked){
            ischecked = true;
        }
    }
    if(x > 0){
        f[0].checked = false;
    }

    if (x >= f.length -1) {
        allTZ();
    }

}
function allTZ() {
    var f = document["body"]["timezone[]"];
    for(var i = 1; i < f.length; i++){
        f[i].checked = false;
    }
    f[0].checked = true;
}
function isTZ() {
    var f = document["body"]["timezone[]"];
    var ischecked = false;
    for(var i = 1; i < f.length; i++){
        if(f[i].checked){
            ischecked = true;
        }
    }
    return(ischecked)
}


function checkURLRedir(x){
    if (x == 'http://www.yourwebsite.com' || x == '') {
        document.body.cRURL.value = '';
        return false;
    } else if (x.indexOf("APIREDIRECT") != -1) {
    	return true;
    } else {
        var urlRegxp = /^http[s]{0,1}:\/\/[a-z|A-Z|0-9]*\.(\w)\.*/;
        return(urlRegxp.test(x));
    }
}

function goGetLeads(){
    var okay = true;
    var f = document.body;

	if (okay == true){
		if (document.body.PromoID) {
			var radioChecked = false;
			for (var i=0; i<document.body.PromoID.length; i++)  {
				if (document.body.PromoID[i].checked)  {
					radioChecked = true;
				}
			} 
			if (!radioChecked) {
				okay = false;
				alert("Please select a Lead Package to continue.");
			}
		}
	}

	if (okay == true){
        if(document.body.Opportunity1){
            var str = document.body.Opportunity2.value;
            str = str.replace(/^\s*|\s*$/g,"");
    		if (document.body.Opportunity1.value == 0 && str.length < 3){
    			alert("Please select your Primary Business Opportunity.")
    			okay = false;
    		}else{
    		    document.body.Primary_Opportunity1.value = document.body.Opportunity1.value;
    		    document.body.Primary_Opportunity2.value = document.body.Opportunity2.value;
    		}
    	}
	}

	if (f.iRURLOpt) {
    	if(okay === true && f.iRURLOpt[0].checked) {
        	f.cRURL.value = '';
    	}


	    if(f.cRURL.value == 'http://www.yourwebsite.com'){
	        f.cRURL.value = '';
	    }
	
	    //if redirect is selected, and there is a value for the redirect
	    if (okay == true && f.iRURLOpt[1].checked) {
	        okay = checkURLRedir(f.cRURL.value);
	        if (!okay) {
	            alert("You must enter a valid URL for Redirection ( http://www.yourwebsite.com )");
	            //f.cRURL.type="hidden";
	            //f.cRURL.value = 'http://www.yourwebsite.com';
	            f.cRURL.select();
	            f.cRURL.focus();
	        } else {
	            //select redirect option if radio is not selected
	            f.iRURLOpt[1].checked = true;
	        };
	    }
	} else if (f.cRURL) {
		if (okay && f.cRURL.value != '') {
			okay = checkURLRedir(f.cRURL.value);
			if (!okay) {
	            alert("You must enter a valid URL for Redirection ( http://www.yourwebsite.com )");
	            f.cRURL.select();
	            f.cRURL.focus();
	        }
		}
	}

    if (okay) {
        f.submit();
    }
}


function goGetMonthlyLeads(){
    var okay = true;
    var f = document.body;
    var aContainers = new Array('nationwideSuperFreshContainer','nationwideFreshContainer','nationwideDiscountedContainer','nationwideAgedContainer');
    
    if (okay == true){
		if (document.body.PromoID) {
			var radioChecked = false;
			var PromoIDLength = document.body.PromoID.length;
			if( !PromoIDLength ){
				PromoIDLength = 1;
			}
			if( PromoIDLength == 1 ){
				if (document.body.PromoID.checked)  {
					radioChecked = true;
				}				
			} else {
				for (var i=0; i<PromoIDLength; i++)  {
					if (document.body.PromoID[i].checked)  {
						radioChecked = true;
					}
				} 
			}
			if (!radioChecked) {
				okay = false;
				alert("Please select a Lead Package to continue.");
			}
		}
	}
    
	if (okay == true){
        if(document.body.Opportunity1){
            var str = document.body.Opportunity2.value;
            str = str.replace(/^\s*|\s*$/g,"");
    		if (document.body.Opportunity1.value == 0 && str.length < 3){
    			alert("Please select your Primary Business Opportunity.")
    			okay = false;
    		}else{
    		    document.body.Primary_Opportunity1.value = document.body.Opportunity1.value;
    		    document.body.Primary_Opportunity2.value = document.body.Opportunity2.value;
    		}
    	}
	}
	
	//var f2 = document.productForm;
	if (document.getElementById('discountprodSuperFresh') && document.getElementById('discountprodSuperFresh').checked == true) {
		if( document.getElementById('nationwideFreshContainer') ){
			document.getElementById('nationwideFreshContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideDiscountedContainer') ){
			document.getElementById('nationwideDiscountedContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideAgedContainer') ){
			document.getElementById('nationwideAgedContainer').innerHTML= '';
		}
	}
	if (document.getElementById('discountprodFresh') && document.getElementById('discountprodFresh').checked == true) {
		if( document.getElementById('nationwideSuperFreshContainer') ){
			document.getElementById('nationwideSuperFreshContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideDiscountedContainer') ){
			document.getElementById('nationwideDiscountedContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideAgedContainer') ){
			document.getElementById('nationwideAgedContainer').innerHTML= '';
		}
	}
	if (document.getElementById('discountprodDiscounted') && document.getElementById('discountprodDiscounted').checked == true) {
		if( document.getElementById('nationwideSuperFreshContainer') ){
			document.getElementById('nationwideSuperFreshContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideFreshContainer') ){
			document.getElementById('nationwideFreshContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideAgedContainer') ){
			document.getElementById('nationwideAgedContainer').innerHTML= '';
		}
	}
	if (document.getElementById('discountprodAged') && document.getElementById('discountprodAged').checked == true) {
		if( document.getElementById('nationwideSuperFreshContainer') ){
			document.getElementById('nationwideSuperFreshContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideDiscountedContainer') ){
			document.getElementById('nationwideDiscountedContainer').innerHTML= '';
		}
		if( document.getElementById('nationwideFreshContainer') ){
			document.getElementById('nationwideFreshContainer').innerHTML= '';
		}
	}
		
	/*for (x=0; x < f2.discountprod.length; x++) {
		
		if (f2.discountprod[x].checked != true ){
			var ID = f2.discountprod[x].id;
			var ID2= ID.replace('discountprod','');
			document.getElementById('nationwide'+ID2+'Container').innerHTML = '';
		}
	}
	*/
    if (okay) {
        f.submit();
    }
}

function goGetRedirectedLeads(){
    var okay = true;
    var f = document.body;
    
    if (okay == true){
		if (document.body.PromoID) {
			var radioChecked = false;
			for (var i=0; i<document.body.PromoID.length; i++)  {
				if (document.body.PromoID[i].checked)  {
					radioChecked = true;
				}
			} 
			if (!radioChecked) {
				okay = false;
				alert("Please select a Lead Package to continue.");
			}
		}
	}
	if (okay == true){
        if(document.body.Opportunity1){
            var str = document.body.Opportunity2.value;
            str = str.replace(/^\s*|\s*$/g,"");
    		if (document.body.Opportunity1.value == 0 && str.length < 3){
    			alert("Please select your Primary Business Opportunity.")
    			okay = false;
    		}else{
    		    document.body.Primary_Opportunity1.value = document.body.Opportunity1.value;
    		    document.body.Primary_Opportunity2.value = document.body.Opportunity2.value;
    		}
    	}
	}

    if(okay == true){
        if(f.cRURL.value == 'http://www.yourwebsite.com'){
            f.cRURL.value = '';
            okay = false;

        }

        if(f.cRURL.value > '') {
            okay = checkURLRedir(f.cRURL.value);
        }

        if (okay !== true) {
            alert("You must enter a valid URL for Redirection ( http://www.yourwebsite.com )");

                f.cRURL.value = 'http://www.yourwebsite.com';
                f.cRURL.select();
                f.cRURL.focus();
        }

    }

    if (okay) {
        f.submit();
    }
}

function goGetShortLeads(){
    var okay = true
    var f = document.body;
    var sched = f.sched_rb;

    if (okay == true){
        if(document.body.Opportunity1){
            var str = document.body.Opportunity2.value;
            str = str.replace(/^\s*|\s*$/g,"");
    		if (document.body.Opportunity1.value == 0 && str.length < 3){
    			alert("Please select your Primary Business Opportunity.")
    			okay = false;
    		}else{
    		    document.body.Primary_Opportunity1.value = document.body.Opportunity1.value;
    		    document.body.Primary_Opportunity2.value = document.body.Opportunity2.value;
    		}
    	}
	}

    if(f.cRURL.value == 'http://www.yourwebsite.com'){
        f.cRURL.value = '';
    }

	if (f.iRURLOpt) {
	    //if redirect is selected, and there is a value for the redirect
	    if (okay == true && f.iRURLOpt[1].checked) {
	        okay = checkURLRedir(f.cRURL.value);
	        if (!okay) {
	            alert("You must enter a valid URL for Redirection ( http://www.yourwebsite.com )");
	            //f.cRURL.type="hidden";
	            //f.cRURL.value = 'http://www.yourwebsite.com';
	            f.cRURL.select();
	            f.cRURL.focus();
	        } else {
	            //select redirect option if radio is not selected
	            f.iRURLOpt[1].checked = true;
	        };
	    }
	} else if (f.cRURL) {
		if (okay && f.cRURL.value != '') {
			okay = checkURLRedir(f.cRURL.value);
			if (!okay) {
	            alert("You must enter a valid URL for Redirection ( http://www.yourwebsite.com )");
	            f.cRURL.select();
	            f.cRURL.focus();
	        }
		}
	}

    if (okay) {
        f.submit();
    }
}

function enableDisable(x, y) {
    var f = x;

    for(var i = 0; i < f.length; i++){
        f[i].disabled = y;
    }

    if (x = document["bodyLocal"]["timezone[]"] && y == false) {
        checkTZ();
    }


}


function validateCompany(){
    var ok = 1;

	if (ok == true){
        if(document.body.Opportunity1){
            var str = document.body.Opportunity2.value;
            str = str.replace(/^\s*|\s*$/g,"");
    		if (document.body.Opportunity1.value == 0 && str.length < 3){
    			alert("Please select your Primary Business Opportunity.")

    			ok = false
    		}else{
    		    document.body.Primary_Opportunity1.value = document.body.Opportunity1.value;
    		    document.body.Primary_Opportunity2.value = document.body.Opportunity2.value;
    		}
    	}
	}

	return(ok);

}


function disableRadios(x) {
    if (x == 1) {
        enableDisable(document.bodyLocal.countries, false);
        enableDisable(document["bodyLocal"]["timezone[]"], false);
        clearSels(document["bodyLocal"]["states[]"]);
        clearSels(document["bodyLocal"]["areas[]"]);
        document["bodyLocal"]["states[]"].disabled = true;
        document["bodyLocal"]["areas[]"].disabled = true;
    } else if (x == 2) {
        enableDisable(document.bodyLocal.countries, true);
        enableDisable(document["bodyLocal"]["timezone[]"], true);
        clearSels(document["bodyLocal"]["areas[]"]);
        document["bodyLocal"]["states[]"].disabled = false;
        document["bodyLocal"]["areas[]"].disabled = true;
    } else if (x == 3) {
        enableDisable(document.bodyLocal.countries, true);
        enableDisable(document["bodyLocal"]["timezone[]"], true);
        clearSels(document["bodyLocal"]["states[]"]);
        document["bodyLocal"]["states[]"].disabled = true;
        document["bodyLocal"]["areas[]"].disabled = false;
    }

}
function clearSels(f){
    for(var i = 0; i < f.length; i++){
        f[i].selected = false;
    }
}

function getLeads() {
	var allValid = 1

	if (allValid == 1){
        var str = document.bodyLocal.Primary_Opportunity2.value;
        str = str.replace(/^\s*|\s*$/g,"");
		if (document.bodyLocal.Primary_Opportunity1.value == 0 && str.length < 3){
			alert("Please select your Primary Business Opportunity.")
			allValid = 0
		}
	}

    if (allValid == 1) {
        document.bodyLocal.action = "searching.php"
        document.bodyLocal.submit();
    }

}
