//
// js/api.js from rian.ru project
//
// $Id: main.js,v 1.5 2008/02/15 09:31:43 disaev Exp $
//

function setCookie ( name, value, days ) {
	var d=new Date();
	d.setDate(d.getDate()+days);

	var strd = d.toGMTString();

	document.cookie = name + '=' + value + ';path=/;expires=' + strd;

	return ( 1 );
}

function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if the cookie exists
      offset += search.length
      //set the index of beginning value
      end = document.cookie.indexOf(";", offset);
      
    if (end == -1) // set the index of the end of cookie value
         end = document.cookie.length;
         returnvalue = unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

//formObj - object form
//fields - string format 'fieldname*empty|badmail*label'

function checkForm ( formObj, fields) {
	var msg = '';
	for ( n = 0; fields[n] ; n++ ) {
		
		st = new String(fields[n]);
		ar = st.split("*");
		name = ar[0];
		request = ar[1];	
		label = ar[2];	
		v = eval("formObj.elements['"+name+"'].value");
			
			//check for empty
			if (request == 'empty') {
				if ( v.match(/^\s*$/) != null ) {
					msg = msg + '- ' + empty_field + ' ' + label + '\n';
				}
			}
			

			
			//check for badmail
			if (request == 'badmail') {
			var emailReg = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$";
			var regex = new RegExp(emailReg);
				if ( v.match(/^\s*$/) == null & regex.test(v) == false ) {
					msg = msg + '- ' + incorrect_adress + ' ' + label + '\n';
				}
			}
		
	}
	if ( msg != '' ) {
		alert(found_errors + ':\n\n'+msg+'\n');
		return ( false );
	}
	return ( true );
}

//formObj - object form
//num - number of checked form
//forms bust be:
// orderNUMy - commercial order
// orderNUMn - test order


function order_check_n_submit(formObj,num){
   		
	flag=0;
	for (i=1; i<num+1; i++) {
		if ( eval('document.getElementById(\'order'+ i +'y\').checked == true || document.getElementById(\'order'+ i +'n\').checked == true') ) {
		flag=1;
		}
	}
	   
	if (flag == 0) {
		alert(empty_services);
		return false;
	} else {
		return true;
	}     
   
}

function order_sum (formObj,field) {

	if (document.getElementById(field+'y').checked == true) {
		if (eval('formObj.'+field+'c.value ==  0')) {
		eval('formObj.sum.value =  parseInt(formObj.sum.value)+parseInt(formObj.'+field+'sum.value)');
		eval('formObj.allsum.value =  parseInt(formObj.allsum.value)+parseInt(formObj.'+field+'sum.value)');
		eval('formObj.'+field+'c.value =  1');
		}
	} else {
		if (eval('formObj.'+field+'c.value ==  1')) {
		eval('formObj.sum.value =  parseInt(formObj.sum.value)-parseInt(formObj.'+field+'sum.value)');
		eval('formObj.allsum.value =  parseInt(formObj.allsum.value)-parseInt(formObj.'+field+'sum.value)');
		eval('formObj.'+field+'c.value =  0');
		}
	}

}

function popup(contentsURL, winName, winProps, width, height) {
     if (!winName) var winName = "_blank";
    // if (!winProps) var winProps = "menubar=0,toolbar=0,resizable=0,location=0,status=0,scrollbars=1";
     if (!width) var w = 600
             else w = width;
     if (!height) var h = 400
                 else h = height;

     var args = "width=" + w + ",height=" + h
      + "," + winProps
     
	 return window.open(contentsURL, winName, args)
}

function open_photo(url, width, height) {
	if (!width) width = 835;
	if (!height) height = 660;

	var left = (screen.width - width)/2;
	var wnd=window.open(url , "","left="+left+",top=10,width=" + width + ",height=" + height + ",directories=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,toolbar=no");
}

function open_send_news() {
 var wnd=window.open("/sendnews.html", "","width=600,height=536,directories=no,menubar=no,status=yes,resizable=no,scrollbars=no,toolbar=no");
}

function makeh(obj,host){
    obj.style.behavior='url(#default#homepage)';
    if (!host) host = 'www.rian.ru';
    obj.setHomePage("http://" + host);
}

function addf(){
    window.external.AddFavorite('http://www.rian.ru', ria_name);
}

function swtch(str) {
	var el = document.getElementById(str);
	if (el.style.display == 'none') {el.style.display = ''}
	else if (el.style.display == '') {el.style.display = 'none'}		
	return false;
}
