// Script to contain common JS functions


function toggle_special_price(the_checkbox)
{
	var price = document.getElementById('special_price');
		
	if ( the_checkbox.checked )
	{
		price.style.display = 'block';
	}
	else
	{
		price.style.display = 'none';
	}
}


function view_larger(url, width, height)
{
	width += 16;

	height += 66;
	

	var left = (screen.availWidth - width) / 2;
	var top = (screen.availHeight - height) / 2;
	var args = 'left='+left+',top='+top+',height='+height+',width='+width+',resizable=yes,menubars=no,status=no,scrollbars=auto';

	window.open( url, '_blank', args );
	
}

/* adds product to session var on secure domain */
function add(secure_domain, id, option)
{
	var width=400;
	var height=180;
	var left = (screen.availWidth - width) / 2;
	var top = 300;
	//var url = 'http://'+secure_domain+'/add.php?id='+id;
	var url = 'https://' + secure_domain + '/add.php?id=' + id + '&option=' + option;
	var args = 'left='+left+',top='+top+',height='+height+',width='+width+',resizable=yes,menubars=no,status=no,scrollbars=auto';
	
	//alert(url);return (false);

	window.open( url, '_cart', args );
	
	return(false);
}

// Loop through field names in global array reqFieldNames and validate
function checkFields(theForm)
{
	for (var ind in reqFieldNames ) {
		var field = eval("theForm.elements['" + reqFieldNames[ind] + "']");
		//var field = document.getElementById(reqFieldNames[ind]);
		//alert(reqFieldNames[ind] + ' ' + field);
		if (field.type == "text") {
			if(isEmpty(field.value)) {
				alert("Please enter a value for: " + field.name);
				field.focus();
				return(false);
			}
			// check email
			if (field.name == 'Email') {
				if (!validEmail(field.value)) {
					alert('Please enter a valid email address.');
					field.focus();
					field.select();
					return(false);
				}
			}
		} else if (! field.type) {
			//alert(field.type);
			// Could be a radio
			if ((field.length > 0) && (field[0].type == 'radio')) {
				//alert("It's a radio array");
				if (!isRadioChecked(field)) {
					alert("Please select a option for: " + field[0].name);
					field[0].focus();
					return(false);
				}
			}
		}
		else if ( (field.type == "select") || (field.type == "select-one") ) {
			if (field.selectedIndex < 1) {
				alert("Please select a option for: " + field.name);
				field.focus();
				return(false);
			}
		}
		else if (field.type == "select-multiple") {
            for(i=1;i<field.length;i++) {
				if (field.options[i].selected == true) {
					return(true);
				}
			}
            alert("Please select option(s) for: " + field.name);
            field.focus();
            return(false);
        }
	}
	// we passed so submit the form
	return(true);
}


// Check to see if anything is left after whitespace has gone
function isEmpty(str)
{
    if(str.replace(/\s*/g,"") == "") {
        return(true)

    }

    return(false);

}

// checks to see if there is a radio element checked in group ele
function isRadioChecked(ele)
{
    for (i=0; i<ele.length; i++) {
        if (ele[i].checked) {
            return(true);
        }
    }
    return(false);

}

 
// checks for valid email
function validEmail(parsedEmail) {
	invalidChars       = " /:,;+= ";
	atPos                = parsedEmail.indexOf("@",1);
	periodPos          = parsedEmail.indexOf(".",atPos);

	if (parsedEmail == "") { return false; }

	for (i = 0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (parsedEmail.indexOf(badChar,0) > -1) {
			return false;
		}
	}

	if (atPos == -1) { return false; }

	if (parsedEmail.indexOf("@", atPos+1) > -1) { return false; }

	if (periodPos == -1) { return false; }

	if (periodPos+3 > parsedEmail.length) { return false; }

	return true;
}