function serchCheck() {
	var term = trim10($('search_term').value);
	if (term.length < 3) {
		alert("Search term should contain at least 3 symbols")
		return false;
	}
	return true;
}
function checkChecked() {
	var c = 0;
	$$('input.compare_checkbox').each(function(n) {
		if(n.checked) {			
			var el = new Element('input', {
                         type: "hidden",
                         name: n.readAttribute('name'),
                         value: 'on'
                       });
			$('compare_form').appendChild(el);
			c++;			
		}
	});
	if (c > 1)
		$('compare_form').submit();
	else 
		alert("Please select at least 2 items to comapre");	
}
function popitup(url, width, height) {
	newwindow=window.open(url,'name','height='+height+',width='+width+'');
	//if (window.focus) {newwindow.focus()}
	return false;
}

Event.observe(window, 'load',
	function() {  
		$$('#tabs_bg ul li a').each(function(n) {
			n.observe('click', DeactivateTabs);
		});
});

function DeactivateTabs(e) {
	var element = e.element();
	$$('li.tab_slctd').each(function(n) {
		n.className = 'tab_nslctd';		
	});
	element.up().className = 'tab_slctd';	
	
	new Ajax.Request( request_url, {
		method: 'get',
	    parameters: {
	        id: current_id,
	        get: element.up().id
	    },
	        onSuccess: function( transport ){
	            var response = transport.responseText;
	            $('plan_features_content').update(response);
	            
	        },
	    	onFailure: function( transport ){
	            //alert('Epic fail!');
	        }
	 });
	//
	e.stop();
}

function trim10 (str) {
	var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}


function setSameData() {
	if ($('same_as_shipping').checked) {
		for ( var i in shipping_data ) {
			if($(i)) {
		    	$(i).value = shipping_data[i];
			} 
		} 
	} else {
		$('bill_form').reset();
	}
}

function validateDate(d){
	var reg = /(01|02|03|04|05|06|07|08|09|10|11|12)\/(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\/(2005|2006|2007|2008|2009|2010)/;
	return reg.test(d);
}


function clearErrors() {
	var err = $("errorBlock");
	err.style.display = "none";
	while (err.hasChildNodes()) {
		err.removeChild( err.firstChild );
	}
}

function regError(msg) {
	var con=document.createElement('p');
	con.appendChild(document.createTextNode( msg ));

	$("errorBlock").appendChild( con );
}

function validateEmail(e) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])/;
	return reg.test(e);
}

//step 2 validation
function validateStep2(){
	var f = $('shipping_form');
		var flag=true;
		clearErrors();
		if(!_vClientName()) flag=false;
		if(!_vClientAddress()) flag=false;
		if(!_vClientCity()) flag=false;
		if(!_vClientZip()) flag=false;
		if(!_vClientCountry()) flag=false;
		if(!_vClientPhone()) flag=false;
		if(!_vClientEmail()) flag=false;
		if(!_vTypeAddr()) flag=false;		
		if(!_vShippingRates()) flag=false;
		
		if (!flag) {			
			$("errorBlock").style.display = "";
			window.location = "/products/store/checkout/shipping#error";
		}
		return flag;	

	function _vClientName(){
		if($F('firstName')=="" || $F('lastName')=="") {
			regError('Please provide your name.');
			return false;
		}
		return true;
	}
	
	function _vClientPhone(){
		if($F('phone')=="") {
			regError('Please provide your phone number.');
			return false;
		}
		return true;
	}
	
	function _vClientEmail(){
		var e1=$F('email');
		if(e1==""){
			regError('Please provide your email address.');
			return false;
		}
		else if(!validateEmail(e1)) {
			regError('Invalid email format.');
			return false;
		}
		return true;
	}

	function _vClientAddress(){
		if($F('address_one')=="") {
			regError('Please provide your address.');
			return false;
		}
		return true;
	}

	function _vClientCity(){
		if($F('city')=="") {
			regError('Please provide your city.');
			return false;
		}
		return true;
	}

	function _vClientZip(){
		if($F('zip')=="") {
			regError('Please provide your Zip code.');
			return false;
		}
		return true;
	}

	function _vClientCountry() {		
		if($F('country')=="") {
			regError('Please provide your country.');
			return false;
		}		
		return true;
	}

	function _vTypeAddr(){
		if(!$('type_address1').checked && !$('type_address2').checked){
			regError('Please provide Type of Address.');
			return false;
		}
		return true;
	}

	function _vShippingRates() {
		var value = $RF('shipping_form', 'shipping_data[shipping_rates]');
		if(!value) {
			regError('Please select a shipping method.');
			return false;
		}
		return true;
	}
}

//step 2 validation
function validateStep3(){
	var f = $('bill_form');
		var flag=true;
		clearErrors();
		if(!_vClientName()) flag=false;
		if(!_vClientAddress()) flag=false;
		if(!_vClientCity()) flag=false;
		if(!_vClientZip()) flag=false;
		if(!_vClientCountry()) flag=false;
		if(!_vClientPhone()) flag=false;
		if(!_vClientEmail()) flag=false;		
		if(!_vCCname()) flag=false;
		if(!_vCCnumber()) flag=false;
		if(!_vCCcvv()) flag=false;

		if (!flag) {			
			$("errorBlock").style.display = "";
			window.location = "/products/store/checkout/billing#error";
		}
		return flag;	

	function _vClientName(){
		if($F('firstName')=="" || $F('lastName')=="") {
			regError('Please provide your name.');
			return false;
		}
		return true;
	}
	
	function _vClientPhone(){
		if($F('phone')=="") {
			regError('Please provide your phone number.');
			return false;
		}
		return true;
	}
	
	function _vClientEmail(){
		var e1=$F('email');
		if(e1==""){
			regError('Please provide your email address.');
			return false;
		}
		else if(!validateEmail(e1)) {
			regError('Invalid email format.');
			return false;
		}
		return true;
	}

	function _vClientAddress(){
		if($F('address_one')=="") {
			regError('Please provide your address.');
			return false;
		}
		return true;
	}

	function _vClientCity(){
		if($F('city')=="") {
			regError('Please provide your city.');
			return false;
		}
		return true;
	}

	function _vClientZip(){
		if($F('zip')=="") {
			regError('Please provide your Zip code.');
			return false;
		}
		return true;
	}

	function _vClientCountry() {		
		if($F('country')=="") {
			regError('Please provide your country.');
			return false;
		}		
		return true;
	}
	function _vCCname(){
		if($F('ccName')==""){
			regError('Please provide the name from credit card.');
			return false;
		}
		return true;
	}

	function _vCCnumber() {
		var c=$F('ccNumber').replace( / /, "" );
		if(c=="") {
			regError('Please provide your credit card number.');
			return false;
		}
		else if(!Mod10(c)) {
			regError('Invalid credit card number.');
			return false;
		}
		return true;
	}

	function _vCCcvv() {
		if($F('ccCvv')==""){
			regError('Please provide the card security code.');
			return false;
		}
		return true;
	}
}

function $RF(el, radioGroup) {
	    if($(el).type && $(el).type.toLowerCase() == 'radio') {
	        var radioGroup = $(el).name;
	        var el = $(el).form;
	    } else if ($(el).tagName.toLowerCase() != 'form') {
	        return false;
	    }
	 
	    var checked = $(el).getInputs('radio', radioGroup).find(
	        function(re) {return re.checked;}
	    );
	    return (checked) ? $F(checked) : null;
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: David Leppek :: https://www.azcode.com/Mod10

Basically, the alorithum takes each digit, from right to left and muliplies each second
digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
the multiple are then added together for a new number (1 + 2 = 3). You then add up the
string of numbers, both unaltered and new values and get a total sum. This sum is then
divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
name Mod 10 or Modulus 10. */
function Mod10(ccNumb) {  // v2.0
	var valid = "0123456789"  // Valid digits in a credit card number
	var len = ccNumb.length;  // The length of the submitted cc number
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var bNum = true;  // by default assume it is a number
	var bResult = false;  // by default assume it is NOT a valid cc
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit

	// Determine if the ccNumb is in fact all numbers
	for (var j=0; j<len; j++) {
		temp = "" + sCCN.substring(j, j+1);
		if (valid.indexOf(temp) == "-1"){bNum = false;}
	}

	// if it is NOT a number, you can either alert to the fact, or just pass a failure
	if(!bNum){
		/*alert("Not a Number");*/bResult = false;
	}

	// Determine if it is the proper length
	if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
		bResult = false;
	} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
		if(len >= 15){  // 15 or 16 for Amex or V/MC
			for(var i=len;i>0;i--){  // LOOP throught the digits of the card
				calc = parseInt(iCCN) % 10;  // right most digit
				calc = parseInt(calc);  // assure it is an integer
				iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
				i--;  // decrement the count - move to the next digit in the card
				iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
				calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
				calc = calc *2;                                 // multiply the digit by two
				// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
				// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
				switch(calc){
					case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
					case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
					case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
					case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
					case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
					default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
				}
				iCCN = iCCN / 10;  // subtracts right most digit from ccNum
				iTotal += calc;  // running total of the card number as we loop
			}  // END OF LOOP
			if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
				bResult = true;  // This IS (or could be) a valid credit card number.
			} else {
				bResult = false;  // This could NOT be a valid credit card number
			}
		}
	}
	return bResult; // Return the results
} 

function subscribeUser() {
	var email = $F('subscribeEmail')
	new Ajax.Request( '/utils/ajax_controller', {
		method: 'post',
		parameters: {
			m: 'NewsLetter',
			action: 'addSubscriber',
			email: email
		},
		onSuccess: function( transport ){
			var response = transport.responseText.evalJSON();			
			alert(response.info);
		},
		onFailure: function( transport ){
			//alert('Epic fail!');
		}
	} );
}

function changeWebCam( id ) {
	new Ajax.Request( '/utils/ajax_controller', {
		method: 'post',
		parameters: {
			m: 'WebCams',
			action: 'getHomeWebCam',
			id: id
		},
		onSuccess: function( transport ){
			var response = transport.responseText.evalJSON();
			if( response.success ) {
				var domain = response.data.w_domain;
				var camera_name = escape( response.data.w_camera_name );
				var mode = response.data.w_mode;
				var width = 320;
				var height = 240;
				var swf_url = domain + "/flash/webcam.swf";
				var express_url = domain + "/flash/expressInstall.swf";
				var div_name = "webcam_holder";
				var flashvars={domain: domain, camera_name: camera_name, app_mode: mode };
				var params={ allowfullscreen:"true", allowScriptAccess: "always"};
				var attributes={};
				swfobject.embedSWF( swf_url, div_name, width, height, "9.0.0", express_url, flashvars, params, attributes );
				
				loadWebCams( response.data.w_id );
			}
		},
		onFailure: function( transport ){
			//alert('Epic fail!');
		}
	} );
}

function loadWebCams( id ) {
	new Ajax.Request( '/utils/ajax_controller', {
		method: 'post',
		parameters: {
			m: 'WebCams',
			action: 'getHomeWebCams',
			id: id
		},
		onSuccess: function( transport ){
			var response = transport.responseText.evalJSON();
			if( response.success ) {
				$('leftCam').value = response.leftCam;
				$('rightCam').value = response.rightCam;
				
				$( 'horizontal_cams_list' ).hide();
				$('horizontal_cams_list').update( response.data );
				$( 'horizontal_cams_list' ).appear();
			}
		},
		onFailure: function( transport ){
			//alert('Epic fail!');
		}
	});
}

function popupWebCam( id ) {
	Shadowbox.init({
		skipSetup: true
	});
	
	Shadowbox.open({
		content: '/utils/ajax_controller/?m=WebCams&action=getPopupWebCam&id=' + id,
		player: 'iframe',
		title:  '',
		width:  640,
		height: 480
	});
}




function loadWidgetWebCam( id ) {
	new Ajax.Request( '/utils/ajax_controller', {
		method: 'post',
		parameters: {
			m: 'WebCams',
			action: 'getWidgetWebCam',
			id: id
		},
		onSuccess: function( transport ){
			var response = transport.responseText.evalJSON();
			if( response.success ) {
				var domain = response.data.w_domain;
				var camera_name = escape( response.data.w_camera_name );
				var mode = 'icon'; //response.data.w_mode;
				var width = 174;
				var height = 124;
				var swf_url = domain + "/flash/webcam.swf";
				var express_url = domain + "/flash/expressInstall.swf";
				var div_name = "widget_webcam_holder";
				var flashvars={domain: domain, camera_name: camera_name, app_mode: mode };
				var params={ allowfullscreen:"true", allowScriptAccess: "always"};
				var attributes={};
				if( $('widget_webcam_holder') ) {
					swfobject.embedSWF( swf_url, div_name, width, height, "9.0.0", express_url, flashvars, params, attributes );
				}
			}
		},
		onFailure: function( transport ){
			//alert('Epic fail!');
		}
	} );
}


function loadHomeWebCam() {
	new Ajax.Request( '/utils/ajax_controller', {
		method: 'post',
		parameters: {
			m: 'WebCams',
			action: 'getHomeWebCam'
		},
		onSuccess: function( transport ){
			var response = transport.responseText.evalJSON();
			if( response.success ) {
				var domain = response.data.w_domain;
				var camera_name = escape( response.data.w_camera_name );
				var mode = 'icon'; //response.data.w_mode;
				var width = 660;
				var height = 420;
				var swf_url = domain + "/flash/webcam.swf";
				var express_url = domain + "/flash/expressInstall.swf";
				var div_name = "webcam_holder";
				var flashvars={domain: domain, camera_name: camera_name, app_mode: mode, adv_mode: "false", background: "transparent", asp_mode: "false" };
				var params={ allowfullscreen:"true", allowScriptAccess: "always", wmode: "transparent"};
				var attributes={};
				if( $('webcam_holder') ) {
					swfobject.embedSWF( swf_url, div_name, width, height, "9.0.0", express_url, flashvars, params, attributes );
				}
			}
		},
		onFailure: function( transport ){
			//alert('Epic fail!');
		}
	} );
}