User:Goldzahn/wikidata useful.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// copied and adapted from: 
// [[User:Magnus Manske/wikidata_useful.js]]
// Version 1.00: two boxes; the first box is for occupations, taken from [[User:ValterVB/Sandbox]] (new)
//              the second box is for countries.
//              some functions from Magnus Manske version are deleted.
// Version 2.07: added stack for storing property and value in two cookies
// Version 3.00: storing two different values; three cookies
// Version 3.22: storing box don´t allow typos 
// Version 3.23: more locations foe <instance of>
// Version 3.24: unbundling properties
// Version 3.25: label works
// Version 4.00: user can add p- and q- rows 
// Version 5.02: load one select from wikipage User:<your datafile>/data Example: User:Goldzahn/data
// Version 5.03: patch from User:Zuphilip (wrong test if statement is already there)
// Version 6.00: Tool now moveble, storing descriptions now possible, p107 (main type) gone 

$(document).ready(function(){
    /* Hier der jQuery-Code */
    var feature = (function() {
        test = function() {
            id_cnt = 0 ;
            api = '/w/api.php' ;
            var pq = [];   

            if ( mw.config.get('wgNamespaceNumber') !== 0 ) return ;
            if ( mw.config.get('wgAction') !== 'view' ) return ;

            var text1 = '{"name1": {"property1":"p21","firstlabel":"persons"}, "list1":[{"label":"male","item":"q6581097"}, {"label":"female","item":"q6581072"}]}';
            var text2 = '{"name2": {"property2":"p106","firstlabel":"occupations"}, "list2":[{"label":"actor","item":"q33999"},{"label":"archaeologist","item":"q3621491"}, {"label":"architect","item":"q42973"}, {"label":"artist","item":"q483501"}, {"label":"astronaut","item":"q11631"}, {"label":"astronomer","item":"q11063"}, {"label":"aviator","item":"q2095549"}, {"label":"biologist","item":"q864503"}, {"label":"cartoonist","item":"q2489362"}, {"label":"chemist","item":"q593644"}, {"label":"columnist","item":"q1086863"}, {"label":"composer","item":"q36834"}, {"label":"computer scientist","item":"q82594"}, {"label":"designer","item":"q5322166"}, {"label":"economist","item":"q188094"}, {"label":"engineer","item":"q81096"}, {"label":"film producer","item":"q3282637"}, {"label":"film director","item":"q2526255"}, {"label":"football player","item":"q937857"}, {"label":"geologist","item":"q520549"}, {"label":"hacker","item":"q1487"}, {"label":"historian","item":"q201788"}, {"label":"inventor","item":"q205375"}, {"label":"journalist","item":"q1930187"}, {"label":"lawyer","item":"q40348"}, {"label":"mathematician","item":"q170790"}, {"label":"military officer","item":"q189290"}, {"label":"model","item":"q4610556"}, {"label":"musician","item":"q639669"}, {"label":"painter","item":"q1028181"}, {"label":"philosopher","item":"q4964182"}, {"label":"photographer","item":"q33231"}, {"label":"physician","item":"q39631"}, {"label":"physicist","item":"q169470"}, {"label":"pianist","item":"q486748"}, {"label":"politician","item":"q82955"}, {"label":"poet","item":"q49757"}, {"label":"programmer","item":"q5482740"}, {"label":"publisher","item":"q2516866"}, {"label":"scientist","item":"q901"}, {"label":"singer","item":"q177220"}, {"label":"singer-songwriter","item":"q488205"}, {"label":"sportsperson","item":"q2066131"}, {"label":"theologian","item":"q1234713"}, {"label":"writer","item":"q36180"}]}';
            var text3 = '{"name3":{"property3":"p107","firstlabel":"main type"},"list3":[{"label":" person","item":"q215627"},{"label":"organization","item":"q43229"},{"label":"event","item":"q1656682"},{"label":"creative work","item":"q386724"},{"label":"term","item":"q1969448"},{"label":"place","item":"q618123"},{"label":"disambiguation","item":"q11651459"}]}';
            // {"label":"","item":""}, {"label":"","item":""}, {"label":"","item":""}, {"label":"","item":""}, {"label":"","item":""}
 
            var obj = JSON.parse(text1);
            var obj2 = JSON.parse(text2);
            var obj3 = JSON.parse(text3);
            obj.name2 = obj2.name2; obj.list2 = obj2.list2;
            obj.name3 = obj3.name3; obj.list3 = obj3.list3; 
            
            var d = "<div id='tool_text' <div style='position:absolute;right:0px;top:15px;z-index:1'>" ;
            d +="<div id='dialog-1' title='' style='background-color:#ffe1bc'>";

            d +="<ul style='passing-left:0.5em'>";
            d += "<li><select class='frm1'></select></li>" ;
            d += "<li><select class='frm2'></select></li>" ;
            d += "<li id='id1'><span id='user_data' style='cursor:pointer;background-color:white;padding:1px 5px 1px 5px' title='file: User:your username/data' >load user data</span></li>";
            d += "<div id='placeholder'></div>" ;
            d +="</ul>";
 
            d += "<table style='border:thin solid black; border-spacing:5px'>";
 
            d += "<tr id='description-d1'>";
            d += "<td><form><input type='checkbox' class='d' id='description' title='adds the item description' value='' /></td>";
            d += "<td colspan='2'><input type='text' class='label-l' size='2' maxlength='5' id='label-l' value='de' title='your choosen language' />";
            d += "&nbsp;<input type='text' class='label-d' size='12' maxlength='50' id='label-d1' value='item description' /></td></form>";
            d += "</tr>";

            d += "<tr><td colspan='3' style='border:thin solid #ffe1bc; border-top-color:black'></td></tr>";

            d += "<tr><td align='center' style='cursor:pointer' id='plus-p' title='add a new row'>p+</td><td align='center'>property</td><td align='center'>label</td></tr>";
 
            d += "<tr id='row-p1'><td><form><input type='checkbox' class='p' id='p1' value='p1' checked='checked'/>p1</form></td>";
            d += "<td><input type='text' class='property' size='6' maxlength='5' value='p' id='input-p1' /></td>";
            d += "<td><input type='text' class='label-p' size='8' maxlength='25' id='label-p1' value='' /></td></tr>";
 
            d += "<tr><td align='center' style='cursor:pointer' id='plus-q' title='add a new row'>q+</td><td align='center'>value</td><td align='center'>label</td></tr>";
 
            d += "<tr id='row-q1'><td><form><input type='checkbox' class='q' id='q1' value='q1'  title='transfer to database p and q' />q1</form></td>";
            d += "<td><input type='text' class='value' size='6' maxlength='9' value='q' id='input-q1' title='input-q1' /></td>";
            d += "<td><input type='text' class='label-q' size='8' maxlength='25' id='label-q1' value='' title='label-q1' /></td></tr>";

            d += "<tr><td colspan='3' <p align='center'><textarea id='message' cols='21' rows='3'>Clicking p+ and q+ adds new rows. ";
            d += "Clicking one p and one q, updates wikidata. Loading user data from user:your username/data (help: user:Goldzahn/data and disk)</textarea></td></tr>";
            d += "</table>";
            d += "</div><button id='opener'>The Brown Tool</button>";
            d += "</div></div>" ;
            d += "<div id='num-p'></div>" ;
            d += "<div id='num-q'></div>" ;
            d += "</div>" ;
            $('#mw-content-text').append ( d ) ;

            var h, i;
            h = "<option>" + obj.name1.firstlabel + "</option>";
            for (i = 0, len = obj.list1.length; i < len; i++) {
        	    h += "<option value=obj.list1[i].item>" + obj.list1[i].label + "</option>";
            }
            $('.frm1').html(h);
 
            h = "<option>" + obj.name2.firstlabel + "</option>";
            for (i = 0, len = obj.list2.length; i < len; i++) {
                h += "<option value=obj.list2[i].item>" + obj.list2[i].label + "</option>";
            }
            $('.frm2').html(h);
 
            h = "<option>" + obj.name3.firstlabel + "</option>";
            for (i = 0, len = obj.list3.length; i < len; i++) {
                h += "<option value=obj.list3[i].item>" + obj.list3[i].label + "</option>";
            }
            $('.frm3').html(h);
 
            pq.p1 = true; pq.q1 = true;
            $('#num-p').val(2); $('#num-q').val(2); // next p-row and q-row

            mw.loader.using( 'jquery.ui', function () {
                 $( "#dialog-1" ).dialog({
                    autoOpen: true, width: 250, position: { my: "right top", at: "right bottom", of: "#opener" } 
                 });
                 $( "#opener" ).click(function() {
                     $( "#dialog-1" ).dialog( "open" ); 
                 });    
            } );

            function processNextQueueItem2() {
    	 		     var q = mw.config.get('wgPageName').toLowerCase() ;
    	 		     var lang = $('#label-l').val();
    	 		     //console.log ( "item ist: " + q ) ;
    	 		     tryCreateDescription (q, lang.toString(), $('#label-d1').val());
            }
            
function tryCreateDescription (entity, language, description) {
	//console.log ( "tryCreateDescription" ) ;
	//console.log ( entity, language, description ) ;
	$.getJSON ( api , {
		action : 'wbgetentities',
		format : 'json',
		ids : entity,
		props : 'descriptions',
		languages : language
	} , function ( data ) {
		// console.log(data.entities[entity].descriptions[language].value);
		if ( undefined !== data.entities[entity] ) {
			if ( undefined !== data.entities[entity].descriptions ) {
				if ( undefined !== data.entities[entity].descriptions[language] ) {
					console.log ("description does exist: ");
					$('#message').val('description already exists - Sorry!');
					console.log(data.entities[entity].descriptions[language].value);
					return false;
				}	
				else {
					console.log ("description don´t exist");
				}
			}
		}
	createClaim2 ( entity, language, description ) ;
	} ) ;
} 

function createClaim2( entity, mylanguage, description ) {
	var self = this ;
	$.post ( self.api , {
		action : 'query' ,
		meta : 'tokens' ,
		type : 'csrf' ,
		titles : entity ,
		format : 'json'
	} , function ( data ) {
		var mytoken = data.query.tokens.csrftoken ;
		$.post ( self.api , {
			action : 'wbsetdescription' ,
			id : entity ,
    		value : description ,
			token : mytoken ,
    		language : mylanguage ,
			format : 'json'
		} , function ( data ) {
			//console.log(data);	
			if ( undefined !== data.error ) {
				$('#message').val(data.error.info);
			}
			else {
            console.log ( "Creating " + entity + " / " + mylanguage + " / " + description ) ;
            $('#message').val('Creating ' + entity + ' / ' + mylanguage + ' / ' + description); 
			}
		} , 'json' ) ;
	} , 'json' ) ;
} 

function processNextQueueItem(prop, value) {
	// processNextQueueItem( $('#input-' + p_is).val(), $('#input-' + q_is).val())
	var q = mw.config.get('wgPageName').toLowerCase() ;
    tryCreateClaim ( q, prop , value ) ; 
	} 
 
 function tryCreateClaim( entity , property , value ) {
	//console.log ( "tryCreateClaim " + entity + " / " + property + " / " + value ) ;
	var value2 = JSON.parse ( value.replace ( /\D/g , '' ) ) ;
	var nid = value2['numeric-id'] ;
	$.getJSON ( api , {
		action : 'wbgetentities' ,
		format : 'json' ,
		ids : entity ,
		props : 'info|claims'
	} , function ( data ) {
	//console.log(data);
		if ( undefined !== data.entities[entity] ) {
			if ( undefined !== data.entities[entity].claims ) {
				if ( undefined !== data.entities[entity].claims[property] ) {
					var n = data.entities[entity].claims[property] ;
					var exists = false ;
					$.each ( n , function ( k , v ) {
						if ( v.mainsnak.datavalue.value['numeric-id'] == nid ) {
							exists = true ;
							return false ;
						}
					} ) ;
					if ( exists ) {
						console.log ( property+' exists for '+entity ) ;
						return ;
					}
				}
			}
		}
		createClaim ( entity , property , value ) ;
	} ) ;
} 
//END OF Patch
 
 
function createClaim( entity , property , value ) {
	//console.log ( "createClaim " + entity + " / " + property + " / " + value ) ;
	$.post ( api , {
		action : 'query' ,
		meta : 'tokens' ,
		type : 'csrf' ,
		titles : entity ,
		format : 'json'
	} , function ( data ) {
		//console.log(data);
		var mytoken = data.query.tokens.csrftoken ;
		//console.log (data.query.tokens);

		if ( undefined === mytoken ) {
			console.log ( "Cannot get edit token for " + entity ) ;
			return ;
		}
 
		var vo = JSON.parse ( value.replace ( /\D/g , '' ) ) ;
		var value_id = vo['numeric-id']+'' ;
 
 		//console.log ("wbcreateclaim " + entity + property + value);
 		var val = '{"entity-type":"item","numeric-id":' + value.replace(/\D/g,'') + '}' ;
 		//console.log ("val = " + val);
		$.post ( api , {
			action : 'wbcreateclaim' ,
			entity : entity ,
			snaktype : 'value',				
			property : property ,
			value : val,
			token : mytoken ,
			format : 'json'
		} , function ( data ) {
			//console.log(data);
            console.log ( "Creating " + entity + " / " + property + " / " + value ) ;
            $('#message').val('Creating ' + entity + ' / ' + property + ' / ' + value); 
		} 
		, 'json' ) ;
	} , 'json' ) ;
} 
 
             // test input for 'item description'
            $("tr#description-d1").on("click", "input", function(event){
                 if ( $( "input.d:checked" ).length === 1 ) { 
                 	$('#message').val('item description - OK'); 
                 	$('input.d[type=checkbox]').prop('disabled', true); 
                    processNextQueueItem2();  
                    }	
            });

            $('table').on('blur', 'input.label-l, input.label-d', function(event){
          		$.cookie('cookie-d', $('#label-l').val() + '|' + $('#label-d1').val()); 
	            var str = "en ru es ja de fr zh it pt pl nl ceb sv vi war"; // Wiki-languages abbreviations 
	            str += "az bg ca cs da et el eo eo hr id he la lt hu ms no nn ro sk sl sr sh fi tr uk jv"; 
	            var patt =  new RegExp($('#label-l').val(), "i");  // label input valid? 
	            var lang = str.match(patt);          		
				var search = /^[a-zA-Z]{2,5}$/ ; // between 2 and 5 char
				var result = search.test($('#label-l').val()); console.log(result);
 
				// switching checkboxes on and off depending on 'result'
				if ((result === true) && (lang !== null)) 
    			    { $('#message').val(' '); // 'input is valid' 
    			      $('input[type=checkbox]').prop('disabled', false); 
        		      $('input#label-l.label-l').css('backgroundColor', '#ffffff');
    		    }
				else      
        			{ $('#message').val('language is invalid'); 
        			$('input[type=checkbox]').prop('disabled', true);
        		    $('input#label-l.label-l').css('backgroundColor', '#F93');   //'#F93'); 
        	    }
            });

// first block of register events
// register click-event for property
$("#plus-p").on("click", function(event){
var new_span;
var span = [];
span[0] = "<tr id='row-p";
span[1] = "'><td><form><input type='checkbox' class='p' id='p";
span[2] = "'/>p";
span[3] = "</form></td><td><input type='text' class='property' size='6' maxlength='5' id='input-p";
span[4] = "' /></td><td><input type='text' class='label-p' size='8' maxlength='25' id='label-p";
span[5] = "' value='' /></td></tr>";
var n = $('#num-p').val();
new_span = span[0]+n+span[1]+n+span[2]+n+span[3]+n+span[4]+n+span[5];
var p = n - 1 ; 
$('#row-p' + p).after(new_span);
$('#num-p').val($('#num-p').val() * 1 + 1);
pq['p' + n] = true; // add new p in array pq 
});
 
// register click-event for value
$("#plus-q").on("click", function(event){
var new_span;
var span = [];
span[0] = "<tr id='row-q";
span[1] = "'><td><form><input id='q";
span[2] = "' type='checkbox' class='q' id='q";
span[3] = "' title='transfer to database p and q' />q";
span[4] = "</form></td><td><input type='text' class='value' size='6' maxlength='9' id='input-q";
span[5] = "' /></td><td><input type='text' class='label-q' size='8' maxlength='25' id='label-q";
span[6] = "'  value='' /></td></tr>";
var n = $('#num-q').val();
new_span = span[0]+n+span[1]+n+span[2]+n+span[3]+n+span[4]+n+span[5]+n+span[6];
var q = n - 1 ; 
$('#row-q' + q).after(new_span);
$('#num-q').val($('#num-q').val() * 1 + 1);
pq['q' + n] = true; // add new p in array pq 
});
 
// register a blur event for <input-p> or <input-q> tags
$('table').on('blur', 'input.property, input.value', function(event){
var search1 = /^[pPqQ][1-9]/ ; // looking for 'p' or 'P' and one number between 1 to 9
var search2 = /\d/ ;       // strings following 'p1' or 'q1' which should be a number
var result; 
var i = 0; 
var a = []; 
 
do {
           a[i] = $(this).val().charAt(i); 
           i = i + 1;
} while (i < $(this).val().length);
 
result = search1.test(a[0] + a[1]); // console.log(result);
 
for (i = 2; i < $(this).val().length; i++) {
           result = result && search2.test(a[i]); // alert(a[i]);
}
// test if 'p...' or 'q...' is typed into input-p or 'input-q'
var x1 = this.id.charAt(6); 
var x2 = $(this).val().toLowerCase().charAt(0); 
if (x1 != x2) result = false; 
 
// switching checkboxes on and off depending on 'result'
var new_string = this.id.replace('input', '#elem'); // 'input-p1' > '#elem-p1', 'input-q2' > '#elem-q2'
if (result === true) // {console.log('p is OK')} else {console.log('p is wrong')};
          { $('#message').val(' '); // 'input is valid' 
            $(this).css('backgroundColor', '#ffffff');
            pq[this.id.slice(6, 8)] = true; // 'p1', 'q2', ...
          }
else      
          { $('#message').val('input is invalid'); 
            $(this).css('backgroundColor', '#F93'); 
            pq[this.id.slice(6, 8)] = false;
          }
// console.log('new_string = ' + new_string);
var elem = true;
// gets every element of the pq array
for (i in pq) {
           result = result && pq[i];
           }            
 
if (result) 
           {$('input[type=checkbox]').prop('disabled', false); }
else {$('input[type=checkbox]').prop('disabled', true); } 
});
// end first block of register events
 

// second block of register events
$("#user_data").on("click", function(event){
	    	var self = this ;
            $.getJSON ( '//' + 'wikidata.org/w/api.php?callback=?' , {
			action : 'parse' ,
		//	page : 'User:Goldzahn/data' ,
			page : 'User:' + mw.config.get('wgUserName') + '/data' ,
			format : 'json' ,
			prop : 'wikitext'
              } , function ( data ) {
			if ( undefined === data.parse.wikitext ) {
				return ;
			}
                        var text = data.parse.wikitext['*'].replace(/\n/gm,' ') ;
 						// console.log ('getJSON= ' + text);
                        var obj_data = JSON.parse(text);
                        obj.list = obj_data.list;
 
                        var h, i;
                        h = "<div id='userdata'></div>" ;
                        h += "<li><select class='frm_data'>" ;
                        h += "<option>user data</option>";
                        for (i = 0, len = obj.list.length; i < len; i++) {
                            h += "<option value=obj.list[i].item>" + obj.list[i].label + "</option>";
                        }
                        h += "</select></li>" ;
                        $('#id1').remove () ;
                        $('#placeholder').html(h);
 
                        console.log ('Your datafile should be at: User:'+mw.config.get('wgUserName')+'/data');
 
                        $("select.frm_data").change(function () {  
                        var index=this.selectedIndex-1; 
                        if (index >= 0) {
                            processNextQueueItem(obj.list[index].property, obj.list[index].item);
                            }
                        });
              } ) ;
     });
 
     $("select.frm1").change(function () {  
     var self = this ; 	
     var index=this.selectedIndex-1; 
     if (index >= 0) {
       	 processNextQueueItem(obj.name1.property1, obj.list1[index].item);
         }
     });
 
     $("select.frm2").change(function () { 
     var self = this ;
     var index=this.selectedIndex-1; 
     if (index >= 0) {
         processNextQueueItem(obj.name2.property2, obj.list2[index].item);
         }
     });
 
     $("select.frm3").change(function () {
     var self = this ;  	
     var index=this.selectedIndex-1; 
     if (index >= 0) {
         processNextQueueItem(obj.name3.property3, obj.list3[index].item);
         }
     });

         // register a blur event for <input-p, label-p> tags
            $('table').on('blur', 'input.property, input.label-p', function(event){
            var n = $('#num-p').val(); 
            var cookie_string = '';
            var cookie_elem;
            var cookie_label;  
            for (i=1; i<n; i++) {
               cookie_elem =  '#input-p' + i;
               cookie_label = '#label-p' + i;
               if ($(cookie_elem).val() !== '') cookie_string += $(cookie_elem).val() + '|' + $(cookie_label).val() + '|';   
            } 
            $.cookie('cookie-p1', cookie_string);
            });
 
         // register a blur event for <input-q, label-q> tags
            $('table').on('blur', 'input.value, input.label-q', function(event){
            var n = $('#num-q').val(); 
            var cookie_string = '';
            var cookie_elem;
            var cookie_label;  
            for (i=1; i<n; i++) {
               cookie_elem =  '#input-q' + i;
               cookie_label = '#label-q' + i;
               if ($(cookie_elem).val() !== '') cookie_string += $(cookie_elem).val() + '|' + $(cookie_label).val() + '|';   
            } 
            $.cookie('cookie-q1', cookie_string);
            });
 
 
            // test that only one p and only one q is checked
            $("table").on("click", "input", function(event){
            var self = this ;
            var p_is, q_is;
            if ($( "input.p:checked" ).length > 1 && $( "input.d:checked" ).length === 0) $('#message').val('more than one p clicked - please correct input');
            if ($( "input.p:checked" ).length === 1 && $( "input.d:checked" ).length === 0) $('#message').val('one p clicked - O.K. ');
            if ($( "input.p:checked" ).length === 0 && $( "input.d:checked" ).length === 0) $('#message').val('no p clicked - please correct input');
            if ($( "input.p:checked" ).length === 1 && $( "input.q:checked" ).length === 1) 
            	 {
                 $('#message').val('one q and one p clicked');
                 $( "input.p:checked" ).each(function() {
                      p_is = this.id; //console.log( "p is: " + p_is ); 
                 });
                 $( "input.q:checked" ).each(function() {
                      q_is = this.id; //console.log( "q is: " + q_is );
                 });
                 if (($('#input-' + p_is).val() !== '') && ($('#input-' + q_is).val() !== '')) {
                     processNextQueueItem( $('#input-' + p_is).val(), $('#input-' + q_is).val() );
                     } 
                     else { $('#message').val('empty row clicked - please correct input') }
                 }
            });
 
        }; // end function test

        return {
            test: test
        };
     })();

     feature.test ();              

// fetch data from cookies
var x, y;
x = $.cookie('cookie-d');
if (x !== null) { // test if there is a cookie 
    y = x.split('|'); 
    $('#label-l' ).val(y[0]); 
    $('#label-d1').val(y[1]); 
}    

x = $.cookie('cookie-p1'); 
if (x !== null) { // test if there is a cookie 
    y = x.split('|'); 
    $('#input-p1').val(y[0]); 
    $('#label-p1').val(y[1]); 
    $('#input-p1').trigger('blur');  // trigger event. Test if value is valid
    i = 2;
    var num = "2"; // String
    var number;    // Zahl
    var a, b;
    while (i<(y.length-1)) {
           $('#plus-p').trigger('click');
           a = '#input-p' + num; b = '#label-p' + num; number = parseInt(num) + 1; num = number.toString();
           $(a).val(y[i]); i++;
           $(b).val(y[i]);  i++;
           $(a).trigger('blur');  // trigger event. Test if property is valid  
    }
} 
else $('#input-p1').val('p1');
 
x = $.cookie('cookie-q1'); 
if (x !== null) { // test if there is a cookie 
    y = x.split('|'); 
    $('#input-q1').val(y[0]); 
    $('#label-q1').val(y[1]); 
    $('#input-q1').trigger('blur');  // trigger event. Test if value is valid
    i = 2;
    var num2 = "2";
    var number2;
    var a2, b2;
    while (i<(y.length-1)) {
           $('#plus-q').trigger('click'); 
           a2 = '#input-q' + num2; b2 = '#label-q' + num2; number2 = parseInt(num2) + 1; num2 = number2.toString();
           $(a2).val(y[i]);   i++;
           $(b2).val(y[i]); i++;
           $(a2).trigger('blur');  // trigger event. Test if value is valid
    }
} 
else $('#input-q1').val('q1');

});