function MejM(id)
{
var params = 'width=710,height=443';
var url = id;
x = (screen.width - 710) / 2;
y = (screen.height - 443) / 2;
if (x<0) x = 0;
if (y<0) y = 0;
params += ',xposition=' + x + ',left=' + x + ',yposition=' + y + ',top=' + y;
params += ',location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,hotkeys=0';
var newWin = window.open(url, 'descWin', params );
newWin.focus();
}



function showBlock(block) {
divs = document.getElementById(block).parentNode.parentNode.getElementsByTagName('DIV');
if(document.getElementById(block).style.display=='block') {
document.getElementById(block).style.display='none';
}
 else {

for(i=0; i<divs.length; i++) {

if(divs[i].id.indexOf('block')!=-1) {
divs[i].style.display='none';
}
}
document.getElementById(block).style.display='block';
}
}


function doLoadCityOblast(value,p1) {
     var req = new JsHttpRequest();
     req.onreadystatechange = function() {
        if (req.readyState == 4) {
     					if(p1==1){
             		document.getElementById('result').innerHTML = req.responseText;
             	}
              if(p1==2)
             		document.getElementById('result2').innerHTML = req.responseText;
             	if(req.responseText.length==0)
             		document.getElementById('result2').innerHTML ="";
           //удаление окна загрузк списка
           var div = document.getElementById("wait");
 					 if(div) div.parentNode.removeChild(div);
        }
    }
     req.open(null, 'aj_callcounter.php?id='+p1, true);
     req.send( { q: value } );
}

function doLoadCityP(value, p, mode, country) {
     var req = new JsHttpRequest();
     req.onreadystatechange = function() {
        if (req.readyState == 4) {
        	if(mode=='cityoblast')
        		document.getElementById('result_oblcityp').innerHTML = req.responseText;
        	else if(mode=='forukr')
        		document.getElementById('result_citypp').innerHTML = req.responseText;
        	else if(mode=='forbel')
        		document.getElementById('result_citypp').innerHTML = req.responseText;
        	else if(mode=='forkaz')
        		document.getElementById('result_citypp').innerHTML = req.responseText;
        	else
           	document.getElementById('result_cityp').innerHTML = req.responseText;
          Loading('no');
        }
    }
    	//alert('aj_calc.php?id='+p+'&mode='+mode+'&country='+country)
     req.open(null, 'aj_calc.php?id='+p+'&mode='+mode+'&country='+country, true);
     req.send( { q: value } );
}

function doLoadCityCC(value, p, mode, country) {
     var req = new JsHttpRequest();
     req.onreadystatechange = function() {
        if (req.readyState == 4) {
        	if(mode=='idsen5')
        		document.getElementById('idsen5').innerHTML = req.responseText;
        	else
           	document.getElementById('idrecip4').innerHTML = req.responseText;
          Loading('no');
        }
    }
    req.open(null, 'aj_callcounter.php?id='+p+'&mode='+mode+'&country='+country, true);
    req.send( { q: value } );
}

function OutResult(value, p, mode, country) {
     var req = new JsHttpRequest();
     req.onreadystatechange = function() {
        if (req.readyState == 4) {
           document.getElementById('calcres').innerHTML = req.responseText;
        }
    }
    if(country=='rus')
    	req.open(null, 'ajcalrus.php5', true);
    
    if(country=='ukr'){
    		req.open(null, 'ajcalukr.php5', true);
    }
    if(country=='bel'){
    		req.open(null, 'ajcalbel.php5', true);
    }
    if(country=='kz'){
    		req.open(null, 'ajcalkaz.php5', true);
    }
    if(country=='abh'){
    		req.open(null, 'ajcalabh.php5', true);
    }
    req.send( { q: value } );
}

function OutResultOblast(value, p, mode){
     var req = new JsHttpRequest();
     alert("a")
     req.onreadystatechange = function(){
        if (req.readyState == 4){
        	alert("b")
           document.getElementById('calcres').innerHTML = req.responseText;
        }
    }
    req.open(null, 'aj_rescalcobl.php', true);
    req.send( { q: value } );
}

function showTable() {
	var tr = document.getElementById('hid1');
	var tr2 = document.getElementById('hid2');
	var tr3 = document.getElementById('hid3');
	if (tr.style.display == "") {
		tr.style.display = "none";
		//tr2.style.display = "none";
		tr3.style.display = "none";
	} else {
		tr.style.display = "";
		//tr2.style.display = "";
		tr3.style.display = "";
	}
}

function showTr(pr,buk) {
	var tr = document.getElementById(pr);
	if(tr.value!=""){
		if (buk == "n" || buk == "") {
			tr.style.display = "none";
		} else {
			tr.style.display = "";
		}
	}
}

function showTr2(pr,buk) {
	var tr = document.getElementById(pr);
	var cityo = document.getElementById("cityo");
	var trcitymov = document.getElementById("citymov_view");
	var oplata_v = document.getElementById("oplata_view");
	if(tr.value!=""){
		if (findfilial()>0) {
				addSelect(1);
		} 
		else{
			addSelect(0);
		}
	}
	//alert(cityo.options[cityo.selectedIndex].value)
	//(buk=="28z"&&cityo.options[cityo.selectedIndex].value==104) || (buk=="80z"&&cityo.options[cityo.selectedIndex].value==109) ||(buk=="55z"&&cityo.options[cityo.selectedIndex].value==130)
	//alert(findSelect())
	if(findSelect()){
		trcitymov.style.display = "";			
		oplata_v.style.display = "none";
	}
	else{
		trcitymov.style.display = "none";
		oplata_v.style.display = "";
	}
	//addRow("tab1");
}

function Loading(setLoad)
{
 if (setLoad=='no')
 {
 var div = document.getElementById("wait");
 if(div) div.parentNode.removeChild(div);
 }
 else
 {
 var div = document.body.appendChild(document.createElement("DIV"));
 div.id = "wait";
 div.innerHTML = '<div class="preload"><h5>Загрузка. Пожалуйста, подождите...</h5><br /><img src="./i/loading.gif"  border="0" alt="" /></div>';
 div.style.width='200px';
 div.style.height='50px';
 div.style.left = getScrollPosition()['left'] + getWindowSize()['width']/2 - div.offsetWidth/2 + "px";
 div.style.top = getScrollPosition()['top'] + getWindowSize()['height']/2 - div.offsetHeight/2 + "px";
 div.style.position = 'absolute';
 
 div.style.background='#FFF';
 div.style.padding='10px';
 div.style.border='1px solid #2D5591';
 
}
}

function getScrollPosition() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof window.pageYOffset == "number") {
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body &&
        (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement &&
        (document.documentElement.scrollLeft ||
        document.documentElement.scrollTop)) {
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return {top:scrOfY, left:scrOfX};
}

function getWindowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof window.innerWidth == "number") {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement &&
        (document.documentElement.clientWidth ||
        document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body &&
        (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return {height:myHeight, width:myWidth};
}

function form_element (element_form) {
        form_value='';
		if (element_form.type=='text' || element_form.type=='textarea') {
			if(element_form.value==''){
			form_value='';
			}
			else {
			form_value=element_form.value;
			}
		}
		if (element_form.type=='select-one'){
			if(element_form.options[element_form.selectedIndex].value==''){
			form_value='';
			}
			else {
			form_value=element_form.options[element_form.selectedIndex].value;
			}
		}
		return form_value;
		}
		
//		function checkFeedback(form, flag) {
//		var count=0;
//	
//		var ms = document.getElementById("cityo");
//		var ms_i = ms.selectedIndex;
//		var ms_c = document.getElementById("coutry");
//		if(ms.options[ms_i].value==''){
//		 alert("Необходимо заполнить город отправления!");
//		 //ms_c.style.disabled=true;
//		 	ms_c.disabled=true;
//		}
//		else{
//			ms_c.disabled=false;
//		}
//	
//	for(i=0; i<(form.length-1); i++) {
//		var star='star_'+form.elements[i].name;
//		//alert(star);
//		//alert(document.getElementById(star));
//		//if(form.elements[i].type!='select-one') {
//		//alert(form.elements[i].name+form.elements[i].selectedIndex);
//			//if(form.elements[i].value=='' || form.elements[i].options[form.elements[i].selectedIndex].value=='') {
//			form_value=form_element (form.elements[i]);
//			if(document.getElementById(star)){ //&& form.elements[i].name!='dis'
//				if ((form_value=='')){
//					document.getElementById(star).style.display = 'inline';
//					count++;
//					}
//				else {
//					document.getElementById(star).style.display = 'none';
//				}
//				if((form.elements[i].name=='email')&&(!isEmail(form.email.value))){
//					document.getElementById(star).style.display = 'inline';
//					//alert(document.getElementById(star))
//					count++;
//				}
//			//}
//		}
//			if(form.elements[i].name=="pay"){
//						if(form.pay.value=="n"){
//							count--;
//						}
//					}
//	}
//		//alert(count);
//	if((count==0)&&(flag==1)) { 
//			form.submit();
//		return false;
//	} else {
//		return false;
//	}
//}

function checkFeedbackS(form, flag) {
	var count=0;


	for(i=0; i<(form.length-1); i++) {
		var star='star_'+form.elements[i].name;
		form_value=form_element (form.elements[i]);
		if(document.getElementById(star)){
			if ((form_value=='')){
				document.getElementById(star).style.display = 'inline';
				count++;
			}
			else {
				document.getElementById(star).style.display = 'none';
			}
			if((form.elements[i].name=='email')&&(!isEmail(form.email.value))){
				document.getElementById(star).style.display = 'inline';
				count++;
			}
		}
	}
	if((count==0)&&(flag==1)) {
		form.submit();
		return false;
		} else {
			return false;
		}
	}

function checkFeedbackM(form, flag) {
	var count=0;

	var ms = document.getElementById("cityo");
	var ms_i = ms.selectedIndex;

	for(i=0; i<(form.length-1); i++) {
		var star='star_'+form.elements[i].name;
		form_value=form_element (form.elements[i]);
		if(document.getElementById(star)){
			if ((form_value=='')){
				document.getElementById(star).style.display = 'inline';
				count++;
			}
			else {
				document.getElementById(star).style.display = 'none';
			}
			if((form.elements[i].name=='email')&&(!isEmail(form.email.value))){
				document.getElementById(star).style.display = 'inline';
				count++;
			}
		}
		if(form.elements[i].name=="pay"){
			if(form.pay.value=="n"){
				count--;
			}
		}
	}
	if((count==0)&&(flag==1)) {
		form.submit();
		return false;
		} else {
			return false;
		}
	}

function checkFeedbackCalc(form, flag, mess) {
		var count=0;
	
		var ms = document.getElementById("cityo");
		var ms_i = ms.selectedIndex;
		var cityp = document.getElementById("cityp");
		var citypp = document.getElementById("citypp");
	
	for(i=0; i<(form.length-1); i++) {
		var star='star_'+form.elements[i].name;

			form_value=form_element (form.elements[i]);
			if(document.getElementById(star)){ //&& form.elements[i].name!='dis'
				if ((form_value=='')){
					document.getElementById(star).style.display = 'inline';
					count++;
					}
				else {
					document.getElementById(star).style.display = 'none';
				}
		}
		if((citypp) && (form.elements[i].name=="cityp")){
			var ind = cityp.selectedIndex;
			var indpp = citypp.selectedIndex;
			if((cityp.options[ind].value!=320) ){
				if(cityp.options[indpp].value=='')
							count--;
			}
		}
	}
	if((count==0)&&(flag==1)) { 
		return true;
	} else {
		alert(mess+"!");
		return false;
	}
}
function checkFeedback(form, flag) {
		var count=0;
	
		var ms = document.getElementById("cityo");
		var ms_i = ms.selectedIndex;
		var ms_c = document.getElementById("coutry");
		if(ms.options[ms_i].value==''){
		 	ms_c.disabled=true;
		}
		else{
			ms_c.disabled=false;
		}
	
	for(i=0; i<(form.length-1); i++) {
		var star='star_'+form.elements[i].name;

			form_value=form_element (form.elements[i]);
			if(document.getElementById(star)){ //&& form.elements[i].name!='dis'
				if ((form_value=='')){
					document.getElementById(star).style.display = 'inline';
					count++;
					}
				else {
					document.getElementById(star).style.display = 'none';
				}
				if((form.elements[i].name=='email')&&(!isEmail(form.email.value))){
					document.getElementById(star).style.display = 'inline';
					count++;
				}
		}
			if(form.elements[i].name=="pay"){
						if(form.pay.value=="n"){
							count--;
						}
					}
	}
	if((count==0)&&(flag==1)) { 
			form.submit();
		return false;
	} else {
		return false;
	}
}

function isEmail(string) {
	return string.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
}

function doc() {
	if (document.getElementById('tipdoc').options[document.getElementById('tipdoc').selectedIndex].value=='n') 
	document.getElementById('tipdoc2').name = 'soderjomoe'; 
	else document.getElementById('tipdoc2').name = 'dis';
}

function dogovor() {
	if (document.getElementById('pay').options[document.getElementById('pay').selectedIndex].value=='n') 
	document.getElementById('dogovor').name = 'pay2'; 
	else document.getElementById('dogovor').name = 'dis1';
}


function addRow(id){
   	var table = document.getElementById(id); 

		var row = table.insertRow(table.rows[0] ? table.rows.length : 0);
		var colsCount = table.rows[0] ? table.rows[0].cells.length : 0;
		var indt = table.rows.length-2
		var td = document.createElement("td");
		td.innerHTML ='&nbsp;'
		row.appendChild(td); 
		
		var td = document.createElement("td");
		td.innerHTML ='<select name="oplata" id="oplata"><option value="">Выберите</option><option value="o">оплата отправителем<option value="p">оплата получателем</select>';
		//td.setAttribute("tabindex","'+indt+3+'");
		row.appendChild(td); 
  }  
  
  
function addSelect(bit){
    var select = document.getElementById("oplata");
    // очищаем SELECT
    select.options.length = 0;
    if(bit){
	    // если данных нет - не делаем больше ничего
	    select.options[select.options.length]= new Option("Выберите", "", false, false);
	    select.options[select.options.length]= new Option("оплата отправителем", "o", false, false);
	    select.options[select.options.length]= new Option("оплата получателем", "p" , false, false);
	  }
	  else{
	  	select.options[select.options.length]= new Option("оплата отправителем", "o", false, false);
	  }
}  

function findSelect(){
	var ms = document.getElementById("cityo");
	var ms_i = ms.selectedIndex;
	var cp = document.getElementById("cityp");
	var ind = cp.selectedIndex;
	//for (var i = 0; i < ms.options.length; i++) {
		//alert(ms.options[i].text+"  "+cp.options[ind].text);
      if(ms.options[ms_i].text==cp.options[ind].text)
       return  1;
	//}
	return  0;
}

function findfilial(){
	var ms = document.getElementById("cityo");
	var ms_i = ms.selectedIndex;
	var cp = document.getElementById("cityp");
	var ind = cp.selectedIndex;
	for (var i = 0; i < ms.options.length; i++) {
	    if(ms.options[i].text==cp.options[ind].text)
       return  1;
	}
	return  0;
}

function win(id)
{
	var params = 'width=400,height=316';
	var url = id;
	x = (screen.width - 400) / 2;
	y = (screen.height - 316) / 2;
	if (x<0) x = 0;
	if (y<0) y = 0;
	params += ',xposition=' + x + ',left=' + x + ',yposition=' + y + ',top=' + y;
	params += ',location=0, menubar=0, resizable=0, scrollbars=0, status=0, titlebar=0, toolbar=0,hotkeys=0';
	var newWin = window.open(url, 'descWin', params );
	newWin.focus();
}

function isDigit(data) {
		numStr="0123456789.,";
		k = 0;
		for (i=0;i<data.length;i++) {
		thisChar = data.substring(i, i+1);
		if (numStr.indexOf(thisChar) != -1) k++;
		}
		if (k == data.length && data.length>0 ){
		 return 1
		}
		else{ 
			return 0
		}
	}
	
	function VerifInput(form){
		var cp = document.getElementById('cityp'); 
		var er=""
		if(cp.value == 0){
			cp.style.background = "#FFCCCC";
			er = "Заполните поле город отправления\n";
		}
		else{
			cp.style.background = "#FFFFFF";
		}
		var ms = document.getElementById('massa'); 
		if(!isDigit(ms.value)){
			ms.style.background = "#FFCCCC";
		  er += "Заполните поле Общий вес отправки\n";
		}
		else{
			ms.style.background = "#FFFFFF";
		}
		if(er.length>0){ 
			alert(er)
			return false;
		}
		else
			return true;
	}
	
function showDopField(el, curind, sel) {
	var tr = document.getElementById(el);
	if(tr.value!=""){
		if (sel == curind) {
			tr.style.display = "";
		} else {
			tr.style.display = "none";
		}
	}
}

function showDopFieldTo(el, curind) {
	var t3 = document.getElementById('admin3dop');
	var t2 = document.getElementById('admin3dop2');
	var t4 = document.getElementById('idadmin4');
	
	var tr = document.getElementById(el);
	if(tr.value!=""){
		if (curind==2) {
			t2.style.display = "";
			t3.style.display = "none";
			t4.style.display = "none";
		}
		else if (curind==3) {
			t2.style.display = "none";
			t3.style.display = "";
			t4.style.display = "";
		} else {
			t2.style.display = "none";
			t3.style.display = "none";
			t4.style.display = "none";
		}
	}
}

function checkFeedback2(form, flag) {
		var count=0;
	
	
	for(i=0; i<(form.length-1); i++) {
		var star='star_'+form.elements[i].name;

			form_value=form_element(form.elements[i]);
			if(document.getElementById(star)){ //&& form.elements[i].name!='dis'
				if ((form_value=='')){
					document.getElementById(star).style.display = 'inline';
					count++;
					}
				else {
					document.getElementById(star).style.display = 'none';
				}
				if((form.elements[i].name=='email')&&(!isEmail(form.email.value))){
					document.getElementById(star).style.display = 'inline';
					count++;
				}
		}
	}
	if((count==0)&&(flag==1)) { 
			form.submit();
		return false;
	} else {
		return false;
	}
}
