
// Validation scripts

function isValidEmail(email)
{
	return (email.indexOf('@') < 1 || email.indexOf('.') < 1 || email.length < 6) ? showAlert('a', 'Mail is not correct') : true;
}

function isValidName(name)
{
	return (name.length < 1) ? showAlert('b', 'Name should not be blank') : true;
}

function isValidCode(code)
{
	return (code.length != 6 || Math.abs(code) + 0 != code) ? showAlert('c', 'Security Code is not correct') : true;
}

function isValidPhone(phone)
{
	if(phone.length < 7 || phone.length >20) {
		return showAlert('d', 'Phone number is not correct');
	}
	for(i = 0; i < phone.length; i++) {
		c = phone.charAt(i);
		if(!((c >= 0 && c <= 9) || c == '-' || c == '+' || c =='(' || c == ')' || c =='*' || c == '#' || c==' ' || c=='x')) {
			return showAlert('e', 'Phone number is not correct');
		}
	}
	return true;
}

function isValidLearnedFrom(select) 
{
	return (select == 0) ? showAlert('f', 'Please indicate how did you find us') : true;
}

function isValidCountry(select) 
{
	return (select == 0) ? showAlert('f', 'Please specify country') : true;
}

function isValidFullName(firstName, lastName) 
{
	return (firstName.length + lastName.length < 3) ? showAlert('g', 'The name entered does not seem real') : true;
}

function isValidAddress(address)
{
	return (address.length < 5) ? showAlert('h', 'Please fill correctly address field') : true;
}

function isValidCity(city)
{
	return (city.length < 2) ? showAlert('i', 'Please fill correctly city field') : true;
}

function isValidState(state)
{
	return (state.length > 2) ? showAlert('i', 'State field should contain 2 or less characters') : true;
}

function isValidZip(zip)
{
	return (zip.length < 3) ? showAlert('j', 'Please fill correctly zip field') : true;
}

function isValidCardNumber(cardNumber)
{
	return (cardNumber.length < 15) ? showAlert('k', 'Please specify correct credit card number') : true;
}

function isValidExpDate(month, year)
{
	today = new Date();
	return ((month < today.getMonth() + 1) && (year == today.getFullYear())) ? showAlert('l', 'Please specify correct Expiration Date') : true;
}

function isValidCsc(csc)
{
    return (csc.length != 3 && csc.length != 4) ? showAlert('m', 'Please specify correct Card Verification Number') : true;
}

function isValidSecurityCode(securityCode)
{
	return (security_code.length != 6) ? showAlert('n', 'Please specify correct Security Code') : true;
}


function validateForm(form) 
{

    if (form.length != 0) {
        for(var i = 0; i < form.length; i++) {
            var objct = form.elements[i];
            var value = objct.value;
            var r = true;
            switch (objct.name) {
                case "ui[firstName]":
                    r = isValidName(value);
                    break;
                case "ui[lastName]":
                    if ((r = isValidName(value)) && form.elements["ui[firstName]"] && form.elements["ui[lastName]"]) {
                        r = isValidFullName(form.elements["ui[firstName]"].value, form.elements["ui[lastName]"].value);
                    }
                    break;
                case "ui[phone]":
                    r = isValidPhone(value);
                    break;
                case "ui[email]":
                    r = isValidEmail(value);
                    break;
                case "ui[learnedFrom]":
                    r = isValidLearnedFrom(value);
                    break;
                case "ui[address]":
                    r = isValidAddress(value);
                    break;
                case "ui[city]":
                    r = isValidCity(value);
                    break;
                case "ui[state]":
                    r = isValidState(value);
                    break;
                case "ui[zip]":
                    r = isValidZip(value);
                    break;
                case "ui[country]":
                    r = isValidCountry(value);
                    break;
                case "pi[cardNumber]":
                    r = isValidCardNumber(value);
                    break;
                case "pi[csc]":
                    r = isValidCsc(value);
                    break;
                case "pi[expMonth]":
                case "pi[expYear]":
                    if (form.elements["pi[expMonth]"] && form.elements["pi[expYear]"]) {
                        r = isValidExpDate(form.elements["pi[expMonth]"].value, form.elements["pi[expYear]"].value);
                    }
                    break;
                case "securityCode":
                    r = isValidCode(value);
                    break;
            }
            if (!r) {
                objct.focus();
                return false;
            }
        }
    }
    return true;
}


function getScreenResolution()
{
	return screen.width + "x" + screen.height + "x" + screen.colorDepth
}

function getBrowserName()
{
	return navigator.appName;
}


function getLocalDate()
{
	d = new Date();
	return  d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + (d.getDate()) + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();// + "." + d.getMilliseconds();
}

function getWindowSize()
{
	if (parseInt(navigator.appVersion) > 3)
	{
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!= -1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	return winW + 'x' + winH;
}

function getWindowPosition()
{
	if (parseInt(navigator.appVersion) > 3) {
		if (navigator.appName=="Netscape") {
			winX = window.screenX;
			winY = window.screenY;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winX = document.body.screenLeft;
			winY = document.body.screenTop;
		}
	}
	return winX + 'x' + winY;
}

function getPluginsList() {
	plugins_list = '';
	for(i = 0; i < navigator.plugins.length ; ++i) {
		plugins_list += navigator.plugins.item(i).name + ';';
	}
	return plugins_list;
}

var startDate;
var startTime;
var trace;

function startParams() 
{
	d = new Date();
	startDate = getLocalDate();
	startTime = d.getTime();
	trace = '';
	
}

function showAlert(id, str)
{
	trace += id;
	alert(str);
	return false;
}

function endParams(formName)
{
    var form = document.forms[formName];
	d = new Date();
	form["tr[fillTickcount]"].value = d.getTime() - startTime;
	form["tr[screenResolution]"].value = getScreenResolution();
	form["tr[browser]"].value = getBrowserName();
	form["tr[javascriptAvailable]"].value = '1';
	form["tr[localDate]"].value = startDate;
	form["tr[localTickcount]"].value = startTime;
	form["tr[pluginsList]"].value = getPluginsList();
	form["tr[windowSize]"].value = getWindowSize();
	form["tr[windowPosition]"].value = getWindowPosition();
	form["tr[trace]"].value += trace;

	return true;
}

startParams();

