﻿function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function validate_required(field, alerttxt, typeoffield) {
	with (field) {
		value = value.replace(/-/, "");
		switch (typeoffield) {
			case 1:
				if (value == null || value == "") {
					alert(alerttxt);
					return false;
				}
				break;
			case 2:
				var patt = new RegExp(/[a-z,0-9]+[@]+[a-z]+["."]+[a-z]/gi);
				value = value.replace(/[ ]/gi, "");
				var result = patt.test(value);
				if (value == null || value.length == 0) {
					alert(alerttxt);
					return false;
				}
				if (result == false) {
					alert(alerttxt);
					return false;
				}
				break;
			case 3:
				var patt = new RegExp(/\D/gi);
				value = value.replace(/[-]/gi, "");
				value = value.replace(/[)]/gi, "");
				value = value.replace(/[(]/gi, "");
				value = value.replace(/[ ]/gi, "");
				value = value.replace(/[a-z]/gi, "");
				var result = patt.test(value);
				if (value == null || value.length < 10 || value.length > 11 || value == "1111111111" || value == "11111111111" || value == "2222222222" || value == "22222222222" || value == "3333333333" || value == "33333333333" || value == "4444444444" || value == "44444444444" || value == "5555555555" || value == "55555555555" || value == "6666666666" || value == "66666666666" || value == "7777777777" || value == "77777777777" || value == "8888888888" || value == "88888888888" || value == "9999999999" || value == "99999999999" || value == "0000000000" || value == "00000000000") {
					if (value.length == 10)
						value = value.substr(0, 3) + "-" + value.substr(3, 3) + "-" + value.substr(6);
					else if (value.length == 11)
						value = value.substr(0, 1) + "-" + value.substr(1, 3) + "-" + value.substr(4, 3) + "-" + value.substr(7);
					else
						value = "";
					alert(alerttxt);
					return false;
				}
				if (result == true) {
					if (value.length == 10)
						value = value.substr(0, 3) + "-" + value.substr(3, 3) + "-" + value.substr(6);
					else if (value.length == 11)
						value = value.substr(0, 1) + "-" + value.substr(1, 3) + "-" + value.substr(4, 3) + "-" + value.substr(7);
					else
						value = "";
					alert(alerttxt);
					return false;
				}
				if (value.length == 10)
					value = value.substr(0, 3) + "-" + value.substr(3, 3) + "-" + value.substr(6);
				else if (value.length == 11)
					value = value.substr(0, 1) + "-" + value.substr(1, 3) + "-" + value.substr(4, 3) + "-" + value.substr(7);
				else
					value = "";
				break;
			default:
				alert("type " + typeoffield + " not found");
				return false;
		}
	}
}
function validate(thisform) {
	with (thisform) {
		if (validate_required(Contact0FirstName, "First Name must be filled out!", 1) == false) {
			Contact0FirstName.focus();
			return false;
		}
		if (validate_required(Contact0LastName, "Last Name must be filled out!", 1) == false) {
			Contact0LastName.focus();
			return false;
		}
		if (validate_required(Contact0Phone1, "Please Enter A Valid US Phone Number!\n\t\t(i.e. 000-000-0000)", 3) == false) {
			Contact0Phone1.focus();
			return false;
		}
		if (validate_required(Contact0Email, "Please Enter A Valid Email Address!", 2) == false) {
			Contact0Email.focus();
			return false;
		}
		if (Contact0_AMOptInAgreement.checked == false) {
			Contact0_AMOptInAgreement.focus();
			alert("You Must Agree To The Privacy Policy.");
			goodlink = false;
			return false;
		}
	}
	return true;
}

function validate_form(thisform) {
	if (!validate(thisform))
		return false;
	
	with (thisform) {
	    createCookie('email', Contact0Email.value, 90);
        createCookie('affid', affiliateID.value ,90);
        createCookie('subid', affiliateSubID.value, 90);

		var Lead = {};
		Lead.FirstName = $("#Contact0FirstName").val();
		Lead.LastName = $("#Contact0LastName").val();
		Lead.Email = $("#Contact0Email").val();
		Lead.Phone = $("#Contact0Phone1").val();
		Lead.Address = 'N/A';
		Lead.City = 'N/A';
		Lead.State = 'N/A';
		Lead.Zip = 'N/A';
		Lead.DateAdded = '1/1/1900';
		Lead.IPAddress = $("#ipAddress").val();
		Lead.LeadType = 'PA';
		Lead.CampaignID = $("#campaignID").val();
		Lead.AffiliateID = $("#affiliateID").val();
		Lead.AffiliateSubID = $("#affiliateSubID").val();
		
		var DTO = { 'lead': Lead };

		$.ajax({
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify(DTO),
			dataType: "json",
			type: "POST",
			url: "http://www.teleprofitsnow.com/WebServices/LeadAPI.asmx/AddLead",
			error: function(xhr, desc, exceptionobj) {
				$('#results').html(xhr.responseText);
			},
////			success: function(msg) {
////				alert(msg.d);
////			}
		});
	}
	goodlink=true;	
	 return true;
}
function MM_openBrWindow(theURL, winName, features) { //v2.0
	window.open(theURL, winName, features);
}

function padNumber(nbr, places) {
	var padded = "00000" + nbr;

	padded = padded.split('').reverse().join('');
	padded = padded.substr(0, places);
	padded = padded.split('').reverse().join('');

	return padded;
}

function sampleData(myForm) {
	var today_date = new Date()
	var month = today_date.getMonth() + 1
	var today = today_date.getDate()
	var h = today_date.getHours()
	var m = today_date.getMinutes()
	var s = today_date.getSeconds()
	var uniq = "";

	myForm.Contact0FirstName.value = "System";
	myForm.Contact0LastName.value = "Check";
	uniq = padNumber(h, 2) + padNumber(m, 2) + padNumber(s, 2);
	myForm.Contact0Email.value = "Tst" + month + today + uniq + "@systemcheck.com";
	myForm.Contact0Phone1.value = "8005" + uniq;
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0, pos).toLowerCase();
		var value = pairs[i].substring(pos + 1);
		args[argname] = unescape(value);
	}
	return args;
}

function checkDevMode() {
	var retVal = false;
	if (args.test)
		if (args.test == 'sample')
		retVal = true;

	return retVal;
}

var args = getArgs();

if (checkDevMode()) {
	sampleData(document.forms[0]);
}
