var x = 1;
var wsf_scrollInterval = 150;
var count = 0, countDiv = 0, countArray = -1, wsf_flag = true;
var resent_sales_name = [], resent_sales_price = [];

function insertAfter(parent, newNode, referenceNode) {
	parent.insertBefore(newNode, referenceNode);
}

function wsf_checkEmail(str) {
	return str.search(/^([\w\+\-]+)(\.[\w\+\-]+)*@([a-z\d\-]+\.)+[a-z]{2,6}$/i) > -1; 
}


function wsf_ini() {
	var da = document.getElementById('wsf_scroll');
		da.style.top = 0 + 'px';
	var html = ''; lengthArray = resent_sales_name.length >= 17 ? 17 : resent_sales_name.length;
	for(var i = 0; i < lengthArray; i++) {
		if(i == lengthArray - 1)
			html += '<div id="sib' + i + '" style="width:198px;">';
		else 
			html += '<div id="sib0" style="width:198px;">';
		if(i % 2 == 0) {	
			html += '<span id="line_l_on">' + resent_sales_name[i] + '</span>';
			html += '<span id="line_r_on">$' + resent_sales_price[i] + '</span>';
		}
		else{
			html += '<span id="line_l_off">' + resent_sales_name[i] + '</span>';
			html += '<span id="line_r_off">$' + resent_sales_price[i] + '</span>';
		}
		html += '</div>';
	}	
	da.innerHTML = html;
	countDiv = lengthArray - 2;
	countArray = lengthArray - 1;
}

function addElement(newDiv) {	
	var da = document.getElementById('wsf_scroll');
	var elementAboveNew = document.getElementById('sib' + newDiv);
	var newElement = document.createElement('div');	
	if(countArray == resent_sales_name.length - 1)
		countArray = 0;//resent_sales_name.length >= 17 ? 17 : resent_sales_name.length;
	else
		countArray++;
	newElement.setAttribute("width","198px");	
	if(countArray % 2 == 0)
		newElement.innerHTML = '<span id="line_l_off">' + resent_sales_name[countArray] + '</span><span id="line_r_off">$' + resent_sales_price[countArray] + '</span>';
	else
		newElement.innerHTML = '<span id="line_l_on">' + resent_sales_name[countArray] + '</span><span id="line_r_on">$' + resent_sales_price[countArray] + '</span>';
	insertAfter(da,  newElement, elementAboveNew);
	count = 0;
	if(newDiv == resent_sales_name.length + 27){
		wsf_ini();
	}	
}


function wsf_moveDiv() {	
	count++;	
	if(count % 10 == 0){
		countDiv++;
		addElement(countDiv);
	}	
	document.getElementById('wsf_scroll').style.top = parseInt(document.getElementById('wsf_scroll').style.top) - 1 + 'px';	
	if(wsf_flag)
		setTimeout('wsf_moveDiv()',wsf_scrollInterval);	
}

function disableDateofCalendar(date, y, m, d){
   var minDate = new Date();
   if((date.getFullYear() < minDate.getFullYear()) || (date.getFullYear() == minDate.getFullYear() && date.getMonth() < minDate.getMonth()) || (date.getFullYear() == minDate.getFullYear() && date.getMonth() == minDate.getMonth() && date.getDate() < minDate.getDate())){
      return true;
   }
}

function wsf_trim(s) {
	return s.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
}

function wsf_parse_date(date) {	
	var checkDate = date.split("/");		
	var sss = checkDate[2] + '' + checkDate[0] + '' + checkDate[1];	
	return parseInt(sss);		
	 
}


function wsf_checkForm(type){
	
	var depdate = $('depdate').value;
	var retdate = $('retdate').value;
	if ($('fairport').value == 'Select Departure'){
		alert ("You forgot to fill in where you\'re flying from!");
		$('fairport').focus();
		return false;
	}
	if ($('tairport').value == 'Select Destination'){
		alert ("You forgot to fill in where you\'re flying to!");
		$('tairport').focus();
		return false;
	}
	if (wsf_trim(depdate) == ''){
		alert ("You forgot to fill in departure date!");
		$('depdate').focus();
		return false;
	}
		
	if (wsf_trim(retdate) == '' && !document.search.trip_type[1].checked){
		alert ("You forgot to fill in return date!");
		$('retdate').focus();
		return false;
	}
	var checkdepDate = wsf_parse_date(depdate);
	var checkretDate = wsf_parse_date(retdate);
	if(document.getElementById("one_way")){
		if(document.getElementById("one_way").checked == false && (checkretDate - checkdepDate < 0)){
			alert('Please, check you return date!');
			return false;
		}
	}
	else{
		if(checkretDate - checkdepDate < 0){
			alert('Please, check you return date!');
			return false;
		}
	}
	
	if(type == 3){		
		if($('contactWay').value == 1){		
			if(!wsf_checkPhoneNumber($('phone').value)){				
				alert("Your Phone is not correct! Please check it and try again.");
				$('phone').focus();
				return false;		
			}					
		}	
		else {	
			if(wsf_Trim($('phone').value) != ''){				
				if(!wsf_checkPhoneNumber($('phone').value)){
					alert("Your Phone is not correct! Please check it and try again.");
					$('phone').focus();
					return false;		
				}	
			}	
			if(wsf_Trim($('email').value) != '') {
				if(!wsf_checkEmail($('email').value)){
					alert("Your Email is not correct! Please check it and try again.");
					$('email').focus();
					return false;		
				}
			}		
			else{
				alert("Your Email is not correct! Please check it and try again.");
				$('email').focus();
				return false;		
			}
		}	
	}	
	else {
		//alert('153');
		if(document.getElementById("open_jaws").checked == true) {
			if(wsf_trim(document.getElementById("openjaw").value) == "") {
				alert ("Return city must be entered!");	
				document.getElementById("openjaw").focus();				
				return false;
			}	
		}
	}	
	return true;	
}


function wsf_sity(value) {	
	var rootSelect = document.formSubmit.tairport;		
	var length = rootSelect.length, str = '';
	for(var i = 0; i < length; i++) {
		str = rootSelect.options[i].value;		
		if(str.indexOf(value) != -1){
			rootSelect.value = str;
			break;
		}
	}	
}
function wsf_onSubmit(type, domain) {	
	var check = wsf_checkForm(type);		
	if(check){
		$('formSubmit').action = 'http://' + domain + '/' + (type == 1 ? "search-economy" : (type == 2 ? "search-business" : "result-group"));									
		$('formSubmit').submit();
	}	
}

function wsf_requestForm(domain) {	
	$('formSubmitOn').action = 'http://' + domain + '/request-form';									
	$('formSubmitOn').submit();
}

function wsf_Trim(s) {
	return s.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
}

function wsf_checkPhoneNumber(str) {
	return str.search(/^([0-9]{3})+(\-)+([0-9]{3})+(\-)+([0-9]{4})$/i) > -1;
}

function wsf_check_req_form() {
		
	if(wsf_Trim($('firstName').value) == '') {
		alert ("You must to fill your First name!");
		$('firstName').focus();
		return false;
	}		
	if(wsf_Trim($('lastName').value) == '') {
		alert ("You must to fill your Last name!");
		$('lastName').focus();
		return false;
	}	
	if($('contactWay').value == 1){		
		if(!wsf_checkPhoneNumber($('phone').value)){				
			alert("Your Phone is not correct! Please check it and try again.");
			$('phone').focus();
			return false;		
		}					
	}	
	else {	
		if(wsf_Trim($('phone').value) != ''){				
			if(!wsf_checkPhoneNumber($('phone').value)){
				alert("Your Phone is not correct! Please check it and try again.");
				$('phone').focus();
				return false;		
			}	
		}	
		if(wsf_Trim($('email').value) != '') {
			if(!wsf_checkEmail($('email').value)){
				alert("Your Email is not correct! Please check it and try again.");
				$('email').focus();
				return false;		
			}
		}		
		else{
			alert("Your Email is not correct! Please check it and try again.");
			$('email').focus();
			return false;		
		}
	}	
	return true;	
}

function wsf_subRequestForm(domain) {	
	var checkForm = wsf_check_req_form();
	if(checkForm){
		$('formSubmit').action = 'http://' + domain + '/send-form';									
		$('formSubmit').submit();
	}
}



function changeClass(type, domain) {	
	$('formSubmit').action = 'http://' + domain + '/' + (type == 1 ? "" : "business");									
	$('formSubmit').submit();
}

function wsf_showResult(typeClass, domain) {			
	$('formSubmit').action = 'http://' + domain + '/' + (typeClass == 1 ? "result-economy" : "result-business");									
	$('formSubmit').submit();
}

function wsf_hS(id, action) {
	$(id).setStyle('display', action);
}

function openWin2(domain) {
	window.open ('http://' + domain + '/taxes','taxeswindow','status=0, toolbar=0, scrollbars=1, menubar=0, location=0, resizable=0, width=395, height=480');
}