var Semaphore = 0;

function open_win(doc,h,w) {
var scrolling = "yes";
var wnd = window.open(doc,"","width=" + w + ",height=" + h + ",scrollbars=" + scrolling + ",status=no,menubar=no,resizable=yes,location=no,toolsbar=no");
}

function open_win1(doc,h,w) {
var scrolling = "no";
var wnd = window.open(doc,"","width=" + w + ",height=" + h + ",scrollbars=" + scrolling + ",status=no,menubar=no,resizable=no,location=no,toolsbar=no");
}

function open_win2(doc,h,w) {
	var scrolling = "no";
	
	var wx = (screen.width-w)/2;
	var wy = (screen.height-h)/2;
	
	var wpos = "left="+wx+",top="+wy;

	window.open(doc, "", "width=" + w + ",height=" + h + ",scrollbars=" + scrolling + ",status=no,menubar=no,resizable=no,location=no,toolsbar=no," + wpos);

}

// ---------------------------------------------------------------------------
function IsNumber(data) {
    var NumStr="0123456789";
    var ch;
    var count;
    var i;
    for(i=0;i<data.length;i++) 
      {
	ch=data.substring(i,i+1);
	if(NumStr.indexOf(ch)!=-1)count++;
      }
    if(count==data.length) { return true; } else { return false; }
}

function IsE164 (data){
var NumStr="0123456789+ ()";
var ch;
var count;
var i;
for(i=0;i<data.length;i++) 
  {
	ch=data.substring(i,i+1);
	if(NumStr.indexOf(ch)!=-1)count++;
  }
if(count==data.length) { return true; } else { return false; }
}

// рТПЧЕТЛБ РБТПМС
function ValidatePwd(pw1,pw2,lang) {
var line = "";	   // Information line
var invalid = " "; // Invalid character is a space
var minLength = 8; // Minimum length
// check for a value in both fields.
if (pw1.value == '' || pw2.value == '') 
  { 
	if (lang == 0) { line = "PASSWORD: Please, insert password twice!\n"; }
	else { line = "ПАРОЛЬ: Необходимо ввести пароль дважды!"; }
	alert(line); 
	return false;
  }
// check for minimum length
if (pw1.value.length < minLength)
 {
	if (lang == 0) { line = "PASSWORD: Minimum password size should be " + minLength + " letters!\n"; }
	else { line = "ПАРОЛЬ: Минимальная длина пароля " + minLength + " букв!"; }
	alert(line); 
	return false;
 }
// check for spaces
if (pw1.value.indexOf(invalid) > -1) 
 { 
	if (lang == 0) { line = "Spaces are not allowed!\n"; }
	else { line = "ПАРОЛЬ: Пробелы не допустимы!"; }
	alert(line); 
	return false;
 }
else 
 {
	if (pw1.value != pw2.value) 
	  { 
		if (lang == 0) { line = "PASSWORD: Passwords are not the same!\n"; }
		else { line = "ПАРОЛЬ: Пароли не совпадают!"; }
		alert(line); 
		return false;
	  }
	else { return true; }
 }
}

// ------------------------------------------------------------
// Clear HTML code in TEXTAREA fields
// Using: 
// CheckedString = CheckHTML(String)

function DelHTML(Word) {
a = Word.indexOf("<");
b = Word.indexOf(">");
len = Word.length;
c = Word.substring(0, a);
if(b == -1)
b = a;
d = Word.substring((b + 1), len);
Word = c + d;
tagCheck = Word.indexOf("<");
if(tagCheck != -1) Word = Del(Word);
return Word;
}

function CheckHTML(str) {
var Checked = DelHTML(str);
return Checked;
}

// -------------------------- WAIT when operaion is finished --
function wait_for() {
document.body.style.cursor = "wait";
}

function normal_mode() {
document.body.style.cursor = "default";
}

// ---------------- BLOCK JS ERRORS ---------------------------
// in BODY onLoad="....; window.onerror = blockError; ...."
function blockError(){return true;}

// --------------------- SPECIAL CHARS FUNCTIONS --------------
// Encode special chars
function EncodeSpecialChars(str) {
return escape(str);
}

// Doesn't allow special symbols in the TEXT // TEXTAREA fields
// Do something like this...
//<form name=xyz>
//<input type="text" name="txtTerm" size="35" maxlength="50" value="" 
//                                       onKeyUp="javascript:SpecSymbolsCheck(xyz.txtTerm);">
function SpecSymbolsCheck(val) {
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
}

// -----------------------------------------------------------------------
// чТЕНС Й дБФБ
var YourDate = new Date();
parent.header_loaded = '';

function ShowDayOfWeek(Day){
DayText='Today';
if (Day == 0){DayText = 'Sunday'}
if (Day == 1){DayText = 'Monday'}
if (Day == 2){DayText = 'Tuesday'}
if (Day == 3){DayText = 'Wednsday'}
if (Day == 4){DayText = 'Thirsday'}
if (Day == 5){DayText = 'Friday'}
if (Day == 6){DayText = 'Saturday'}
document.write(DayText);
}
			
function ShowMonth(Month){
MonthText='This Month';
if (Month == 0){MonthText = 'January&nbsp;'}
if (Month == 1){MonthText = 'February&nbsp;'}
if (Month == 2){MonthText = 'March&nbsp;'}
if (Month == 3){MonthText = 'April&nbsp;'}
if (Month == 4){MonthText = 'May&nbsp;'}
if (Month == 5){MonthText = 'June&nbsp;'}
if (Month == 6){MonthText = 'July&nbsp;'}
if (Month == 7){MonthText = 'August&nbsp;'}
if (Month == 8){MonthText = 'September'}
if (Month == 9){MonthText = 'October&nbsp;'}
if (Month == 10){MonthText = 'November&nbsp;'}
if (Month == 11){MonthText = 'December&nbsp;'}
document.write(' ' + MonthText);
}
			
function ShowDay(DayDate){
th='th';
if (DayDate == 1) {th = 'st'}
if (DayDate == 21) {th = 'st'}
if (DayDate == 31) {th = 'st'}
if (DayDate == 2) {th = 'nd'}
if (DayDate == 22) {th = 'nd'}
if (DayDate == 3) {th = 'rd'}
if (DayDate == 23) {th = 'rd'}
document.write(' ' + DayDate + "-" + th + ",");
}
			
function ShowYear(Year){
if (Year > 96) {Century = ''} else {Century = '20'}
if (navigator.appName.lastIndexOf('Netscape') !=-1)
  {
 	Century = ''; Year = Year + 1900;
  }
document.write(Century + Year);
}

function loaded (){
parent.header_loaded = 'true';
}

// -------- Time -------

function show_time()
{
my_date= new Date();
hour=my_date.getHours() - 4;
if (hour<10)
{hour="0"+hour;}
minute=my_date.getMinutes();
if (minute<10)
{minute="0"+minute;} 
second=my_date.getSeconds();
if (second<10)
{second="0"+second;} 

// window.status=" " + hour + "h" + minute + ":" + second;
// setTimeout("show_time()",1000);
sTime = " " + hour + ":" + minute + ":" + second;
document.write(' ' + hour + ":" + minute + ":" + second);
}

// ------------------- Cookies --------------------------

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf(prefix);
  if (begin == -1)
    return null;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

// ------------------------------ Net addresses checking up functions -----------------
// IP address validation insert this into text field:
// onBlur='javascript:VerifyIP(IPvalue.value)'
function VerifyIP (IPvalue) {
errorString = "";
theName = "IP address";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern); 

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and can not be used at here!';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and can not be used at here!';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is wrong IP address!';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is wrong IP address!';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and can not be used at here!';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "") { return true; } else { alert (errorString); return false; }
}

function ValidateEMail(name) {
var arr;
var badchars="<> ;,'!#$%^&*()+=";
if (name=="") return false;
for (var i=0; i < name.length; i++)
 {
	temp = "" +name.substring(i, i+1);
	if (badchars.indexOf(temp) != -1) { return false; }
 }
arr = name.split('@');
if (arr.length != 2) return false;
if ((arr[0] == "") || (arr[1] == "")) return false;
if ((arr[1].charAt(0) == '.') || (arr[1].charAt(arr[1].length-1) == '.')) return false;
if ((arr[0].charAt(0) == '.') || (arr[0].charAt(arr[0].length-1) == '.')) return false;
if ((arr[1].indexOf('.') > arr[1].length-3) || (arr[1].indexOf('.') == -1)) return false;
if (name.indexOf("..") > 0) return false;
return true;
}
// --------------------- OPTIONAL FUNCTIONS END LINE ----------
function clear_semaphore() {
Semaphore = 0;
}

// ------------------------------------------------------------

function on_login() {
var line="";

fLang = eval("document.postform.lang.value")

if (document.postform.login.value=="") 
  { 
  	if (fLang == 0) { line = line + "Login\n"; }
	else { line = line + "Логин\n"; }
  }
if (document.postform.pass.value=="")
  { 
  	if (fLang == 0) { line = line + "Password\n"; }
	else { line = line + "Пароль\n"; }
  }

if (line.length > 0)
  {

	if (fLang == 0) { line = "You didn't fill the following neccessary fields:\n\n" + line; }
	else { line = "Вы не заполнили следующие поля:\n\n" + line; }
	alert(line);
  }
else
  {
  	bStorePassw = eval("document.postform.storepass.checked")
	if (bStorePassw)
	  {
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);

		fLogin = eval("document.postform.login.value")
		fPassw = eval("document.postform.pass.value")

		setCookie("login", fLogin, now);
		setCookie("passw", fPassw, now);
	  }
	document.forms[0].action="/mod/enter.pl";
	document.forms[0].submit();
  }
}

function test_login() {
	fCode = eval("document.forms[0].login.value")
	fLang = eval("document.forms[0].lang.value")
	if (fCode == "") 
	 { 
	    line = "";
	    if (fLang == 0) { line = line + "You must insert login name!\n"; }
	    else { line = line + "Вам необходимо ввести логин!\n"; }
	    alert (line);
	 }
	else { open_win("/mod/test_login.cgi?login="+fCode+"&lang="+fLang,150,400); }
}

function reg_step() {
var line="";

fLang = eval("document.postform.lang.value")
fReg = eval("document.postform.reg.value")

if (document.postform.login.value=="") 
  { 
	if (fLang == 0) { line = line + "Login\n"; }
	else { line = line + "Логин\n"; }
  }
if (document.postform.fname.value=="")
  { 
	if (fLang == 0) { line = line + "First name\n"; }
	else { line = line + "Имя\n"; }
  }
if (document.postform.lname.value=="")
  { 
	if (fLang == 0) { line = line + "Last name\n"; }
	else { line = line + "Фамилия\n"; }
  }

if (fReg == 1)
 {
    if (document.postform.company.value=="")
      { 
	if (fLang == 0) { line = line + "Company\n"; }
	else { line = line + "Компания\n"; }
      }
    if (document.postform.city.value=="")
      { 
    	if (fLang == 0) { line = line + "City\n"; }
	else { line = line + "Город\n"; }
      }
    if (document.postform.country.value=="")
      { 
    	if (fLang == 0) { line = line + "Country\n"; }
    	else { line = line + "Страна\n"; }
      }

    if (document.postform.phone.value=="")
      {
        if (fLang == 0) { line = line + "Phone\n"; }
        else { line = line + "Телефон\n"; }
      }


    var zip = eval("document.postform.zip.value")
    var phone = eval("document.postform.phone.value")
    var fax = eval("document.postform.fax.value")

    if (IsNumber(zip))
      { 
	if (fLang == 0) { line = line + "Zip code\n"; }
	else { line = line + "Почтовый индекс\n"; }
      }
    if (IsE164(phone))
      { 
    	if (fLang == 0) { line = line + "Phone\n"; }
	else { line = line + "Телефон\n"; }
      }
    if (IsE164(fax))
      { 
	if (fLang == 0) { line = line + "Fax\n"; }
	else { line = line + "Факс\n"; }
      }
 }

var email = eval("document.postform.email.value")
if (!(ValidateEMail(email))) { line=line+"E-Mail\n"; }

if (line.length > 0)
  {
	if (fLang == 0) { line = "You didn't fill the following neccessary fields\nor they filled incorrectly:\n\n" + line; }
	else { line = "Вы не заполнили или заполнили не корректно следующие поля:\n\n" + line; }
	alert(line);
  }
else
  {
	document.forms[0].action="/mod/reg.pl";
	document.forms[0].submit();
  }
}

function show_market(num) {
	    open_win1("/mod/search_frame.cgi?pid="+num,318,518);
}

function on_search2() {
	fPhone = eval("document.forms[0].phone.value")
	fLang = eval("document.forms[0].lang.value")
	if (fPhone == "") 
	 { 
	    line = "";
	    if (fLang == 0) { line = line + "You must insert phone name!\n"; }
	    else { line = line + "Вам необходимо ввести телефонный номер!\n"; }
	    alert (line);
	 }
	else 
	 { 
	    document.forms[0].action="/mod/search_engine.cgi";
	    document.forms[0].submit();
	 }
}

function on_search() {
	fPhone = eval("document.postform1.phone.value")
	fLang = eval("document.postform1.lang.value")
	if (fPhone == "") 
	 { 
	    line = "";
	    if (fLang == 0) { line = line + "You must insert phone name!\n"; }
	    else { line = line + "Вам необходимо ввести телефонный номер!\n"; }
	    alert (line);
	 }
	else 
	 { 
	    open_win2("http://mtk.voipexchange.com/cgi-bin/Exchange.dll/exPrice?phone="+fPhone, 318, 518);
//	    open_win1("/mod/search_frame.cgi?phone="+fPhone,318,518);
	 }
}


function reload_tradefloor() {
    document.forms[0].action="https://customer.voipexchange.ru/cgi-bin/Exchange.dll/Login";
    document.forms[0].submit();
}

// ---------------------------- FORUM -------------------------------
function proc_msg(type,num) {
var line = '';

// Create new message
if (type == 1)
  {
      if (document.postform.subj.value=='') { line = line + "Subject!\n"; }
//    if (document.postform.body.value=='') { line = line + "Body!\n"; }
      if (line != '')
        {
            alert(line);
        }
      else
        {
            document.forms[0].action="/mod/msg_fetch.cgi";
            document.forms[0].submit();
	}
  }

// Read new message
if (type == 2)
  {
      document.postform.fid.value = num;
      document.forms[0].action="/mod/forum.cgi";
      document.forms[0].submit();
  }
}

function check_issues_f() {

	document.forms[0].submit();
}

function check_inv_f() {

        //var Elems = array();

	Elems = document.forms[0].elements;

	Lang = document.forms[0].lang.value;

	for( var i=0; i < Elems.length; i++ ) {
		if( Elems[i].name == 'inv_amt' && (Elems[i].value == '' || Elems[i].value < 1) ) {
		
	
			if( Lang == 'ru' ) {
				alert("Вы не указали сумму");
				return 0;	
			}
			else if ( Lang == 'en' ) {
				alert("You didn't specify amount");
				return 0;
			}

		}
		
                if( Elems[i].name == 'inv_company' && Elems[i].value == '' ) {


                        if( Lang == 'ru' ) {
                                alert("Вы не указали название компании");
                                return 0;
                        }
                        else if ( Lang == 'en' ) {
                                alert("You didn't specify company");
                                return 0;
                        }

                }

                if( Elems[i].name == 'inv_account' && Elems[i].value == '' ) {


                        if( Lang == 'ru' ) {
                                alert("Вы не указали номер договора");
                                return 0;
                        }
                        else if ( Lang == 'en' ) {
                                alert("You didn't specify agreement number");
                                return 0;
                        }

                }

        }

	document.forms[0].submit();

}

function get_inv() {
	document.forms[1].submit();

}




